Email Record Links from a Dynamics CRM Workflow

It’s easy enough to email another CRM user a link to a record. If you open up a form for almost any record type in Dynamics CRM, you can pull down the Actions menu and select the Send Shortcut command to do this. Dynamics CRM will open up your mail client, and insert a rather complicated looking link which the recipient will be able to click to navigate directly to the record (assuming they have read permissions!)

For example, here’s what it looks like from an opportunity record:

Here’s what it looks like for an account record:

While your specific links will be different than mine (mine are for records contained in my CRM Online database, and the nasty-looking 25-character GUID uniquely identifies CRM records so it better be different!), the general structure will always be the same. I’ll come back to that point a little later, since it will solve a problem for us.

Problem: a Workflow-generated email cannot send a record link

So…while you can use the menu command I just mentioned to manually email a link to a record, you cannot email a link to a record from within a workflow. This might sound somewhat obscure, but it actually comes up a lot, and when you run into it, it just seems like something you should be able to do! Fortunately, there’s a relatively easy fix you can implement, with just a pinch of customization and a smidgeon of script. I’ll show you the solution next, but first, let me illustrate the problem in a little more detail.

Suppose I want a workflow to automatically send an email alert any time something important changes about an opportunity record. Here are a few scenarios to illustrate:

  • If the pipeline stage of an opportunity record changes, send an email alerting the sales manager.
  • If the current date is with three days of an open opportunity’s estimated close date, send a reminder email to the opportunity owner.
  • If an opportunity is closed as “Lost”, send an email alert to the owner’s manager.

You can come up with as many scenarios as there are organizations in the world, but in all of them, it would be nice to include a simple clickable link in the body of the email, so the recipient of the alert can navigate directly to the record in question.

As you know if you’ve read my book on Dynamics CRM workflows, there are many things you can do with Dynamics CRM workflows…but unfortunately, this is not one of them! I’ll illustrate with a simple alert email, sent by a workflow that runs automatically any time specified values of the opportunity entity change.

The following screen shot shows the Set Properties form for the workflow’s Send e-mail action. For demo purposes, the email simply goes to me, and I’ve used Dynamic Values to populate the body of the email with presumably interesting information about the current opportunity.

There is an eponymously titled field you can insert for any entity in Dynamics CRM, in this case, using the “{field name{entity name}}” characteristic of the workflow design environment, it’s the {Opportunity{Opportunity}} you can see in the figure. The problem with that is that it’s only a clickable link to the record if it’s on the Regarding field…and the Regarding field only appears if you happen to be viewing the email activity within Dynamics CRM.

Here’s what an email sent by this workflow looks like in Dynamics CRM (e.g., as a History item associated with the opportunity record):

But if, like most people as you prefer consuming your email in Outlook or a non-CRM email client, you never see that Regarding field. For example, Outlook:

Solution: A Pinch of Customization and a Smidgeon of Script (and URL Addressable Forms)

If you’ve read this far, I’m grateful, and you may have noticed the “Link to record” field that’s appeared in a couple of screen shots. That’s the solution, and it uses a technique called URL Addressable Forms, which simply means that every form in Dynamics CRM can be accessed via a unique URL, consisting of an entity-specific prefix combined with a suffix unique to a specific record. You can use the Send Shortcut command I started out by discussing to see what the prefix is for some common record types:

Common Record Types

Record Type Edit Form Prefix
Account https://imginc.crm.dynamics.com/sfa/accts/edit.aspx
Contact https://imginc.crm.dynamics.com/sfa/conts/edit.aspx
Opportunity https://imginc.crm.dynamics.com/sfa/opps/edit.aspx
Case https://imginc.crm.dynamics.com/cs/cases/edit.aspx
Marketing Campaigns https://imginc.crm.dynamics.com/ma/camps/edit.aspx

 

You can use a custom attribute for any entity you want to include a clickable link for by following these steps:

  1. Customize the entity in question (e.g., Opportunity) by adding a new attribute. I added a custom attribute to the Opportunity entity, called it “Record Link” (it has a corresponding schema name), and gave it the following properties:

     

    The most important is to make it a Type of “nvarchar”, since that’s the only type that has the clickable link format of “URL”. Make sure it’s long enough. I made mine 200, but you can always come back and increase the maximum length (unlike the type and format values, which can’t be changed once the custom attribute is created.)

 

  1. Once the attribute is created, the question is how to put the value into it. This really is just a smidgeon of script code – one line of Jscript you can put in the Opportunity form’s OnSave event:

     

    crmForm.all.img_recordlink.DataValue = ‘https://imginc.crm.dynamics.com/sfa/opps/edit.aspx?id=’+crmForm.ObjectId;

That’s pretty much it. If you implement this you’ll notice that since I wrote this code for the form’s save event, the new “Record Link” field won’t contain anything until a record’s form has been saved at least once after you’ve saved and published theses customizations. Too bad there’s not a built-in Record Link attribute for every Dynamics CRM entity. I guess I should add that to my top X list of new features that should be included in Dynamic CRM 5!

I wrote a book on Dynamics CRM workflows, by the way, and it contains tons of examples like this one and other useful workflows. You can purchase the book on Lulu.com or on Amazon, and if you do, you can also get a (free) subscription to the online version of the book, where you can download the workflows themselves, customizations, and related content.

Here’s a link you can visit to find out more about my book and purchase it:

13 Comments »

  1. Benoit Kreins Said,

    October 1, 2009 @ 3:36 am

    Hi Richard,

    This is a great tip!! Do you have a solution for updating the field img_recordlink for all the existing records? It is not possible to open the thousands of accounts for triggering the javascript.

  2. Richard Knudson Said,

    October 1, 2009 @ 8:48 am

    Thanks Benoit — I’m glad you found it useful!

    There are two problems with the approach I suggested (nothing’s perfect, right?) The first one is that the first time the record is saved, the ObjectId doesn’t yet have a value, so nothing is saved in that field. I believe it has a “null” value until AFTER the save event happens, so it won’t be there until the save event runs a second time. There’s probably an easy fix for this, but I haven’t done anything with it yet.

    The second issue is the one you raised…and I don’t believe that’s possible without writing a plug-in or a custom workflow activity. I’ll keep my eyes open and post a note if I can find anything. If you find something in the meantime, please let me know.

    Thanks for your comment!

    Richard

  3. Rob Said,

    October 7, 2009 @ 2:20 pm

    I used cases (in my deployment, they are tickets) and it worked like a charm, and I’m a novice, AT BEST, when it comes to copying and pasting code.

    However I’ve tried to do the same thing to custom entities and am running into a snag. It’s generating the URL for the recordlink attribute, but I’m getting a “the resource cannot be found” error when I try to open it.

    Are there any known issues with this technique and custom entities? Anything I should be looking for?

  4. Richard Knudson Said,

    October 8, 2009 @ 9:39 am

    Hi Rob!

    Hmmm…I wouldn’t think it would make any difference if the entity’s custom. I’ll try it w/ custom when I get a chance, but in the meantime, if you want to show me what your link looks like I can tell from looking if it’s formed correctly or if there are any obvious reasons it wouldn’t work.

    Cheers — Richard

  5. Ersin Öztürk Said,

    October 25, 2009 @ 9:52 am

    Hi Richard,

    Thanks for your tip, for problem no 1, we have written some onSave and OnLoad codes, which are.

    //OnLoad Event
    var fullURL = parent.document.URL;
    var idControl = crmForm.ObjectId;

    if (idControl != null && crmForm.all.new_issaved.DataValue == 0)
    {
    crmForm.all.new_projetalepgeribildirimurl.DataValue = fullURL;
    crmForm.all.new_projetalepgeribildirimurl.ForceSubmit= true;
    crmForm.all.new_issaved.DataValue = true;
    crmForm.all.new_issaved.ForceSubmit = true;
    crmForm.Save();
    }

    // OnSave Event
    if(crmForm.FormType == 1 && event.Mode == 2)
    {
    event.returnValue = false;
    crmForm.Save();
    }

    Basicly saying,
    Ww chek if objectId is null or not, on onLoad event, And while user clicks “Save and Close” we only save form.

    So after save we get objectId and write it on form to use in workflows.

    Ersin Öztürk
    http://ersinozturk.blogspot.com/

  6. Oliver Said,

    October 27, 2009 @ 3:32 am

    Curious – I take it this won’t write the recordlink in when the record is created as the object ID doesn’t exist until after the save is completed?

  7. Oliver Said,

    October 27, 2009 @ 3:54 am

    Oops, I see you answered this already – never mind.

  8. Lindsay Said,

    November 13, 2009 @ 4:45 pm

    Hi Richard,

    Thanks for all the work you put in to the trick bag, I’ve grabbed some really good stuff here.
    I’d love to use this record link idea but adding it to my Contact form’s OnSave kills the many events I have (otherwise working well) in OnLoad. Any thoughts on why that would happen?

    Could similar code be added to OnLoad?
    Troubleshooting Jscript in CRM makes me want to poke my own eyes out.

    - Lindsay.

  9. Richard Knudson Said,

    November 14, 2009 @ 7:57 am

    Hi Lindsay — I’m glad you like my content!

    The version I posted was incomplete in that it didn’t work right the very first time a record is saved. Check out the code Erszin posted in his comment (above). My guess is that that might fix it, although I haven’t tested it. Anyway, if you have time to give it a shot, let me know; if I get to it I’ll post up on my results.

    Cheers — Richard

  10. Richard Knudson Said,

    November 14, 2009 @ 7:58 am

    And btw, don’t poke your eyes out. Editing the jscript in Visual Studio is much better option!

  11. Yvan Said,

    January 24, 2010 @ 8:03 am

    You should try this instead

    http://crmaccelerators.codeplex.com/Release/ProjectReleases.aspx?ReleaseId=26691

    Perfect solution!

  12. Richard Knudson Said,

    January 24, 2010 @ 7:48 pm

    Hi Yvan,

    I have a hard time keeping up with the accelerators, and I definitely missed that one! Thanks for pointing it out. It’s not quite perfect, though, since like all the Accelerators it only works with the on-premise deployment option, and my organization like many others runs Dynamics CRM Online.

    Thanks for reading!

    Richard

  13. Albarada Said,

    August 6, 2010 @ 2:08 am

    Hey Richard,
    i want to open a url in a web browser via workflow.
    anyone have an idea?

Leave a Comment