Skip to main content

Notifications

Announcements

No record found.

Microsoft Dynamics CRM (Archived)

How to Set Default Values to Entity Fields

Posted on by 475

Hello,

I want to set default values in C# by nullable value in some common function which will set value based on field value otherwise it will default value as NULL. I want to set default values for GUID, Entityreference, DateTime, Optionsetvalue and String.

Please suggest how to set default values by null checking .

public object SetAttributeValue(string attributeType, object attributevalue, bool isFormattedValue )
{
object value = null;
bool IsFalse = false;

bool isAliasedValue = objEntity[attribute] is AliasedValue ? !IsFalse : IsFalse;
switch (attributeType)
{
case "EntityReference":
// Need code to set default value for entity reference where i will pass entity reference value and need to extract guid and return guid else set default guid.
break;
case "Guid":
//Need to check if AttributeValue has value otherwise set default guid if it NUll
break;
case "DateTime":
// value= Convert.ToDateTime(attributevalue) ? null : (Convert.ToDateTime(attributevalue));
value = Convert.ToDateTime(attributevalue) : Convert.ToString(attributevalue);
break;
case "OptionSetValue":
value = (Convert.ToInt32(attributevalue)) == 0 ? null : new OptionSetValue(Convert.ToInt32(attributevalue));

break;
default:
value = string.IsNullOrWhiteSpace(Convert.ToString(attributevalue)) ? null : (Convert.ToString(attributevalue));
break;
}

return value;

*This post is locked for comments

  • Verified answer
    gdas Profile Picture
    gdas 50,085 on at
    RE: How to Set Default Values to Entity Fields

    Hi Krishna ,

    For datetime you can write like below -

      value = attributevalue != null ? (System.DateTime)attributevalue : (Nullable<DateTime>)null;

  • Verified answer
    gdas Profile Picture
    gdas 50,085 on at
    RE: How to Set Default Values to Entity Fields

    Hi Krishna ,

    This is correct -

     value = string.IsNullOrWhiteSpace(Convert.ToString(attributevalue)) ? Guid.Empty : ((Guid)(attributevalue));

  • dkrishna Profile Picture
    dkrishna 475 on at
    RE: How to Set Default Values to Entity Fields

    For GUID iam handling like this

                       value = string.IsNullOrWhiteSpace(Convert.ToString(attributevalue)) ? Guid.Empty : ((Guid)(attributevalue));

    is this correct ?

  • dkrishna Profile Picture
    dkrishna 475 on at
    RE: How to Set Default Values to Entity Fields

    Thanks Aric  . For DateTime iam doing like this  is this correct ?

                       value = attributevalue != null ? attributevalue : (Nullable<DateTime>)null;

  • dkrishna Profile Picture
    dkrishna 475 on at
    RE: How to Set Default Values to Entity Fields

    Here i will pass only Value of Attribute and Datatype so based on Datatype passed i need to check null and set default value from common function.

    iam trying like this

    case "EntityReference":

                           if (attributevalue!=null)

                           {

                           value = ((EntityReference)attributevalue).Id;

                           }

                           else

                           {

                               value = Guid.Empty;

                           }

                           break;

    is this correct ?

    Pls suggest in same way how to check if AttributeValue is Guid and set deafult value as Guid.Empty if it is NULL.

  • Aric Levin Profile Picture
    Aric Levin 30,188 on at
    RE: How to Set Default Values to Entity Fields

    Just add

    if (attributeValue != null)

    {

     // Add logic here

    }

  • Suggested answer
    RaviKashyap Profile Picture
    RaviKashyap 55,410 on at
    RE: How to Set Default Values to Entity Fields

    Hi Krishna,

    You can have a top level check for null. This below code -

    ============

    public object SetAttributeValue(Entity objEntity, string attributeName)

           {

               if (objEntity != null && !string.IsNullOrEmpty(attributeName) && objEntity.Contains(attributeName))

               {

                   return objEntity[attributeName];

               }

               else

               {

                   return null;

               }

           }

    ==============

    Hope this helps.

  • dkrishna Profile Picture
    dkrishna 475 on at
    RE: How to Set Default Values to Entity Fields

    Thanka Aric.. How to check attributevalue with null and set default value. If you see default type in above code i have set for string by checking null.  Pls suggest if you know for datetime.

  • Aric Levin Profile Picture
    Aric Levin 30,188 on at
    RE: How to Set Default Values to Entity Fields

    I think the following will work for EntityReference:

    value = ((EntityReference)attributevalue).Id;

  • dkrishna Profile Picture
    dkrishna 475 on at
    RE: How to Set Default Values to Entity Fields

    Iam creating webapi so i need to use this function.

Under review

Thank you for your reply! To ensure a great experience for everyone, your content is awaiting approval by our Community Managers. Please check back later.

Helpful resources

Quick Links

December Spotlight Star - Muhammad Affan

Congratulations to a top community star!

Top 10 leaders for November!

Congratulations to our November super stars!

Tips for Writing Effective Suggested Answers

Best practices for providing successful forum answers ✍️

Leaderboard

#1
André Arnaud de Calavon Profile Picture

André Arnaud de Cal... 291,280 Super User 2024 Season 2

#2
Martin Dráb Profile Picture

Martin Dráb 230,235 Most Valuable Professional

#3
nmaenpaa Profile Picture

nmaenpaa 101,156

Leaderboard

Featured topics

Product updates

Dynamics 365 release plans