Tuesday, July 6, 2021

Retrieve link entity attribute value from fetch XML in dynamics 365 using Plugin C# Code

The following example code shows how to get related entities Country and City, City Code using fetch XML link-entity

Note: Here you need to specify the ‘alias’ name which you can keep different than ‘attributename’ and use the same alias name in GetAttributeValue while fetching value. let's go into detail by looking into the below example.

var citycode = countryResult.Entities[0].GetAttributeValue<AliasedValue>("a_citycode").Value.ToString();

Follow the below example to understand more on how to retrieve the link entity attribute value. In this example I am using fetch XML to get the record based on my requirement you can use advanced find to retrieve the fetch XML query as per your requirement. 

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' uitype='new_country' value='{0}' />
                                    </filter>
                                    <link-entity name='new_city' from='new_cityid' to='new_cityid' visible='false' link-type='outer' alias='a_8e4b53ab387aea11a811000d3a8b2b00'>
                                      <attribute name='new_citycode' alias='a_citycode'/>
                                    </link-entity>
                                  </entity>
                                </fetch>";
                fetchXml = string.Format(fetchXml, CountryId);// pass dynamics country id in the first index {0}
                var countryResult = service.RetrieveMultiple(new FetchExpression(fetchXml));
               
                if (countryResult != null && countryResult.Entities.Count > 0)
                {
                    var citycode = countryResult.Entities[0].GetAttributeValue<AliasedValue>("timezonediff").Value.ToString();
                }


Click here to learn more about Plugin dynamics 365

That's all..

Thank you .. keep learning..

No comments:

Post a Comment