Embedding a Direct Link In An E-Mail - CRM 4.0 Version
This is a long overdue post that is a follow-up to 2 other posts I had. Recently I published Take 2 of a blog post that walks through setting up a CRM 3.0 workflow to build a URL and save it with the record so it can be used in a workflow e-mail. I had promised to post a similar version of this that works in Microsoft CRM 4.0, so here it is. There are a couple of changes though, in place of a custom .NET workflow assembly I am going to leverage the power of plug-ins to accomplish the same task.
There are many benefits to using a plug-in vs a worklfow to accomplish this in 4.0. They are:
1. In a PreCreate event we can dictate what information is actually saved. This allows us to add additional properties to the EntityXML, like a URL or GUID, before the record is saved. This stops us from having to update the entity after it's created.
2. Thanks to the following post from Will Wilson, we can generate a GUID, assign it to our entity, and then immediately use it within our Plug-In. I highly suggest reading this post and implementing this in any future plug-ins where you may need the GUID of the record. It is an excellent post and works like a charm.
3. It can run in the the CRM Outlook Client. You can create this plug-in so that it runs when user's create records Offline as well.
4. Saves unnecessary steps and workflow processes
Let's get started.
Step 1 - Create A Custom Attribute To Hold The URL
For this example we'll just call our attribute: new_urllink with datatype var char
Step 2 – Create a new .cs file for your Plug-In Using Visual Studio 2005
namespace MicrosoftCRM.Examples.PlugIns
{
public class PreCreateIncidentPlugIn : IPlugin
{
public void Execute(IpluginExecutionContext context)
{
if((context.InputParameters.Properties.Contains(ParameterName.Target) && (context.InputParameters[ParameterName] is DynamicEntity))
{
DynamicEntity _dentity = (DynamicEntity)context.InputParameters[ParameterName.Target];
switch(context.MessageName)
{
case MessageName.Create
GenerateGuid(ref _dentity);
BuildUrl(ref _dentity);
context.InputParameters[ParameterName.Target] = _dentity;
break;
case MessageName.Update
break;
case MessageName.Delete
break;
}
}
}
public void BuildURL(ref DynamicEntity _dentity)
{
Key _keyproperty = (Key)_dentity[_dentity.Name + “id”];
string url = “http://crm/cs/cases/edit.aspx?Id=” + _keyproperty.Value.toString();
StringProperty _url = new StringProperty(“new_urllink”,url);
_dentity.Properties.Add[_url];
}
public void GenerateGuid(ref DynamicEntity _dentity)
{
//Generates a GUID to assign to the entity
Guid _incidentguid = Guid.NewGuid();
Key _incidentkey = new Key(_incidentguid);
KeyProperty _keyprop = new KeyProperty(_dentity.Name + “id”,_incidentkey);
_dentity.Properties.Add(_keyprop);
}
}
}
Step 3 – Deploy Code and Publish the Plug-In
Now it's time to deploy the Plug-In to CRM. Stop the CRM Workflow Service and IIS. Copy the .dll to <install_drive>\Program Files\Microsoft CRM\Server\Bin\Assembly. Restart IIS & the CRM workflow service then start the Plug-In Registration Tool. Once the plug-in has been registerd the next step is to register the PreCreate message for the Incident entity. Once that is done you are ready to go.
Step 4 - Create The Associated Workflow(s)
Now all you need to do is create a workflow that will send the e-mail. In the body of the e-mail or e-mail template add the Url field to the body of the e-mail.
Step 5 - Your Done!
A List of Common Url's For CRM Entities:
You can find the paths for CRM Entities rather easily by browsing to the home directory of the CRM Website, if it's installed on the default website the path would be <drive>\inetpub\wwwroot. Once you have access to the CRM Website files find the folders that contain the edit pages for the various entities:
· SFA - Sales Force Automation (accounts,contacts etc)
· CS - Customer Service (cases,contracts etc)
· MA - Marketing Automation (campaigns, marketing lists etc)
Sales Force Automation:
Accounts: http://<crm-url>/sfa/accts/edit.aspx
Contacts: http://<crm-url>/sfa/conts/edit.aspx
Leads: http://<crm-url>/sfa/leads/edit.aspx
Opportunities: http://<crm-url>/sfa/opps/edit.aspx
Marketing Automation
Campaigns: http://<crm-url>/ma/camps/edit.aspx
Marketing Lists: http://<crm-url>/ma/lists/edit.aspx
Campaign Response: http://<crm-url>/ma/campaignresponse/edit.aspx
Case Management:
Cases: http://<crm-url>/cs/cases/edit.aspx
Contracts: http://<crm-url>/cs/contracts/edit.aspx
KB Articles: http://<crm-url>cs/articles/edit.aspx
Custom Entities:
Custom: http://<crm-url>/userdefined/edit.aspx
*Please substitute the actual url of the CRM Implementation you are working with where you see <crm-url> in the links above.
- Jeremy
** This posting is provided "AS IS" with no warranties, and confers no rights.