This blog deep dives into solving one of the common hurdles faced by Dynamics developer in day to day life. With MS Power Automate undergoing rapid growth and MS recommending to switch to MS Flows instead of using CRM Async Workflows to achieve automation within the system, today we are going to demystify how to insert hyperlink into an email and send the email within CRM without any hard coding.
With Model driven apps, the general URL structure of any CRM record looks like: https://contoso.crm.dynamics.com/main.aspx?appid= cad6712f-2345-6789-1011-121314151617&pagetype=entityrecord&etn=account&id=abcdef01-2345-6789-1011-121314151617
Now in order to construct the URL strictly without hard coding, we need to use some of the environmental variables or retrieve them using List Record CRM action.
1. How do we retrieve “contoso.crm.dynamics.com” dynamically without hardcoding the CRM tenant
Whenever, we use Get Record action for any entity, you might have noticed one of the fields is ODate Id which actually stores the oData record URL of respective entity. We then parse this URL, to retrieve our tenant name.
This is how OData Id looks like:
https://contoso.crm.dynamics.com/api/data/v9.1/accounts(abcdef01-2345-6789-1011-121314151617)
Now we use uriHost() function to retrieve our desired output wherein the input to this function is OData Id.
uriHost(outputs('Get_a_record')?['body/@odata']?['id'])
This will fetch us null or empty output. There is a minor tweak to above expression:
uriHost(outputs('Get_a_record')?['body/@odata.id'])
And the desired output of this function is contoso.crm.dynamics.com
2. How to retrieve appid required for this URL Dynamically
There are multiple ways to overcome this roadblock. One straight forward way is to define an environment variable wherein you give the name of app as well as appid and use fetchXML to retrieve the defaultvalue attribute from environmentvariabledefinition entity to get your appId
Another less invasive method to retrieve appid is using List Records action to query on Model Driven App
entity
In the filter, enter the unique name of your app and the attribute appmoduleid fetches you the GUID of app. Remember to enter top count to 1 thereby fetching only one record
3. Retrieve the account entity GUID Dynamically
This is one simple cake walk step wherein you get the account GUID dynamically from one of the parameters
Now define a string variable and construct the URL by initializing all parameters. This is how it looks like:
CREATE EMAIL RECORD WITHIN DYNAMICS 365/CDS
Now that we have the URL, all we need is to just create a new email record in CRM with appropriate subject/body, activity party list and send it. Select the entity as “Email Messages”
TIP: In my next blog, I am going to show you how to add dynamic email recipients instead of hard coding the values.
Now we have to set the regarding field so that email gets attached to appropriate CRM record. Since this is lookup field, you have to enter both the plural schema name of entity along with GUID of account.
In the email description, we have to embed the hyperlink to URL we have generated above. We can use HTML tags to embed the URL as hyperlink.
<A HREF=”Dynamic Account URL”>Click Here</A></br>
Wherein Account URL is the variable which stores dynamic URL being generated above.
SEND AN EMAIL: PERFORM BOUND ACTION
Now the email record is created in crm, we have to perform the bound action of sending email which is just another simple step.
Now that the whole flow setup is completed, this is how the email generated looks like
WATCH OUT FOR MY NEXT BLOG ON HOW TO SEND AN EMAIL TO DYNAMIC RECIPIENTS USING MS FLOW
Happy CRM Em'Powering
*This post is locked for comments