Wednesday, July 7, 2021

Retrieve label name of optionset value from link-entity fetch XML D365 C# Code

Follow the below process step by step to get the label name from the option set attribute from the linked entity.

Country and city are two related entities. In the city, there is an option set field value available. With help of advanced find, I have generated the fetch XML query based on my requirement. 

Note: you have to use an 'alias' to retrieve the label name as shown below.

To get option set value with alias:

OptionSetVal= ((OptionSetValue)((AliasedValue)enitycollection.Entities[0].Attributes["a_optionsetaliasname"]).Value).Value;

-----------------------------------------------------------------------------------------------------

var fetchXml = @"<fetch version='1.0' output-format='xml-platform' mapping='logical' distinct='false'>
                                  <entity name='new_country'>
                                    <attribute name='new_countryid' />
                                    <attribute name='new_cityid' />
                                    <order attribute='new_cityid' descending='false' />
                                    <filter type='and'>
                                      <condition attribute='new_countryid' operator='eq' value='{0}' />
                                    </filter>
                                    <link-entity name='new_city' from='new_cityid' to='new_cityid' visible='false' link-type='inner'>
                                      <attribute name='new_cityoptionset' alias='a_cityoptionset'/>
                                    </link-entity>
                                  </entity>
                                </fetch>";
                fetchXml = string.Format(fetchXml, CountryId);//countryid index value dynamically
                Result = service.RetrieveMultiple(new FetchExpression(fetchXml));
                if (Result != null && Result.Entities.Count > 0)
                {
                    if (Result.Entities[0].Contains("a_citycode"))
                    {
                        var OptionSetValLabel = stationResult.Entities[0].FormattedValues["a_citycode"];
                    }

                }


Step 1:

 var OptionSetValLabel = stationResult.Entities[0].FormattedValues["a_citycode"];

Step 2:

public static string GetOptionSetValueLabel(string entityName, string fieldName, int optionSetValue, IOrganizationService service)
        {
 
            var attReq = new RetrieveAttributeRequest();
            attReq.EntityLogicalName = entityName;
            attReq.LogicalName = fieldName;
            attReq.RetrieveAsIfPublished = true;
 
            var attResponse = (RetrieveAttributeResponse)service.Execute(attReq);
            var attMetadata = (EnumAttributeMetadata)attResponse.AttributeMetadata;
 
            return attMetadata.OptionSetVal.Options.Where(x => x.Value == optionSetValue).FirstOrDefault().Label.UserLocalizedLabel.Label;
 

        }


Click on the link to learn more about the plugin.


That's all...

Thank you...

Keep Learning.