In an earlier blog (E-mail to Case/Lead Using CRM 4 Workflow ), I described a sample workflow that could be used to automatically create cases based on emails sent to a queue.
One of the questions that was frequently asked for the above blog was that the email activity’s regarding field set to blank. In this blog, I’ll show how this can be done using a custom CRM workflow activity.
Let’s summarize the problem we are trying to solve. Joe (email@example.com) is a customer in your system and say you have an account that is capturing this. Joe sends an email to your support alias to report an issue. We would like the email activity that is created to be regarding Joe’s account.
For those of you new to writing custom workflow activities, I highly recommend that you take a look at the SDK documentation at: http://msdn.microsoft.com/en-us/library/cc151142.aspx –It contains all the information needed for you to start using custom workflow activities.
I’m going to split my solution into 2 parts:
1) A custom workflow activity that takes an email address as input and returns a matching account as output
2) A workflow rule that uses the above custom activity. This workflow rule is triggered when an email is created and updates the email activity to have the regarding field set to the output returned by the custom workflow activity.
Custom Workflow Activity
Let’s walk thru’ the code for the custom workflow activity.
1) Define the class skeleton and the using statements for the various namespaces that we will need. Please ensure you have read and understood the SDK documentation at: http://msdn.microsoft.com/en-us/library/cc151142.aspx to get an overview of how to develop CRM custom workflow activities.
[CrmWorkflowActivity("Find Customer with specified email address")]
public class MatchSenderWithExistingCustomerActivity:
// Activity code goes here.
2) Now, add properties that represent the inputs and outputs. As discussed earlier, the input is an email address (of type string) and the output will be the matching account (of type Lookup).
DependencyProperty is a Windows Workflow concept and you can read more about it here: http://msdn.microsoft.com/en-us/library/system.workflow.componentmodel.dependencyproperty.aspx
3) This custom workflow activity needs to be able to retrieve accounts whose email address matches the sender property value. To do this, it needs to be able to call into CRM SDK methods and can do this thru’ IcrmService interface that is provided to all crm custom workflow activities. The following is code for a helper method that uses CRM query functionality to return account that matches the email address.
4) Now, we can implement the Execute method of the custom workflow activity.
Using The Custom Workflow Activity
In order to use MatchSenderWithExistingCustomerActivity in workflow rules, you need to register the activity. Follow the steps at: http://msdn.microsoft.com/en-us/library/cc151144.aspx to accomplish this.
We will create a workflow rule that does the following:
1) When an email is created, it checks if the email’s regarding field is set.
2) If not set, it invokes MatchSenderWithExistingCustomerActivity
3) It updates email with the accountid returned by MatchSenderWithExistingCustomerActivity.
Let’s walk through these in more detail.
1) Create the workflow rule, call it SetEmailRegarding and set it to trigger on email create
2) Insert a check condition step and configure it to check if Email:regarding field does not contain data
3) If Email:regarding is not set, we want to invoke the MatchSenderWithExistingCustomerActivity. Under the add step drop-down, you should find the custom workflow activity as shown below:
4) After you select MatchSenderWithExistingCustomerActivity, you need to ensure that it is being invoked with the correct input. Click ‘Set Properties’ and set the value of the input parameter Sender to that of Email:Sender
5) The next step would be to update the email’s regarding to the value returned by the custom workflow activity in the previous step. To do this, add a Update Email step, select ‘Set Properties’ and tab to the ‘Regarding’ field. In the dynamic values form assistant, you will now find that it shows ‘Match email address:accountid’ to indicate the return value from the previous step. Select this to be the value for regarding field as shown below.
6) Publish the workflow and verify that it is working.
Since the functionality to match email address to an account has been captured as a custom workflow activity, you will be able to use that activity in all your workflow rules. You can also enhance the custom workflow activity by making it return either a matching account or contact if needed. If you need to create an account/contact if a match wasn’t found, you can do this by adding a Create step to the workflow rule.