Extracting Localized Yes/No Values from Dynamics CRM

As I am working on an update of my SnapShot! documentation tool, I realized that most people understand the values of Yes and No better than True or False, so I modified the output so that I replaced the word True with Yes and False with No.

That works great and I think it actually makes the report much more readable.

But, what if Yes and No works fine for English-speaking countries, but what if English is not your language?  I considered that a problem that I could solve.

So, into the internals of Dynamics CRM I dive and I came up with what I think is a fairly elegant solution.

Unfortunately, there does not seem to be a single location where Yes and No values are stored.  BUT, there are fields that contain those values.

After looking at the database metadata, I decided to retrieve the optionset values from the Account field Credit On Hold.

I store those values and later use them to generate my documentation.

Here’s how it works:

First, I created a class to hold the values so I can easily pass the data back and forth.

public class BooleanLabelSet
   {
       public string TrueLabel { get; set; }
       public string FalseLabel { get; set; }
   }

 

Next, I created a method to extract the Attribute information:

public BooleanLabelSet GetBooleanLabels(OrganizationService service)
{
    var request = new RetrieveAttributeRequest
    {
        EntityLogicalName = Account.EntityLogicalName,
        LogicalName = "creditonhold",
        RetrieveAsIfPublished = true
    };

    var response = (RetrieveAttributeResponse)service.Execute(request);

    var attribute = (BooleanAttributeMetadata)response.AttributeMetadata;

    var trueLabel = attribute.OptionSet.TrueOption.Label.UserLocalizedLabel.Label;
    var falseLabel = attribute.OptionSet.FalseOption.Label.UserLocalizedLabel.Label;

    return new BooleanLabelSet { TrueLabel = trueLabel, FalseLabel = falseLabel };
}

And you use it like this:

var BooleanLabels = GetBooleanLabels(OrganizationServiceMain);
var booleanValue = value ? BooleanLabels.TrueLabel : BooleanLabels.FalseLabel;

If you try this technique and run into issues, please let me know so I can correct the code.

Leave a Reply 1 comment