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

Published 09-21-2008 10:03 AM

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.

 

 

Comments

# Kyle said on Thursday, October 16, 2008 4:30 PM

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)

# Kyle said on Monday, October 20, 2008 11:42 PM

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?

# Jeremy Winchell said on Wednesday, October 22, 2008 7:20 PM

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

# Kyle said on Thursday, November 06, 2008 12:00 AM

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

# JokiJani said on Wednesday, November 12, 2008 4:09 AM

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?

# JokiJani said on Thursday, November 13, 2008 6:42 AM

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"];

# Jeremy Winchell said on Thursday, November 13, 2008 7:04 PM

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

Leave a Comment

(required) 
(required) 
(optional)
(required)