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 retrieve optionset value or optionset selected text using plugin?

(0) ShareShare
ReportReport
Posted on by

hi all,

please suggest me how to retrieve optionset value or optionset selected text using plugin.

i tried below code but not working,throwing an error "given key was not present in dictionary"

foreach (Entity results in collection.Entities)
{

string status = results .FormattedValues["tj_status"].ToString(); 

int status = ((OptionSetValue)results.Attributes["tj_status"]).Value;

}

Thanks!

*This post is locked for comments

I have the same question (0)
  • Suggested answer
    ashlega Profile Picture
    34,477 on at
    RE: How to retrieve optionset value or optionset selected text using plugin?

    Try going to the advanced find and search for all records where tj_status is empty. If you get anything in the results, those are the records on which your code is failing.

     You likely need to add a condition (somewhere)

    if(results.Contains("tj_status")){

    }

  • Community Member Profile Picture
    on at
    RE: How to retrieve optionset value or optionset selected text using plugin?

    //my code:i want to update parent record when the child records are updated..

    //i used post-operation and the message is on update of child entity("tj_patient")

    //my parent entity name is "tj_doctor"

    Entity records = (Entity)context.InputParameters["Target"];

                   if (records.LogicalName == "tj_patient")

                   {

                       int statuscount = 0;

                       int status;

                       Guid doctorid;

                       QueryExpression query = new QueryExpression();

                       query.EntityName = records.LogicalName;

                       query.ColumnSet = new ColumnSet("tj_name","tj_status");

                       EntityCollection collection = service.RetrieveMultiple(query);

                       foreach (Entity results in collection.Entities)

                       {                      

                           status = ((OptionSetValue)results.Attributes["tj_status"]).Value;

                           EntityReference reference = (EntityReference)records.Attributes["tj_doctor"];

                           doctorid = reference.Id;

                           ColumnSet parentcolumns = new ColumnSet("tj_status");

                           Entity parentrecord = service.Retrieve("tj_doctor", doctorid, parentcolumns);

                           if (status == 212200000)

                           {

                               statuscount++;

                               if (statuscount >= 4)

                               {

                                   parentrecord.Attributes["tj_status"] = new OptionSetValue(212200000);

                                   service.Update(parentrecord);

                               }

                           }

                       }

                   }

  • Ivan Ficko Profile Picture
    1,380 on at
    RE: How to retrieve optionset value or optionset selected text using plugin?

    Do you get error for both lines or just one?

  • Emre GULCAN Profile Picture
    2,379 on at
    RE: How to retrieve optionset value or optionset selected text using plugin?

    Hi Tejagupta,

    If your optionset attribute didn't change in form (or via SDK code), you can not find this attribute in your plugin.

    For this, you have to use "entity / plugin images", you can easly register images from Plugin Registration Tool. You can find more details on https://msdn.microsoft.com/en-gb/library/gg309673.aspx 

  • Suggested answer
    ashlega Profile Picture
    34,477 on at
    RE: How to retrieve optionset value or optionset selected text using plugin?

    Hi,

      which line is it failing on? Technically, that's exactly how you are supposed to be doing that.. but, on the other hand, if a value has not been selected for a particular record, you'll get that error. So you may want to check, first, if the attribute is there (for each record)

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