I have been lately seeing a lot of questions on the Answers Community related to automatically adding or removing contact/lead from a Campaign when a field is changed on either one of them. Being an avid fanboy of Process Builder and Flows, I love responding to any questions remotely concerned with these automation tools. The objective of this post is to demonstrate how straightforward it is to manipulate Campaign Data via changes made on a Contact or Lead. The ulterior motive however is to link the community folks to this article if a similar question pops up in the future. I will tackle a specific requirement here which can be easily tweaked to meet any related requirement.
Requirement: The system administrator needs to create an automated process which automatically removes a Contact from all Campaigns (that he/she is a part of) as soon as the contact is flagged as Inactive.
Design Approach (questions to ask yourself before you begin the implementation):
- What are we really trying to do here?
Good question! So the primary part of this implementation is first finding the Campaign/(s) which the Contact is a part of and then removing him/her from the Campaign/(s). Which means that we are essentially deleting a Campaign member. Helps put things in perspective, right? Let’s proceed with the rest of the questions.
- What should be the trigger for the automation?
A contact is created or edited and is flagged as Inactive – This becomes the criteria
- Should the automation fire every time the Contact is edited?
Nah, just firing it once when the contact is marked inactive should suffice – This means that we are going to enforce the ‘subsequently meets criteria’ evaluation
- And now the million dollar question among all: Which automation tool/(s) are we using to implement this requirement?
Well we are going to perform some deletion here which is only possible either via Visual Workflows (commonly known as Flows) or Apex. Let’s keep Apex out of the picture since we are trying to design this declaratively.
For the deletion we need to find all Campaign Members that have their ContactId field equal to the ID of the Contact being flagged Inactive – This can be done within the flow
The Flow needs to fire when the Contact is flagged – This can be easily done via the Process Builder
So there we go! We are going to use the Process Builder to create a Process that is triggered when a Contact is created or edited to be flagged Inactive which launches a Flow. This Flow performs the heavy lifting of finding all related Campaign Member records and then purging them from the system. This is what the complete automation flow will look like:
Now when we have got our bases covered, let’s get down to business:
CREATING THE FLOW:
We need two variables in this Flow: One to receive the ID of the Contact being flagged inactive (vContactId) and other to store the Campaign members (vColCampMem).
Using the Fast Lookup element: There can be multiple Campaign members found for a single Contact which means we have to store the result in a Collection variable which demands usage of a Fast Lookup element.
Using the Decision element: We need to factor in the possibility of our Contact not being a part of any Campaign which means no Campaign members are found. The deletion step should only be performed if a related Campaign member is found which is why we need the Decision element.
Using the Fast Delete element: Well you need a Fast Delete element to delete the records inside a collection variable.
Do not forget to activate the Flow otherwise it won’t be accessible within the following Process.
CREATING OUR PROCESS THAT LAUNCHES THE FLOW:
Activate the Process and you are ready to test!
AFTER the magic happens:
See how easy it is to manipulate records via Process Builder and Flows? We were able to query records in an object, store them in a collection variable and also delete them, all of it without using Apex! This is the beauty of Visual workflows and that is exactly why I am so fond of them.
Feel free to share in the comments below any similar scenario involving Contact/Lead and related Campaign data manipulation that you tackled using Process Builder and Flows.
Related Answer Community questions: