Updating Related Record Via Apex When Using ‘Send an Email’ Button Under Activity History

You are currently viewing Updating Related Record Via Apex When Using ‘Send an Email’ Button Under Activity History

Ever tried firing a Process or a Workflow Rule using the ‘Send an Email‘ button under Activity History? Well I did and spent one whole hour attempting to make my Process and Flow work. I made the mistake of being overly optimistic that something must have changed since the following idea was created two years ago and after the launch of Process Builder:
https://success.salesforce.com/ideaView?id=08730000000kqvHAAQ

The requirement was to update the Lead Status from ‘Open- Not Contacted‘ to ‘Working – Contacted‘ when an Email is sent using the ‘Send an Email‘ Button from the Activity History related list on the Lead record. This is what I created to accomplish the requirement:

  1. A Process on the Task object which should fire when a Task is created.
  2. Criteria (Task type is Email which has been sent and associated record is a Lead record):

    AND(
    TEXT([Task].TaskSubtype) = "Email",
    [Task].IsClosed,
    LEFT([Task].WhoId, 3) = '00Q'
    )

  3. Immediate Action:  Launch a Flow, pass the Lead Id (Name Id in Process) into a vLeadId Flow variable and then use a Record Update element to update the Lead Status

Flow

 

Frustrated
Looks straightforward and simple, right? After implementing the above, I went to a Lead record and spammed myself with Emails but the status refused to budge. Assuming it’s a problem with my formula, I changed the Process to execute the actions without a criteria but yet again, to no avail.

Tired and annoyed, I posted the issue on Twitter and Matt Bertuzzi was kind enough to point me to a question on the Answers Community where I found something that I wasn’t hoping for.

David

 

 

But hey, at least my suspicions were confirmed and I knew exactly what to do- Build the solution using Apex. All it took was 25-30 lines of code (and of course a much bigger test class) to get this to work. Here’s the Apex class that I wrote:

Apex class

And the trigger to fire it:

Trigger

 

The trigger fires after a Task is inserted into the database and feeds the Trigger.new batch records to our Apex class test method by invoking it. The Apex class performs the rest of the heavy lifting. It uses only those tasks which are of type = Email and have been completed. The related Lead records where the current status is ‘Open- Not Contacted‘ are then updated to have a status of ‘Working – Contacted‘.

The test class is fairly straightforward and I will let you take care of that. Like always, if you know of a better or more optimized way of accomplishing such a requirement, I would love to hear about it in the comments below.