Skip to main content

Notifications

Microsoft Dynamics CRM (Archived)

Unable to retrieve option-set

Posted on by 30
Hello Guys, 

I am facing a peculiar problem, where I am trying to retrieve the option-sets (Code snippet added below) but I get an error saying "Could not find optionset".
This only happens on production instance but it works fine on the developer instance. I have verified that the option-set I am trying to retrieve is present in that instance and the solution is published.


var retrieveOptionSetRequest = new RetrieveOptionSetRequest() { Name = optionName }; var response = (RetrieveOptionSetResponse)org.Execute(retrieveOptionSetRequest);

Any idea what could have caused this issue?

Thanks

Danish Aalam

*This post is locked for comments

  • Verified answer
    Danish Aalam Profile Picture
    Danish Aalam 30 on at
    RE: Unable to retrieve option-set

    Hello,

    I thank all of you for your responses. I figured it yesterday that the option-set was not global. That was the reason I was unable to retrieve them. I made the option-set global and now it works like a charm.

    Thanks

    Danish Aalam

  • Suggested answer
    jestuder Profile Picture
    jestuder 156 on at
    RE: Unable to retrieve option-set

    Here are some methods I use to get option set by label or by value.  There is 2 for local options set and 2 for global option set.  1 returns the label and one returns the value based on what you are trying to accomplish.  Hope this helps with what you are looking for.  If it does please high the verified answer button.  Thanks.

    public string GetLocalOptionSetLabel(string entityLogicalName, string optionSetName, int value, IOrganizationService service)
            {
                XrmServiceContext crmContext = new XrmServiceContext(service);
                RetrieveEntityRequest retrieveDetails = new RetrieveEntityRequest
                {
                    EntityFilters = EntityFilters.All,
                    LogicalName = entityLogicalName
                };
                RetrieveEntityResponse retrieveEntityResponseObj = (RetrieveEntityResponse)service.Execute(retrieveDetails);
                EntityMetadata metadata = retrieveEntityResponseObj.EntityMetadata;
                PicklistAttributeMetadata picklistMetadata = metadata.Attributes.FirstOrDefault(attribute => string.Equals(attribute.LogicalName, optionSetName, StringComparison.OrdinalIgnoreCase)) as PicklistAttributeMetadata;
                OptionSetMetadata options = picklistMetadata.OptionSet;
                IList<OptionMetadata> OptionsList = (from o in options.Options
                                                     where o.Value.Value == value
                                                     select o).ToList();
                string optionsetLabel = (OptionsList.First()).Label.UserLocalizedLabel.Label;
                return optionsetLabel;
            }

    public string GetGlobalOptionSetLabel(string optionSetName, int value, IOrganizationService service)
            {
                XrmServiceContext crmContext = new XrmServiceContext(service);
                string optionSetLabel = "";
                RetrieveOptionSetRequest retrieveOptionSetRequest = new RetrieveOptionSetRequest { Name = optionSetName };
                RetrieveOptionSetResponse retrieveOptionSetResponse = (RetrieveOptionSetResponse)crmContext.Execute(retrieveOptionSetRequest);
                OptionSetMetadata retrievedOptionSetMetadata = (OptionSetMetadata)retrieveOptionSetResponse.OptionSetMetadata;
                OptionMetadata[] optionArray = retrievedOptionSetMetadata.Options.ToArray();
                foreach (var option in optionArray)
                {
                    if(option.Value.Value == value)
                    {
                        optionSetLabel = option.Label.LocalizedLabels[0].Label;
                    }
                }
                return optionSetLabel;
            }

            public int GetLocalOptionSetValue(string entityLogicalName, string optionSetName, string label, IOrganizationService service)
            {
                XrmServiceContext crmContext = new XrmServiceContext(service);
                RetrieveEntityRequest retrieveDetails = new RetrieveEntityRequest
                {
                    EntityFilters = EntityFilters.All,
                    LogicalName = entityLogicalName
                };
                RetrieveEntityResponse retrieveEntityResponseObj = (RetrieveEntityResponse)service.Execute(retrieveDetails);
                EntityMetadata metadata = retrieveEntityResponseObj.EntityMetadata;
                PicklistAttributeMetadata picklistMetadata = metadata.Attributes.FirstOrDefault(attribute => string.Equals(attribute.LogicalName, optionSetName, StringComparison.OrdinalIgnoreCase)) as PicklistAttributeMetadata;
                OptionSetMetadata options = picklistMetadata.OptionSet;
                IList<OptionMetadata> OptionsList = (from o in options.Options
                                                     where o.Label.UserLocalizedLabel.Label == label
                                                     select o).ToList();
                int optionsetValue = (OptionsList.First()).Value.Value;
                return optionsetValue;
            }

            public int GetGlobalOptionSetValue(string optionSetName, string label, IOrganizationService service)
            {
                XrmServiceContext crmContext = new XrmServiceContext(service);
                int optionSetValue = 0;
                RetrieveOptionSetRequest retrieveOptionSetRequest = new RetrieveOptionSetRequest { Name = optionSetName };
                RetrieveOptionSetResponse retrieveOptionSetResponse = (RetrieveOptionSetResponse)crmContext.Execute(retrieveOptionSetRequest);
                OptionSetMetadata retrievedOptionSetMetadata = (OptionSetMetadata)retrieveOptionSetResponse.OptionSetMetadata;
                OptionMetadata[] optionArray = retrievedOptionSetMetadata.Options.ToArray();
                foreach (var option in optionArray)
                {
                    if (option.Label.LocalizedLabels[0].Label == label)
                    {
                        optionSetValue = option.Value.Value;
                    }
                }
                return optionSetValue;
            }


  • Verified answer
    Nuno Profile Picture
    Nuno 2,005 on at
    RE: Unable to retrieve option-set

    Hi,

    The RetrieveOptionSetRequest message is used to retrieve  a global option sets. The option set you are trying to retrieve is also an global one in the production system?

  • Danish Aalam Profile Picture
    Danish Aalam 30 on at
    RE: Unable to retrieve option-set

    I am copying the schema name from CRM solution to refer the option-set. The code doesn't work for the out of the box option-sets as well.

  • Danish Aalam Profile Picture
    Danish Aalam 30 on at
    RE: Unable to retrieve option-set

    I even tried retrieving out of the box option sets like "customertypecode" but unable to retrieve them from this particular instance.

  • ashlega Profile Picture
    ashlega 34,475 on at
    RE: Unable to retrieve option-set

    Maybe that option set in production has slightly different schema name(upper case vs lower case, for example)?

  • Mahendar Pal Profile Picture
    Mahendar Pal 45,095 on at
    RE: Unable to retrieve option-set

    Please make sure optionset is from same publisher, it may be it has the same display name but having a different prefix ??

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

December Spotlight Star - Muhammad Affan

Congratulations to a top community star!

Top 10 leaders for November!

Congratulations to our November super stars!

Community AMA December 12th

Join us as we continue to demystify the Dynamics 365 Contact Center

Leaderboard

#1
André Arnaud de Calavon Profile Picture

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

#2
Martin Dráb Profile Picture

Martin Dráb 230,149 Most Valuable Professional

#3
nmaenpaa Profile Picture

nmaenpaa 101,156

Leaderboard

Featured topics

Product updates

Dynamics 365 release plans