Read Formatted values using LINQ Queries in Dynamics 365

By | December 13, 2017

Recently we had a business requirement where we came across an issue while retrieving formatted values using LINQ.

In this blog, we will discuss how to resolve this issue.

Suppose, we want to retrieve a revenue with currency symbol in the formatted value and we are using below query:

//Retrieve Account List with Name & Revenue
 var accountList = (from acc in _context.CreateQuery("account")
                      select new
                      {

                accName = acc.GetAttributeValue<string>("name"),

               revenue = acc.FormattedValues.Contains("revenue")? acc.FormattedValues["revenue"]: "No Data"

                                   }).ToList();

 It seems to be correct, but it fails to retrieve the revenue value with a currency symbol.

Instead, we were just getting “No Data” as the value which we have set if no revenue value found.

After some research and play around we found that the FormattedValues are not returned directly as shown above.

For that we needed to change the query to:

//Retrieve Account List with Name & Revenue
   var accountList = (from acc in _context.CreateQuery("account")
                        select new
                      {

                        accName = acc.GetAttributeValue<string>("name"),

              revenueField = acc.Attributes.Contains("revenue") ? acc["revenue"] : null,
         revenue = acc.FormattedValues.Contains("revenue") ? acc.FormattedValues["revenue"] : "No Data"

                                   }).ToList();

And then it worked!

Conclusion:

If we need to retrieve a Formattedvalue using LINQ, then we first need to read the attribute value, and then we may read the FormattedValues later in the query for that attribute.

Microsoft Dynamics CRM and QuickBooks Integration