Skip to main content

Notifications

Announcements

No record found.

Microsoft Dynamics CRM (Archived)

FormattedValues not working for inactive reocrds

Posted on by

Hi,

after upgrading CRM from 8.2 to v9 I've found a weird issue with a linq query in the c# code.

Working fine with v8.2 but for v9 it only works for Active records, not sure why it fails for Inactive.

StatusReason = r.FormattedValues["statuscode"]

And the error is: The given key was not present in the dictionary    System.Exception System.Collections.Generic.KeyNotFoundException

 

Any help on this?

*This post is locked for comments

  • Suggested answer
    Community Member Profile Picture
    Community Member Microsoft Employee on at
    RE: FormattedValues not working for inactive reocrds

    Hi,

    You could retrieve the attribute metadata in the cases that the attribute is not available as a formatted value

    //Given field is available as an formatted value
    if (r.FormattedValues.contains("statuscode") {
        StatusReason = r.FormattedValues["statuscode"]
    }
    else {
        //Retrive attribute metadata to get the label for statuscode
        var attributeRequest = new RetrieveAttributeRequest
        {
            EntityLogicalName = r.LogicalName,
            LogicalName = "statuscode",
            RetrieveAsIfPublished = true
        };
    
        var attributeResponse = (RetrieveAttributeResponse)service.Execute(attributeRequest);
        var attributeMetadata = (EnumAttributeMetadata)attributeResponse.AttributeMetadata != null ? (EnumAttributeMetadata)attributeResponse.AttributeMetadata : null;
    //This is only an example of how to handle the metadata retrieved for OptionSets List<KeyValuePair<string, int?>> optionMetadataList = new List<KeyValuePair<string, int?>>(); foreach (OptionMetadata option in attributeMetadata.OptionSet.Options) { optionMetadataList.Add(new KeyValuePair<string, int?>(option.Label.UserLocalizedLabel.Label, option.Value)); } //Do something with the metadata retrieved }


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!

Tips for Writing Effective Suggested Answers

Best practices for providing successful forum answers ✍️

Leaderboard

#1
André Arnaud de Calavon Profile Picture

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

#2
Martin Dráb Profile Picture

Martin Dráb 230,188 Most Valuable Professional

#3
nmaenpaa Profile Picture

nmaenpaa 101,156

Leaderboard

Featured topics

Product updates

Dynamics 365 release plans