In a previous post I showed how to update the Lead or Contact related to a Task when the activity was logged. That solution only supports updating the primary Lead or Contact (Task.WhoId).
But what if you have Shared Activities enabled and there are 3, 10, 50 Contacts or Leads related to the Task? How do we update all the related records not just the primary one? That is what fellow reader Elias Letsios asked too. In this post I show you my solution for supporting Shared Activities.
NOTE: This solution requires “Shared Activities” enabled in Setup | Activities | Activity Settings. If you do not have this feature enabled then please see my other post How to Update Lead or Contact When Activity Logged.
Getting Started
The solution I developed, available on my GitHub repo, includes one Process Builder, one Flow, and for a small (but important) step, one invocable Apex class.
- Enable Shared Activities in
Setup | Activities | Activity Settings
- Deploy the process, flow, and apex classes to your org:
- “Update Contact/Lead Task Relations” (Process Builder)
- “Update Contact/Lead Task Relations Flow” (Flow)
- “GetTaskWhoRelationSObjectsInvocable” (Apex)
- Since
TaskRelation
records are created asynchronously, wait for the Process Builder scheduled action to complete (usually within a minute) - Navigate to any of the Contacts related with your activity and note the description has changed to “Has been related to a Task.” (the specific field updates are customizable which we discuss later)
Flow Overview
The Flow kicks off in the top-left by querying for TaskRelation records related to the Task input variable that just got logged.
The second step is where our invocable Apex class comes into play to determine which relations are Contacts vs. Leads. More about why this is necessary in the next section below.
After that there are two main blocks in the Flow as outlined in green and blue in the above screen shot focused on looping through the related Contacts (or Leads) and assigning field updates before performing a Fast Update on the sobject collection variables.
Out of the box, the field updates (outlined in red in the above screen shot) simply set the Contact or Lead Description field to “Has been related to a Task.” Those Assignment elements are where you customize the Flow to your field update logic. Here you can either (a) do all your desired field updates in the Flow, or (b) update a custom Date/Time field specific to this solution and when the Contacts/Leads get updated then leverage another Process Builder you create that monitors for the custom Date/Time to change to do any other field updates or actions you desire. I recommend option B as that keeps the business logic outside of the mechanics of identifying which Contacts and Leads to update.
Why the #LowCode with Apex and not just #NoCode?
Unfortunately, Flow does not let you assign values to ID field of SObject Variables. So even though our Flow makes an initial query to learn all the Contact or Lead IDs related to the Task, there is no way to create SObject Variables with those IDs without either:
- making a Fast Lookup inside a loop (no code, but not bulk friendly), or
- invoking an Apex class to generate the SObject Variables for you (low code, but bulk friendly)
I chose the second option for performance reasons.
So after the first step in the Flow to query for the TaskRelations, we pass those records to the invocable Apex class to get a Contacts and Leads SObject Collection Variables.
Why do you use Scheduled Actions and not Immediate Actions in Process Builder?
Process Builder has to launch the Flow as a Scheduled Action because TaskRelations / TaskWhoRelations records are created after a delay by Salesforce and not immediately available.
If the Flow were to run as an Immediate Action then likely only the primary Contact/Lead (Task.WhoId) would be updated but the other related records would not (or at least be very inconsistent) because the Flow would not find them when the Fast Lookup step ran.
Hello Doug,
Thank you so much for putting this together it works great and it is exactly what I was looking for! I spent lots of time trying to put this together but I was getting stuck somewhere. Your solution with some small changes that were very easy to make, due to your great descriptions within the flows, worked with no problems!
Once again thank you very much for your help!
Best,
Elias Letsios
LikeLiked by 1 person
Glad to hear it, Elias!
The changes you had to make, were they problems with my Flow or tweaks for your own business logic?
Thanks,
Doug
LikeLike
Thanks so much for this! Any chance you’ve developed this for “events” as well?
LikeLiked by 1 person
Good idea, Christopher! Not yet but will add it as a feature request https://github.com/DouglasCAyers/sfdc-update-task-who-relations/issues/1
Thanks,
Doug
LikeLike
Hi Doug,
I am working on something similar and I am stuck. I can’t seem to be find your flow of shared activities. Would you be able to send me the link to the actual flow.
LikeLiked by 1 person
Hi Pooja,
The code is available on my GitHub project.
LikeLike
You’re awesome!!! I ran into taskrelation for the first time today when my pb/flow wasn’t updating activity counts correctly on contact level. I’m still having issues figuring out how to get from taskrelation to relationid back to taskrelation to get all taskids and then to count those by relationid to update the contact. This helps me troubleshoot what I was doing wrong. I wasn’t doing the delayed update or the apex class to get the id’s. fingers crossed I get it working!
LikeLiked by 1 person
I got an error when I tried to deploy to my sandbox. All the failures were around the classes. I do not see the flows/pb’s in my sandbox either. So not sure what I’m doing wrong.
LikeLiked by 1 person
Hi Monica,
If you’d like, you may open an issue on the GitHub project with your error messages and I can try to provide some troubleshooting help.Updated April 27 2018, this question is being tracked on GitHub.
Thanks
LikeLike
I deployed the package, and started creating Events and Tasks with multiple contacts. Shared Activities is turned on. I’ve checked that the Process Builders, Flows, and Apex is active. It seems, however, that nothing is happening.
Am I missing a step? I’ve looked at the Apex jobs and there is nothing at all from today, although I’ve created several tasks and events.
Any advice you are able to give is appreciated. This is a great solution, and I’d really love to leverage it!
Thanks!
LikeLiked by 1 person
Hi Robin,
Thanks for trying out my solution =)
After deploying the code from GitHub into your org, did you make any customizations to them? It may help me to troubleshoot if you could share some screen shots of your configurations.
Thanks,
Doug
LikeLike
Thanks for the response, Doug. I think I figured it out. I’ve a added some custom fields and logic to populate our custom last activity field, which many of our dashboard reports rely on. It’s in review now! Great solution, thanks!
LikeLiked by 1 person
Awesome! You’re welcome =)
LikeLike
you’re a hero Doug! Thanks for this.
LikeLiked by 1 person
You’re very welcome Dave 🙂
LikeLike
This was awesome thank you so much!!
LikeLiked by 1 person
Hi Doug,
Thank you for sharing this, really appreciate it.
I just deployed using Github tool but for some reason the last activity date for shared contacts are not getting updated.
all I did was just deployed from git hub and then I have created an event that is linked to 3 contacts and related to an opportunity.
Then I created a report on contacts to see last activity on those three contacts, but only primary contact has last activity date not the other two.
Could you please help if I meant to do anything after deployment.?
Thank you in advance.
LikeLike
Hello Doug,
Thanks for the wonderful posts. Being a newbie I am not able to figure out the following. I want a checkbox to be set to True if a checkbox on Event is set to True. I have already replaced the description field from the flow to the custom checkbox. But not sure where to add the condition I mentioned above. Please advise.
Thanks
LikeLike