Wednesday, 23 April 2014

Modifying the "New Email on Case" Template in Salesforce

If you are using Email-to-Case on Salesforce, there is a super handy standard feature that will send an e-mail out to the case owner when a new incoming e-mail is filed against one of their cases.

This is an option turned on via Setup > Customize > Cases > Email-to-Case and then checking the box "Notify Case Owners on New Emails.


Then, when an incoming e-mail is detected by your Email-to-Case config and associated with a Case, the owning user will get an e-mail that reads:

An email has been received for case 00003630: Case system test.

Please click the link below to review the new email and respond appropriately.

[https://na13.salesforce.com/02sf00000000000]

NOTE: It is not recommended that you forward this email because its association with the original case may be lost.

and then the body of the email from the customer..

The requirement that led me to end up writing this was to replace the link in the middle of the e-mail for a client, but I can imagine a whole host of reasons this message might want customising in client systems.. but, if you are setting out on that road, like me, you will probably find quite quickly it is not a standard or custom e-mail template that you can edit. It is fixed, and can only be turned on; and off.

So what do you have to do if you want to edit it? You have to turn it off and replicate the functionality.

Turning it off can be done by following the navigation steps above and then de-selecting the checkbox. Now, when a new e-mail enters a case via Email-to-Case, there will be no notification. Now, you need to build a workflow rule to replicate the functionality.

That is not as simple as it sounds though. Here are the steps we will have to go through:

  • Create a workflow rule on Email Message that will be triggered when a new incoming case e-mail is inserted
  • Because Email Message workflows cannot dispatch Email Messages themselves, we will have to do a field update instead, which we perform on the parent Case
  • Add a workflow rule to the Case object to send the owner an e-mail when it is updated by the previous workflow
First we need the field on Case to update; lets make this field a Date field, as it is something we can set with a formula value quite easily and it gives us an audit value that maybe just one day might be useful to someone!

Access your case fields via Setup > Customize > Cases & Fields and click to create a new field. Call the field "Last Emailed" so the API name is Last_Emailed__c and set it's type to "Date/Time"- make it not required and with no default. I would leave field permissions at the defaults, but then remove it from all page layouts (for the moment at least).

Now we have our field, we need to cause it's update, and then effect it's action to dispatch an e-mail. First let's set it, and we are going to do this when a new Email Message is received on a case.

Click through Setup > Create > Workflow & Approvals > Workflow Rules and click to create a new rule.

On the first page, select "Email Message" from the object drop down (this may only be there if Email-to-Case is enabled). Call it something like "Custom Notification Email" and have it fire whenever a record is created. Set your Rule Criteria to be a Formula, and for the formula enter "true" so every message triggers this Workflow.

On the next page of the Workflow, click to add a New Workflow Action and then select New Field Update and populate it as such so that it will update the case field we have just created to the value of the Formula method "NOW()":


Once this is done, and you are presented with the Workflow, here is a catchy step. Click to Edit the field update you have just created, select the check box to "Re-evaluate Workflow Rules afer Field Change" and save it.

Edit: July 2015

Thank you readers for your comments that this stage is no longer possible! I recreated the steps and lo-and-behold, you cannot mark this field update to re-evaluate. How weird.
 
Therefore, we need to find an alternative way to send the e-mail based on this first workflow evaluating. Well seeing as it's now 2015, why not use Process Builder! Process Builder comes later in the order of execution than Workflow, so I can't see why you couldn't create a process based on the same conditions/premise as this post - that sent an e-mail when the Case "Last_Emailed__c" field was updated.

I don't have time right now to test this sorry :( but if anyone has any luck with that route, please let me know in the comments. Just replace the word "Workflow" with "Process builder" for the rest of this article, and the complicated screen shots for the lovely new screenshots that a Process might have!

Resume original post:

Finally, complete the Workflow setup and remember to activate it! This is the most commonly made workflow mistake is forgetting to activate! We now have the second stage of our process complete.

Before we can make our final Workflow rule to dispatch a custom e-mail, we better make sure we have our custom e-mail template ready to use. Click through Setup > Communication Templates > Email Templates and make a new template (the details of the process for which I will not cover here as it is well documented).

Now we can make that final Workflow rule. Go back to Setup > Create > Workflow & Approvals > Workflow Rules and click to create another new rule. This time, we are making it on "Case".

Give you rule a name, and select "created, and every time it's edited" as your evaluation criteria. This time, the rule criteria is important, as we don't want to send an e-mail everytime any change is made to a case, therefore, select the formula criteria entry again, and this time enter

ISCHANGED(Last_Emailed__c)


This means the Workflow will only fire if the "Last_Emailed__c" field has changed from it's previous value (i.e. the first workflow rule caused it).

Once we have this, get into your new Workflow rule, add an Email alert, choose your custom template (this is the point you might want to set off your success fireworks) and pick the "Owner" as the recipient. Fiddle with any other settings you like, then save your Workflow, activate it, and sit back and watch as new incoming e-mails cause the dispatch of a custom notification email to the case owner to inform them of this message.



9 comments:

  1. Great article, I love this functionality in Salesforce.

    ReplyDelete
  2. Blog very attractive.Congratulations! valuable post it was really useful.

    ReplyDelete
  3. Nice article. I think you should check if the EmailMessage is incoming or not. This might fire for emails that are outbound.

    ReplyDelete
    Replies
    1. That's a really good point, and I'm sure I remember it being discussed when we were developing this solution.. but I'm not sure of the outcome, looking back over it now, it definitely looks like there would be some complication surrounding the creation of outbound e-mails. Adding some entry criteria to the first workflow would probably provide a hard-and-fast solution to avoid this. Not sure how/why we are "getting away with it" at the moment though, might be worth investigating if I have a quiet afternoon one day!

      Delete
  4. I tried using your logic to send an email to case related users when a case comment is added. I added the new field on the case but updated it from a workflow rule on the Case Comment object. However, when selecting "Re-evaluate Workflow Rules after Field Change" I get a message that says:

    "Re-evaluate Workflow Rules after Field Change cannot be specified for a parent field update"

    Any way around that? Thanks!

    ReplyDelete
    Replies
    1. I have same problem, got same error, cannot check "re-evaluate"

      Delete
    2. Hi Guys... thanks for your comments. I have reviewed this post now, and my best suggestion is to not set this check box (which you can't!) and instead use a Process Builder process to send the e-mail from the case object update. The firing criteria of the Last_Emailed__c field being updated stands true, just add an e-mail alert to the process instead of doing a workflow. Hope that helps.

      Delete
  5. Me too - I have same problem, got same error, cannot check "re-evaluate"

    ReplyDelete
  6. Hi, Thank you for your comment, it does seem that this is now the case, I have just replicated the scenario. I have added an update (in yellow) in the middle of the post, in which I would now suggest doing the second "action" with a Process instead of workflow? Hopefully this helps. Please let me know if so and/or you need any other specific help (though I am no process expert at the moment!)

    ReplyDelete