1

I'm doin a simple query linq to retrieve a label from an optionSet. Looks like the formatted value for the option set is missing. Someone knows why is not getting generated?

Best Regards

Mauro De Biasio
  • 1,146
  • 6
  • 16
  • Please post you code. Are you querying the option set metadata or trying to get the label directly from the option set? The label is in the metadata, the option set only contains a value. Is the Linq query in an external application? – Bvrce Mar 21 '14 at 08:54

3 Answers3

2

Sorry for the unclear post. I discovered the problem, and the reason of the missing key as formattedvalue.

The issue is with the way you retrieve the property. With this query:

var invoiceDetails = from d in xrmService.InvoiceSet      
                     where d.InvoiceId.Value.Equals(invId)
                     select new
                     {
                         name = d.Name,
                         paymenttermscode = d.PaymentTermsCode
                     }

I was retrieving the correct int value for the option set, but what i needed was only the text. I changed the query this way:

var invoiceDetails = from d in xrmService.InvoiceSet         
                     where d.InvoiceId.Value.Equals(invId)
                     select new
                     {
                         name = d.Name,
                         paymenttermscode = d.FormattedValues["paymenttermscode"]
                     }

In this case I had an error stating that the key was not present. After many attempts, i tried to pass both the key value and the option set text, and that attempt worked just fine.

var invoiceDetails = from d in xrmService.InvoiceSet                        
                     where d.InvoiceId.Value.Equals(invId)
                     select new
                     {
                         name = d.Name,
                         paymenttermscode = d.PaymentTermsCode,
                         paymenttermscodeValue = d.FormattedValues["paymenttermscode"]
                     }

My guess is that to retrieve the correct text associated to that option set, in that specific entity, you need to retrieve the int value too. I hope this will be helpful.

Best Regards

Mauro De Biasio
  • 1,146
  • 6
  • 16
0

You're question is rather confusing for a couple reasons. I'm going to assume that what you mean when you say you're trying to "retrieve a label from an OptionSet" is that you're attempting to get the Text Value of a particular OptionSetValue and you're not querying the OptionSetMetadata directly to retrieve the actual LocalizedLabels text value. I'm also assuming "formatted value for the option set is missing" is referring to the FormattedValues collection. If these assumptions are correct, I refer you to this: CRM 2011 - Retrieving FormattedValues from joined entity

Community
  • 1
  • 1
Daryl
  • 18,592
  • 9
  • 78
  • 145
  • Yes what you guessed is correct. I need this because I'm passing a dataset to a report, and what they were using in the query was the string and not the option set key. My issue is different, is the primary entity and 5 option sets are coming across as formatted values, but not the one that i need (is a default option set on an Invoice "paymenttermscode" in the specific). – Mauro De Biasio Mar 23 '14 at 23:48
  • Also I tried to remove the join condition on the linq query to see if in the list that formatted value was appearing, but still nothing. – Mauro De Biasio Mar 23 '14 at 23:55
  • @Draiden I need to see the Linq Query that you're attempting, and the OptionSetValue you're attempting to retrieve, that is not being retrieved. – Daryl Mar 24 '14 at 13:47
  • If you look at my reply there is the code and the reason why it wasn't working :) thanks for your help ;) – Mauro De Biasio Mar 25 '14 at 01:03
-1

The option set metadata has to be queried.

Here is an extension method that I wrote:

public static class OrganizationServiceHelper
{
    public static string GetOptionSetLabel(this IOrganizationService service, string optionSetName, int optionSetValue)
    {
        RetrieveOptionSetRequest retrieve = new RetrieveOptionSetRequest
        {
            Name = optionSetName
        };
        try
        {
            RetrieveOptionSetResponse response = (RetrieveOptionSetResponse)service.Execute(retrieve);
            OptionSetMetadata metaData = (OptionSetMetadata)response.OptionSetMetadata;
            return metaData.Options
                .Where(o => o.Value == optionSetValue)
                .Select(o => o.Label.UserLocalizedLabel.Label)
                .FirstOrDefault();
        }
        catch { }
        return null;
    }
}

RetrieveOptionSetRequest and RetrieveOptionSetResponse are on Microsoft.Xrm.Sdk.Messages.

Call it like this:

string label = service.GetOptionSetLabel("wim_continent", 102730000);

If you are going to be querying the same option set multiple times, I recommend that you write a method that returns the OptionSetMetadata instead of the label; then query the OptionSetMetadata locally. Calling the above extension method multiple times will result in the same query being executed over and over.

Bvrce
  • 2,170
  • 2
  • 27
  • 45
  • Incorrect @Bvrce, CRM has a Formatted Values Collection that contains the text values: http://nishantrana.wordpress.com/2014/01/31/get-optionset-label-using-formattedvalues-property-of-entity-in-plugin-in-crm/ so you don't have to retrieve the OptionSetMetadata – Daryl Mar 21 '14 at 12:47
  • Thanks @Daryl for the comment. I agree that using the formatted value collection is a better solution if the query is in a custom workflow activity or plugin. In an external application there will be no Target entity to get the formatted values collection from. Can you agree that querying the option set metadata is the correct solution if the Linq query is in an external application? – Bvrce Mar 21 '14 at 20:54
  • 1
    Actually, the `FormattedValues` collection is a property on the Entity itself. The example in the blog post was using a Target, but it's populated on Entities that are retrieved using the SDK, so it's available to even external applications as well. – Daryl Mar 22 '14 at 02:02
  • Thanks for explaining that. In light of what you have just written I would venture to say that the only reason to query the optionset metadata would be if a corresponding entity is not involved as well; for example to populate a drop down list in an external application. Do you think that there could be a marginal performance increase in only requesting the optionsetvalues when a large number of records are queried? The labels could then be resolved from a local copy of the optionset metadata. Would this be a micro-optimization? – Bvrce Mar 22 '14 at 08:30
  • I'm not aware of a way to not have the FormattedValues returned as part of the query, so I'm not seeing it as a performance issue. It is quite a bit more difficult to access the FormattedValues IMHO. Because of this reason, we generally always cache the entire OptionSet metadata and use dictionaries to lookup the text values. And, if you need to display a drop down list, you have to use the OptionSetMetadata option since the FormattedValues collection will only have the text value for the particular OptionSetValue instance. – Daryl Mar 24 '14 at 13:51