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.

 

 

Published 09-21-2008 10:03 AM by Jeremy Winchell

Comments

# re: Embedding a Direct Link In An E-Mail - CRM 4.0 Version

Thursday, October 16, 2008 4:30 PM by Kyle

Great Article! There are many people that want to put entity urls in workflow emails and this is, as far as I can tell, the absolute best way to do it.

It works great for the case entity which I deployed it on.

Do you know if any modifications are needed to make it work with activity entities such as emails and tasks? I tried to amend the code to work with an email entity but I haven't been able to get it to work. I am not sure if I am just doing something wrong, or if a modification is needed.

Thanks,

Kyle (kyle at advantage4u dot ca)

# re: Embedding a Direct Link In An E-Mail - CRM 4.0 Version

Monday, October 20, 2008 11:42 PM by Kyle

Any idea if this works with activity entities (such as tasks, emails, etc...) I tried but wasn't successful - I might just have made a stupid mistake though.

Thoughts?

# re: Embedding a Direct Link In An E-Mail - CRM 4.0 Version

Wednesday, October 22, 2008 7:20 PM by Jeremy Winchell

Kyle,

I wasn't getting notified of new comments so I didn't see your posts until tonight.  It should work with activity entities as well, it may just be a matter of getting the correct format of the URL.

I will look into it.

Jeremy

# re: Embedding a Direct Link In An E-Mail - CRM 4.0 Version

Thursday, November 06, 2008 12:00 AM by Kyle

Any luck? I will try it again later this week to see if I can get it going with activities.

# re: Embedding a Direct Link In An E-Mail - CRM 4.0 Version

Wednesday, November 12, 2008 4:09 AM by JokiJani

I got this working with Contact entity. Thank You for that. But I also have problem with activity entities. I tryed with emails. When I create a new Email activity, the Url Link is formed well, but saving email fails with "No attribute" error. So the Plug-in works fine but somehow Activity entities works differently than Contacts. Any ideas for that?

# re: Embedding a Direct Link In An E-Mail - CRM 4.0 Version

Thursday, November 13, 2008 6:42 AM by JokiJani

I solved the problem with activity entities. Problem is here:

1. GenerateGuid -method.

Your code:

KeyProperty _keyprop = new KeyProperty(_dentity.Name + "id",_incidentkey);

- Above is not working with emails (or other activities) because it generates KeyProperty named emailid. In emails (and other activities) KeyProperty shoud be activityid. I just hardcoded it as shown below, but some dynamic system need to be build in future.

My code:

KeyProperty _keyprop = new KeyProperty("activityid", _incidentkey);

2. Same thing must be changed in BuildURL -method.

Your code:

Key _keyproperty = (Key)_dentity[_dentity.Name + "id"];

My code:

Key _keyproperty = (Key)_dentity["activityid"];

# re: Embedding a Direct Link In An E-Mail - CRM 4.0 Version

Thursday, November 13, 2008 7:04 PM by Jeremy Winchell

JokiJani,

In the generate GUID method you can check to see what the entity type is.  Once you do that if it's an email, phonecall, appointment, campaignresponse etc., you can change the code to use "activityid" as the property name instead of <entityname> + "id".  

You can do the same kind of check in the BuildURL function as well.

Nice find.

- Jeremy

# re: Embedding a Direct Link In An E-Mail - CRM 4.0 Version

Thursday, November 26, 2009 5:05 AM by mingting_afu

Hi~ Jeremy~

Your code is really a big help to me~

but i have a question,

we use this code to generate an ID

"Guid _incidentguid = Guid.NewGuid();"

but the format of the generated ID is quite different from the ones generated by the CRM.

Ex.

this is the sample incidentid after i registered this plugin:

 AA7B3099-C41E-4EC0-9B12-E34A9E1E17DD

 1E633057-7F4C-4DEE-A14D-34A8B63DBEA9

and the following is the sample incidentid generated by CRM before I registered the plugin.

 593C3C22-37DA-DE11-A011-00089BB85CE6

 44992EB5-59DA-DE11-9542-00089BB85CE6

it has the format that the ID ends with "00089BB85CE6",

is it fine to have random id in the CRM system?

Thank you very much!

# re: Embedding a Direct Link In An E-Mail - CRM 4.0 Version

Thursday, November 26, 2009 7:56 PM by mingting_afu

Hi Jeremy,

Your code is really a big help to me..

but i have a question here, since we generate a GUID and assign it to our entity. The format of the GUID is different with the GUID generated by the CRM itself.

please see the example below:

 593C3C22-37DA-DE11-A011-00089BB85CE6

 44992EB5-59DA-DE11-9542-00089BB85CE6

 1E633057-7F4C-4DEE-A14D-34A8B63DBEA9

 E3C345DE-BFF1-4CE9-BBD9-B29289FCD45F

the first 2 GUID are generated by CRM System, it has the format ended with "00089BB85CE6", and the last 2 GUID are the GUID we create and assign to our entity, it seems that there is no format for it.

I am concerning is it fine for the CRM system to have random GUID not following its format?

Thank you for your support! ^^

# re: Embedding a Direct Link In An E-Mail - CRM 4.0 Version

Thursday, November 26, 2009 8:10 PM by mingting_afu

Hi Jeremy,

Your code is really a big help to me..

but i have a question here, since we generate a GUID and assign it to our entity. The format of the GUID is different with the GUID generated by the CRM itself.

please see the example below:

 593C3C22-37DA-DE11-A011-00089BB85CE6

 44992EB5-59DA-DE11-9542-00089BB85CE6

 1E633057-7F4C-4DEE-A14D-34A8B63DBEA9

 E3C345DE-BFF1-4CE9-BBD9-B29289FCD45F

the first 2 GUID are generated by CRM System, it has the format ended with "00089BB85CE6", and the last 2 GUID are the GUID we create and assign to our entity, it seems that there is no format for it.

I am concerning is it fine for the CRM system to have random GUID not following its format?

Thank you for your support! ^^

# re: Embedding a Direct Link In An E-Mail - CRM 4.0 Version

Monday, November 30, 2009 2:34 PM by Jeremy Winchell

I have not run into an issue yet where the GUID that is assigned by the Guid.NewGuid() causes any type of issues.  It is using the CRM SDK to generate the GUID rather than using some other method so everything should be fine.