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 can I set OptionSetValue?

(0) ShareShare
ReportReport
Posted on by 225

I'm trying to set (C#) value of a OptionSetValue field in a entity. While the entity is created and simpler fields (int/strings/...) are set, the OptionSetValue is null.

I followed the helper code here: https://msdn.microsoft.com/en-us/library/hh547435.aspx

Are there any tricks to it? May the int value I'm trying to set wrong; would it result in a null value if it expects value (for instance) 852000000-852000008 and I set it to a simple "2" (int value 2, not string).

*This post is locked for comments

I have the same question (0)
  • _Lars_ Profile Picture
    225 on at
    RE: How can I set OptionSetValue?

    Whats the difference?

  • Suggested answer
    gdas Profile Picture
    50,091 Moderator on at
    RE: How can I set OptionSetValue?

    Yes you are correct  , this is because late binding , some days back also I faced similar issue. However if you used early bound its works.

  • Verified answer
    _Lars_ Profile Picture
    225 on at
    RE: How can I set OptionSetValue?

    This was strange... it works if I update the entity right after I created it. The logic is:

    var entityId = _service.Create(myEntity);
    
    myEntity.Id = entityId;
    
    _service.Update(myEntity);

    It  does not work If I don't update. Why is this? Is there some late binding on the OptionSetValue that does not work on creation?

  • Suggested answer
    Rawish Kumar Profile Picture
    13,758 on at
    RE: How can I set OptionSetValue?

    Goutam is right - you cannot set a value which is not there in the CRM field itself at first place. Remember you are just settings a field value not creating new values - if you need to create new value and inset in the field problematically . you will have to call InsertStatusValueRequest class.

    but again you can easily do it in the frontend. Just in case m sharing the code.

    private void InsertStatusReasonValue(string entityLogicalName, int stateCode, int statusCode, string label)

          {

              InsertStatusValueRequest insertStatusValueRequest = new InsertStatusValueRequest

              {

                  AttributeLogicalName = "statuscode",

                  EntityLogicalName = entityLogicalName,

                  Label = new Label(label, 1033),

                  Value = statusCode, // integer value of current State Code

                  StateCode = stateCode // integer value of new Status Code

              };

              int _insertedStatusValue = ((InsertStatusValueResponse)service.Execute(insertStatusValueRequest)).NewOptionValue;

          }

  • Suggested answer
    gdas Profile Picture
    50,091 Moderator on at
    RE: How can I set OptionSetValue?

    Hello ,

    The optionset value should be available in your optionset field otherwise it obvious you will get error .

    Are you getting any error with valid values in the optionset field.

  • _Lars_ Profile Picture
    225 on at
    RE: How can I set OptionSetValue?

    Yes, I'm not able to set an invalid value. Valid values are 972930000 to ...009. Setting a simple 1 throws an exception that value is outside of range. 

    I get no errors when I set value to 972930003 for instance for the entity field. But If I take the guid of the entity and look up the record in CRM rest tool the fields is null.

    I've also tried using my entity and field:

    OptionSetValue test= new OptionSetValue(972930003);
    
    entity.Attributes.Add("new_department",  test);

    Is there a read only setting for the field in Dynamics? It should throw an error. Is it my user rights maybe?

  • Mahendar Pal Profile Picture
    45,095 on at
    RE: How can I set OptionSetValue?

    Hi,

    Actually you should get error if you will specify wrong value while setting option set, something out of the range, not sure how you are getting it null.

  • Suggested answer
    gdas Profile Picture
    50,091 Moderator on at
    RE: How can I set OptionSetValue?

    Hello,

    Please refer below-

    crmbusiness.wordpress.com/.../crm-2011-how-to-set-optionsetvalue-in-a-plugin

  • Suggested answer
    Rawish Kumar Profile Picture
    13,758 on at
    RE: How can I set OptionSetValue?

    Hi there,

    try below :

    OptionSetValue test= new OptionSetValue(100000000);

    entity.Attributes.Add("new_department",  test);

    mark my suggestion as verified if helpful.

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…

Andrés Arias – Community Spotlight

We are honored to recognize Andrés Arias as our Community Spotlight honoree for…

Leaderboard > 🔒一 Microsoft Dynamics CRM (Archived)

#1
Aric Levin - MVP Profile Picture

Aric Levin - MVP 2 Moderator

#2
MA-04060624-0 Profile Picture

MA-04060624-0 1

Last 30 days Overall leaderboard

Featured topics

Product updates

Dynamics 365 release plans