Question

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

Était-ce utile?

La solution

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

Autres conseils

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

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.

Licencié sous: CC-BY-SA avec attribution
Non affilié à StackOverflow
scroll top