web
You’re offline. This is a read only version of the page.
close
Skip to main content
Community site session details

Community site session details

Session Id :
Microsoft Dynamics CRM (Archived)

How to Set Default Values to Entity Fields

(0) ShareShare
ReportReport
Posted on by 483

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

I have the same question (0)
  • dkrishna Profile Picture
    483 on at
    RE: How to Set Default Values to Entity Fields

    I want to set default value based on datatype not Null value as default. Pls suggest.

  • Community Member Profile Picture
    on at
    RE: How to Set Default Values to Entity Fields

    You have 2 options:

    1. This is tedious process and not suggestible if you have lot many attributes. Go with business rule-set default value.

    2.Register a plug-in in per validation or preoperation of Create message and set  respective default values when it’s null

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

    Iam creating webapi so i need to use this function.

  • Aric Levin - MVP Profile Picture
    30,190 Moderator 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
    483 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.

  • Suggested answer
    RaviKashyap Profile Picture
    55,410 Moderator 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.

  • Aric Levin - MVP Profile Picture
    30,190 Moderator on at
    RE: How to Set Default Values to Entity Fields

    Just add

    if (attributeValue != null)

    {

     // Add logic here

    }

  • dkrishna Profile Picture
    483 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.

  • dkrishna Profile Picture
    483 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
    483 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 ?

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

Responsible AI policies

As AI tools become more common, we’re introducing a Responsible AI Use…

Abhilash Warrier – Community Spotlight

We are honored to recognize Abhilash Warrier as our Community Spotlight honoree for…

Leaderboard > 🔒一 Microsoft Dynamics CRM (Archived)

#1
HR-09070029-0 Profile Picture

HR-09070029-0 2

#1
UllrSki Profile Picture

UllrSki 2

#3
ED-30091530-0 Profile Picture

ED-30091530-0 1

Last 30 days Overall leaderboard

Featured topics

Product updates

Dynamics 365 release plans