PowerObjects Blog 

for Microsoft Business Applications


CRM 4.0 Links to Entities in Workflow Email

Post Author: Joe D365 |

Many times we would like to include an HTML link in an email notification sent to user. Sometimes we could just use 'Regarding' field of email, but this only works if user uses Outlook Client and even then, email tagging may take a while and 'View Regarding' button is not available when user actually reads the email. In addition, we may want to have one entity as Regarding and another as additional link.

It does not sound very difficult, at least that is what I thought first time I encountered this requirement. I figured I could just use the GUID of the record to give user link in format of:

http://CRM/sfa/accts/edit.aspx?id={GUID}

As it turns out, the GUID is not available for workflows.  With some searching, I found similar issues dating back to CRM 3.0 and some approaches that saved the GUID or the full link to a custom field if the Form is Update Form.

The obvious downside of this is that when a new record is created, it will not have value for the custom field unless  the user opens up the form again after saving it. This was not enough for me, but it was the starting point for my solution:

  • When user initiates save on new record, intercept and cancel that save
  • Force Saving the record in order for GUID be generated, reload form and set the GUID to custom attribute
  • Continue User Initiated save
  • The main challenge is to able to handle any kind of save: Save, Save and New, Save and Close.

Actual Solution

  • Create a custom attribute of type nvarchar to hold the GUID or URL. Add this field in form. In my case, I elected to save only the GUID, in order to have the ability to re-use the GUID for possible other purposes late on. I call the field po_workflowguid.
  • Add following logic to OnSave Event: 
//ONSAVE
//If we already have value in field, skip this logic
if ((crmForm.all.po_workflowguid.DataValue == null )
 ||(crmForm.all.po_workflowguid.DataValue==""))
{
 //Set temporary flag on the GUID field, based on which save was called.
 //This is important so we know which event to continue.
 if (event.Mode==2)
  crmForm.all.po_workflowguid.DataValue="SAVEANDCLOSE";
 else if (event.Mode==59)
  crmForm.all.po_workflowguid.DataValue="SAVEANDNEW";
 else
  crmForm.all.po_workflowguid.DataValue="SAVE";
 //Force Form to save, in order to create GUID and re-load the form.
 crmForm.Save();

 //Abort the original Save
 event.returnValue=false;
 return false;
}
  • Add following logic to Onload Event. 
//ONLOAD
//Lets hide the field
crmForm.all.po_workflowguid_c.style.display='none';
crmForm.all.po_workflowguid_d.style.display='none';
//Only process for Update Form
if(crmForm.FormType == 2)
{
 //If this is Update Form, we can simply set the GUID
 if ((crmForm.all.po_workflowguid.DataValue == null )
   ||(crmForm.all.po_workflowguid.DataValue==""))
  crmForm.all.po_workflowguid.DataValue=crmForm.ObjectId;
 else
 {
  //The Additional logic: Check for flag in GUID field
  //We will set the GUID and continue the type of save user initiated
  switch(crmForm.all.po_workflowguid.DataValue)
  {
   case "SAVEANDCLOSE":
    crmForm.all.po_workflowguid.DataValue=crmForm.ObjectId;
    crmForm.SaveAndClose();
   break
   case "SAVE":
    crmForm.all.po_workflowguid.DataValue=crmForm.ObjectId;
    crmForm.Save();
   break
   case "SAVEANDNEW":
    crmForm.all.po_workflowguid.DataValue=crmForm.ObjectId;
    crmForm.SubmitCrmForm(59, true, true, false);
   break
   default:
   break
  }
 }
}
  • Now you can use the po_workflowguid field to add links to Workflow Emails

Workflowguid Link

The downside of this approach is that when user saves the form for the first time, they will see a brief flicker from form loading twice.

Another way to achieve this functionality would be to write a custom Plugin for retrieving the GUID of entity and just using that in the workflow.

Joe CRM
By Joe D365
Joe D365 is a Microsoft Dynamics 365 superhero who runs on pure Dynamics adrenaline. As the face of PowerObjects, Joe D365’s mission is to reveal innovative ways to use Dynamics 365 and bring the application to more businesses and organizations around the world.

Leave a Reply

Your email address will not be published. Required fields are marked *

5 comments on “CRM 4.0 Links to Entities in Workflow Email”

  1. Hi Sander,

    From the look of the code I'd say its because the GUID isn't known until the record is saved. So you need to save it, get the GUID, then save it again.

    I'm using this code and it working nicely with one small tweak. I changed:
    crmForm.all.po_workflowguid_c.style.display='none'
    crmForm.all.po_workflowguid_d.style.display='none'
    to:
    crmForm.all.po_workflowguid_c.style.visibility='hidden'
    crmForm.all.po_workflowguid_d.style.visibility='hidden'

    Thanks for the post!

    Cameron

  2. Hi Cameron,

    How can I default/set "cc" field to the user's account (email address) using email activity form based on the "onLoad" event?

    Once the form is loaded, and cc field is defaulted to user's account, script must not restrict the user from adding multiple recipients as "cc" on this email form.

    Awaiting your reply.
    Mukesh

  3. I have tried this technique using a plugin to set the value. I am having a problem in the format of the email generated by the workflow. CRM puts the value from the Guid variable into an HTML Span, thus making it unrecognizeable as a link.

    Has anyone seen this problem?

  4. I was having the same problem with CRM4 breaking my links when I tried to add the GUID field with the workflow. One work around that has worked for me is to create a custom field the contained the entire address with the GUID concatenated into the correct place using javascript. Then in the workflow just add the field by itself.

    So something like this snippet on a load:
    if(crmForm.all.new_url.DataValue == null)
    {
    crmForm.all.new_url.DataValue = 'http://servername:5555/leads/edit.aspx?id='
    + crmForm.ObjectId;
    }

PowerObjects Recommends