The Dynamics 365 and Common Data Service connectors in Microsoft Flow and Logic Apps do not return formatted values when using the Get Record
and List Records
actions. This is a real pain if you need to use formatted values.
It seems that the Dynamics 365 and Common Data Service actions do not implement a Prefer
header that contains the odata.include-annotations="OData.Community.Display.V1.FormattedValue"
value required by the Web API to return formatted values.
There is a work around, however.
UPDATE: This work around is now redundant. Formatted values are available when using the CDS (Current Environment) connector by means of the new_myfield@OData.Community.Display.V1.FormattedValue
property. Thanks to Linz Zaw Win for pointing this out. See his blog for more details: https://linnzawwin.blogspot.com/2020/07/get-lookup-display-name-and-option-set.html
HTTP with Azure AD
Fortunately, we can use the Invoke an HTTP request
action that is part of the HTTP with Azure AD
connector. This connector and action are awesome because it allows us to send HTTP requests to Dynamics 365 or Common Data Service without having to worry about the authentication. No Azure App registrations! No bearer tokens!
Add a new action to your Flow or Logic App. Search for HTTP with Azure AD
and select the Invoke an HTTP request
action.
Enter the base URL of your Dynamics 365 or Common Data Service instance into the Base Resource URL and Azure AD Resource URI (Application ID URI) fields. For example: https://orgname.crm.dynamics.com
. Next, click Sign in.
Set the Method to GET
and enter the Url of the request field, enter rest of the request URL along with the OData query parameters you would have used in the traditional Dynamics 365 and Common Data Service actions.
Now the important part! Add a new header with a key of Prefer
and a value of odata.include-annotations="OData.Community.Display.V1.FormattedValue"
. This will tell the web API to include formatted values in the response.
Save your Flow or Logic App and then trigger it. If you inspect the Invoke an HTTP request
action of a successful run, you will notice that the formatted values are now included in the response body JSON.
Bonus tips
If you need to iterate through the response values you can use body('Invoke_an_HTTP_request')?['value']
on an Apply to each
action.
Invoke_an_HTTP_request
is the default name of the action we created. If you changed it’s name then you will need you will need to change it in the expression too.
To get the formatted value from one of the values you are iterating over you can use items('Apply_to_each')?['fieldname@OData.Community.Display.V1.FormattedValue']
.
Vote for OData annotations to be added to Flow
If you’d like to see OData annotations added to Flow and Logic Apps please consider voting for it on the Flow Ideas section of the Microsoft Flow Community site. https://powerusers.microsoft.com/t5/Flow-Ideas/Dynamics-365-Header-Include-OData-Annotations/idi-p/126183.