Friday 27 June 2008

CRM 4. 0 Case link in Email notification

Requirement:

In CRM 4.0, an email with a link to the case should be sent out to the owner of the case,when a case is created/assigned. (one liner requirements are tough to crack ;-))

On the face of it, the requirement sounds simple,but it is not!!

I searched through forums and blog posts and friends out there have suggested different approaches along with a significant list of known issues on how to achieve this!!

One of the approaches that was suggested in a newsgroup @ microsoft.com (you should have a live id to see this post) is as below:




>> "... create a new attribute on the Case form that is populated with the Form's URL when the form is saved. The code for this is implemented in the OnSave event for the Case form as seen below:
if (crmForm.ObjectId !=null)
{
var guid=crmForm.ObjectId;
guid=guid.substring(1,guid.length-1);
crmForm.all.kts_formurl.DataValue = "http://servername/cs/cases/edit.aspx?id="+crmForm.ObjectId;
}

I am using port 80, if you are using any other port, you will need to define it in the URL. If you are using CRM 4.0 you will also need to define the org name in the URL. After populating the field with the URL, my workflow sends an email with that attribute in the body. If you run your workflow when the case is created, it will run before the GUID is generated for your case and before the OnSave event can be fired. This means you will end up with a blank URL everytime. To work around that timing issue, you can include a Wait in your workflow to wait until the URL attribute you've created contains data. I hope this helps!"

The idea is good, I tried the approach above, but I realized that I am not that lucky enough to get things working :-(

Issues with the approach above:
  1. The new attribute will not hold the Case Id when a case is just created. The reason why this value will be empty is because Case Id is generated behind the scenes and the OnSave event handler gets executed before a Case Id is generated!
  2. "OnSave" event handler wouldn’t get fired when you assign case(s) to a user/queue using the “Assign” button on the toolbar!!
The following is the procedure to be followed for the above solution to work:
  1. After creating a new case, user should open the form and click Save, and this is required to assign the Case Id to the new attribute.
  2. To assign a case, user has to open the Case form and change the assignment instead of using the button on the toolbar.
Based on the idea above and some literature survey, I have come up with an approach that just "works" :-) and here it is:

The solution is based on a custom workflow activity (that can be coded using MS Visual Studio 2008 and Windows Workflow Foundation).

Approach:
  1. Add a new attribute to Case – say caseidforemail. The purpose of this extra attribute is to hold the uniqueidentifier that is generated for a new Case.
  2. A custom workflow activity which appears as one of the steps while defining a workflow. This custom workflow activity sets the value on caseidforemail to the case guid (or object id or incidentid, etc)
  3. Define a workflow with two steps:
  • Step 1 – Custom workflow activity (there is nothing much to configure on this step)
  • Step 2 – Send Email. Make sure that the content of the template that is used while sending an email should have a url like this : http://<servername>:<port>/CS/cases/edit.aspx?id={!Case:Case ID for Email;}

That’s it, attach the workflow to the Case as required and you can see the emails with a link to the Case.

Technical details:
  1. Using the Plugin developer tool and Plugin registration tool, we can register our Custom workflow activity with CRM 4.0. I can provide you with the source code of the custom workflow activity as well as the steps on how to register it :-)
  2. Restart IIS where CRM is running.

Here is a very nice article on how to write custom workflow activity with screenshots too and below is the screenshot from my CRM environment:



I hope this post gives a sigh of relief for many "googlers" (those who are tired of searching in Google) out there ;-)

5 comments:

Chris said...

Hi Kiran,

Great post! This is just the project I'm working on now.

Any chance you could share the code to build the assembly?

Thanks!

Chris.

Kiran banda said...
This comment has been removed by the author.
raghav said...

Kiran,

Can you please share your knowledge on devloping a workflow assembly and registering it?

Thanks,

Regards,
Mohit

Kiran banda said...

Hi Raghav,

As I mentioned in my post, here is a very good article that can answer all your questions - http://www.stunnware.com/crm2/topic.aspx?id=CustomWorkflowActivity

Kiran banda said...

Please leave your email id in case you wish to try out my component, I shall email the sources to you at the earliest :-)