Friday 9 October 2015

Case Management: Notify Case Owner on creation of a Case comment via Apex

If you have worked with Salesforce, you will acknowledge that irrespective of the application you are building or the solution you are designing, there will always be scenarios where you will need to override the standard Salesforce functions and extend them to get your functionality right. Today, I have a simple, yet useful Case Management hack, that will simplify your lives to a great extent.

Case is a very powerful object within Salesforce and “with great power comes great responsibility”. While dealing with cases, it is our responsibility as a Salesforce developer to ensure that case owners are notified every time there is a new comment added to their cases. These comment notifications are critical in ensuring that the case owner is aware of all updates made against their case and can plan the next steps required for the successful closure of the case.

Let us take a quick look at what standard features Salesforce offers for similar notifications on Cases.

Salesforce Feature


If you are the owner of a case record and you want to monitor the status of your Case, especially around case comments, Salesforce provides a notification feature for this. This feature is accessible and can be enabled from the Setup menu.

Then why does the Standard Function not Suffice?


It is a commonly requested feature to have Visualforce pages which allow users to add/update case comments. I had a requirement very similar to this, with a slight catch, that my Visualforce page was exposed on a website for customers to log, track and update cases.

When I tried adding case comments on the case object using Apex, these comments were successfully added but the case record owner did not receive any notifications around the same, despite the case notifications feature being enabled.

Here is a snippet of what I used to add case comments.


Let us look at two alternative workarounds to address this issue. The first one requires Apex, while the second one will not require any Apex code!

Trick 1: Using Apex


When we create case comments using Apex, we need to set triggerUserEmail true in our code. Even though auto-sent emails can be triggered by actions in the Salesforce user interface, the DML Options settings for EmailHeader take effect only for DML operations carried out in Apex code.

I implemented this using the following code:


This will now send out notifications on case comments via Apex.

However, you should note that for the notifications to work, Case Owner and Case Comment creator are to be different users.

Trick 2: Using Workflow


We can achieve the same result using a Workflow activity. For this, we need to write a workflow rule on the Case Comment object to perform a field update on the related Case. You can create a Boolean field Send Notification on the Case object for the same.

Step 1:  Create a field with Boolean data type.

Step 2:  Create a workflow on Case Comment as shown below:


Step 3: Create a Workflow action for Field update on Case as shown below:


Step 4: Activate the Workflow rule.

Step 5: Create another workflow rule on Case object. This will send an Email to the Case Owner every time our Boolean flag is updated.


Step 6: Create a Workflow action for Email Alert as shown below:


Pro Tip: You can use a custom Email template for configuring this email alert.

Step 7: Finally, activate your workflow!


Both these options are great workaround to ensure the case owner does not miss out on the updates. Irrespective of which option you go for, you will never miss out on a case alert anymore!



Written by Yogesh Sharma, Salesforce Developer at Eternus Solutions

No comments:

Post a Comment