Get last task or event date against multiple contacts in Salesforce (on create/update)

 

 Quite often a question bothers business users as well as functional experts how to get latest event/task date for a particular contact in Salesforce and plan a communication based on contact activity report, like ones with last activity past one month etc.

Last modified date or only Last activity date on Salesforce record is not a true indication of latest communication done with that contact/lead/account. Its actually the latest event or task associated with respective lead, contact or account.

The document below explains setup and technical implementation of the same.

This might sound easy, but it’s not easy as just go and put LastModifiedDate of that Contact on Report. It’s bit challenging. Every time we got Task Created Date of a particular Task which is associated to that Contact or Account on insert/update.

Getting right Last Task Created Date for Task which is associated with 2 or more Contacts becomes challenging, as by default it takes first contact associated to that Task and puts task created date on that first contact only. :)

After spending much time on Apex Triggers, we thought that Instead of Record Triggered Flow let’s make a combination of Process Builder and Flows.








Later, a 
report created on contacts with last task date, last event date or last activity date (standard field) gives a right representation to move ahead with decision making on selected contacts.

Here are some objects which we use in this solution:

1.   1. Task Object:

In task object, we have task id (ID), contact associated with that task (WhoId), account associated with task (WhatId), How many contacts are associated with the task (WhoCount).



 2.    TaskWhoRelation Object:

In this object, we have Task Who Relation Id (ID), associated person to Task (RelationId), associated Task (TaskId). In this example, it shows 6 people records associated to the same task.



 Here’s How.

1.    Create two custom fields Last Task Created Date & Last Event Created Date on Contact Object.

2.    Make a Process Builder on Task with entry criteria when task is created or updated. And Criteria condition is Task.WhoId IsNull False, after that put Flow as Immediate Action.



3. Make Autolaunched Flow.



 

Quick Steps (Flow):

a.     Get Contacts associated to Task: Get records of TaskWhoRelation  object. Filter this object by condition TaskId Equals text Variable. And store Id and RelationId in one collection Variable.




b.    Records Found: Decision for checking whether that collection variable is not null. Records Founds outcome looks at the items in the {!collectionOfContact}

 


c.     Loop: Now we need to create a LOOP flow element so that we can go through each TaskWhoRelation record in collection. It looks at the Record Collection Variable {!collectionOfContact}.



 d.    Lookup Contacts: For each loop record, we use a Get Records flow element to look up a contact record that matches the loop record (i.e TaskWhoRelation). It will lookup on the Contact object where Id equals {! LOOP.RelationId} and only retrieve the first record found. It will store Id and the Last Task Date




 e.     Get Task Date: Get task date from TaskWhoRelation object where we are matching RelationId with ContactId of Lookup Contact. And  sort CreatedDate by descending order and store only first record where we can store the fields of TaskWhoRelation object in text variables.





 

f.     Update Contact: For each loop record, in the Assignment flow element, we will assign the Last Task Date to taskDate variable.



 

g.    Add to Collection: For each loop record, in the Assignment flow element, we will add the loop record to a new collection that will be updated.



 

h.    Update Task Date: Next, we need to create the Update Records flow element to update all the contacts in collection variable.



 

Flow Debug:

Pass appropriate task record and debug the flow successfully.


 

The latest activity (when Task and Event is created or updated) on Contacts Report can be displayed too.

Do share your views. 

By: Kimaya Shirude ;

Salesforce developer - Thutech Infosystem 

www.thutechs.com

 

 

 

 

Comments

  1. This is really helpful for right contacts communication. Liked the way its explained.

    ReplyDelete

Post a Comment

Thanks for your suggestions/comments.

Popular posts from this blog

Post Merger - CRM Operations Onboarding