locked
CRM 2011 C#: Setting an Option Set Field RRS feed

  • Question

  • Hello, I'm trying to set the value of an option set field in my C# code but I am running into difficulties. I found some code examples but they are not working for me. I am using the following code:

    IPluginExecutionContext context = localContext.PluginExecutionContext;
    
    IOrganizationService service = localContext.OrganizationService;
    
    ITracingService tracingService = localContext.TracingService;
    
    if (localContext == null)
    {
       throw new ArgumentNullException("localContext");
    }
    
    Entity targetEntity = null;
    targetEntity = (Entity)context.InputParameters["Target"];
    
    targetEntity.Attributes.Add("apd_emailstatus", new OptionSetValue(921050001));

    I have the correct name for the field and the correct value, but nothing is happening when I run my code. I have my plugin set to Post-Operation running Async on Update. Does anyone have any ideas? Thank you in advance.

    Mike Karls

    Tuesday, July 8, 2014 5:00 PM

Answers

  • Hi Mike,

    You can create the organisation service context like this:

    OrganizationServiceContext _orgContext = new OrganizationServiceContext(service);

    Then use the code below to update and save the record once you have set the option set value:

    targetEntity.Attributes.Add("apd_emailstatus", new OptionSetValue(921050001));
    _orgContext.UpdateObject(targetEntity);
    _orgContext.SaveChanges();

    Thanks

    Sachith



    Sachith Chandrasiri

    • Proposed as answer by Sachith Vidanage Wednesday, July 9, 2014 12:59 AM
    • Marked as answer by Mike Karls Wednesday, July 9, 2014 12:59 PM
    Wednesday, July 9, 2014 12:53 AM

All replies

  • Your code is trying to set the field by adding the attribute to the Target InputParameter. This technique only works on a synchronous plugin step on a Pre stage.

    If you want to update the entity in a Post stage and/or asynchronously, you have to explicitly use the Update message


    Microsoft CRM MVP - http://mscrmuk.blogspot.com/ http://www.excitation.co.uk

    Tuesday, July 8, 2014 5:18 PM
    Moderator
  • Thank you David, do you have any resources or examples of how to do it this way?

    Mike Karls

    Tuesday, July 8, 2014 5:49 PM
  • Hi Mike,

    You can create the organisation service context like this:

    OrganizationServiceContext _orgContext = new OrganizationServiceContext(service);

    Then use the code below to update and save the record once you have set the option set value:

    targetEntity.Attributes.Add("apd_emailstatus", new OptionSetValue(921050001));
    _orgContext.UpdateObject(targetEntity);
    _orgContext.SaveChanges();

    Thanks

    Sachith



    Sachith Chandrasiri

    • Proposed as answer by Sachith Vidanage Wednesday, July 9, 2014 12:59 AM
    • Marked as answer by Mike Karls Wednesday, July 9, 2014 12:59 PM
    Wednesday, July 9, 2014 12:53 AM
  • Could you share plugin registration information ??


    Our Website| Our Blog | Follow US | My Facebook Page | Microsoft Dynamics CRM 2011 Application Design
    Make sure to "Vote as Helpful" and "Mark As Answer",if you get answer of your question.

    Wednesday, July 9, 2014 2:27 AM
    Moderator