Skip to main content

Notifications

Microsoft Dynamics CRM (Archived)

How to retrieve optionset value or optionset selected text using plugin?

Posted on by Microsoft Employee

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

  • Suggested answer
    ashlega Profile Picture
    ashlega 34,475 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
    Community Member Microsoft Employee 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
    Ivan Ficko 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
    Emre GULCAN 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
    ashlega 34,475 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

November Spotlight Star - Khushbu Rajvi

Congratulations to a top community star!

Forum Structure Changes Complete!

🔔 Be sure to subscribe to the new forums you are interested in to stay up to date! 🔔

Dynamics 365 Community Platform update – Oct 28

Welcome to the next edition of the Community Platform Update. This is a status …

Leaderboard

#1
André Arnaud de Calavon Profile Picture

André Arnaud de Cal... 291,113 Super User 2024 Season 2

#2
Martin Dráb Profile Picture

Martin Dráb 229,918 Most Valuable Professional

#3
nmaenpaa Profile Picture

nmaenpaa 101,156

Leaderboard

Featured topics

Product updates

Dynamics 365 release plans