Search code examples
c#dynamics-crm-2011dynamics-crm

How to Check Two Option Set Value in Dynamics CRM Using C#


I have a plugin that triggered to run on Update based on the value of the Two Option Field. If the value is "Yes" then the plugin will run.

I tried to use entity.FormattedValues["fieldname"].toString() to get the text value from the Two Optionset Field. But didn't work.

I don't think that QueryExpression is not suit in my case.CMIIW

Here is my full code:

if (context.InputParameters.Contains("Target") && context.InputParameters["Target"] is Entity)
        {
            Entity entity = context.InputParameters["Target"] as Entity;

            if (entity.LogicalName != "incident")
                return;

            string str = entity.FormattedValues["new_createticket"].ToString();

            if (str == "Yes")
            {
                Entity ticket = new Entity("new_troubleticket");
                ticket["new_subject"] = entity.GetAttributeValue<String>("title");
                Guid ticketid = service.Create(ticket);

                entity["new_troubleticketid"] = new EntityReference("new_troubleticket", ticketid);
                entity["new_createticket"] = false;
            }

            service.Update(entity);
        }

If you have any solution, please share with me Best regards


Solution

  • You should Register your plugin on Update,Post-Operation. In the plugin context, Target Entity you will only get the fields which are updated. To get other fields that are required for your custom business logic You have two options.

    1.  Retrieve the Entity by Guid with all the required attributes
    2.  Register a Post-Entity Image to get all the attributes.
    

    https://msdn.microsoft.com/en-us/library/gg309673.aspx#bkmk_preandpost

    Taken from the Above Url:

    Registering for pre or post images to access entity attribute values results in improved plug-in performance as compared to obtaining entity attributes in plug-in code through RetrieveRequest or RetrieveMultipleRequest requests.

    Now coming to your code/logic problem. In CRM you should have created the Two-Option correctly, by this I mean

    By Default when we create field of Data Type Two Options it has two Options

    1. Label=No and Value=0
    2. Label= Yes and Values=1
    

    If you have set the Text ='Yes' and value =1 for new_createticket, then change your code to this

     //string str = entity.FormattedValues["new_createticket"].ToString();
     Entity entity = context.InputParameters["Target"] as Entity;
    

    Now here is the tricky part, the above entity may contain new_cr‌​ea‌​teticket field, if it is changed, but if it is not changed then it will not be present. Here you can either do this

    Entity updatedEntity = _service.Retrieve(entity.LogicalName,entity.Id,new ColumnSet("new_cr‌​ea‌​teticket","title"))
    if (updatedEntity.GetAttribute‌​‌​Value<bool>("new_cr‌​ea‌​teticket") == true)
      {
         Entity ticket = new Entity("new_troubleticket");
         ticket["new_subject"] = updatedEntity.GetAttributeValue<String>("title");
         Guid ticketid = service.Create(ticket);
    
    updatedEntity["new_troubleticketid"] = new EntityReference("new_troubleticket", ticketid);
        updatedEntity["new_createticket"] = false;
      }
    

    The Above code should work fine if you verify that new_createticket is a Two-Option field, and on update your are either retrieving it or passing it in the Post-Entity Image.