Last Updated on February 10, 2022 by Rakesh Gupta
Big Idea or Enduring Question:
Want to automatically unfollow records when records meet specific criteria?
As you know, users can Follow and Unfollow records by clicking on the Follow icon on the record’s Detail page. However, users can automate the Follow/Unfollow process – based on some criteria – either by downloading an AppExchange App or by building an app using Apex code.
This blog is a continuation of my last blog Getting Started with Salesforce Flow – Part 65 (Auto Follow Records Based on Criteria). In my previous blog, I had discussed the following use case – ‘How to automatically follow records based on users’ defined preference.’
This blog goes a step further. It explains how a user can use Flow to auto remove followers from an Opportunity record.
Objectives:
This blog post will help us to understand the following:
- Learn about Entity Subscription and how to use it.
- Create a Record-Triggered Flow to automatically unfollow records based on criteria.
- Learn how to use a Decision element to find whether a Record variable – or a Record collection variable – contains a record.
- Find out how to use the Loop element to extract records from a record collection.
- And, last but not least, discover how to use the Delete Records element to delete multiple records (remove followers) all at once (Bulk Safe).
Business Use Case
Brenda David is working as a System Administrator at Universal Containers (UC). As part of her Salesforce adoption process management, Brenda wants to provide a tool so that users can define the criteria to auto-follow an Opportunity record (Blog 65).
- Now, the Sales Director wants to remove followers from an opportunity once it is Closed, if followers details found in Opportunity Follow/Unfollow Preference object.
Automation Champion Approach (I-do):
There are a few possible solutions for the above business scenario, but I’ll use After-save Record-Triggered Flow to solve the business requirement. Check out this article to understand why we are using after-save record-triggered flow for this scenario.
Field Name | Details |
ParentId | The Id of the record or user which the user is following. |
SubscriberId | The Id of the user who is following the record or user. |
Before discussing the solution, let me show you a diagram of a Process Flow at a high level. Please spend a few minutes going through the following Flow diagram and understand it.
Let’s begin building this automation process.
Guided Practice (We-do):
There are 3 steps to solve Brenda’s business requirement using After-save Record-Triggered Flow. We must:
- Create a new version of flow created in Blog 65
- Update decision element to check if Opportunity is closed
- Add a get records element to find all Opportunity followers (subscribers)
- Add a decision element to check if a opportunity followers record exists
- Loop element to extract records from the record collection variable (step 3)
- Loop element to extract records from the record collection variable (step 2.3 – from blog 65)
- Add a decision element to check if Opportunity Follower details matches with a record in Opportunity Follow/Unfollow Preference
- Add an assignment element to add subscribers into a record collection variable
- Add a delete records element to remove followers from Opportunity (bulk safe)
Step 1: Salesforce Flow. – Create a Version of Existing Flow
Now we will modify the existing Salesforce Flow, which we created as a part of Blog 65.
- Click on the Save As button available on the button bar. It will open a popup for you.
- Under Save Clone As, select A New Version option. It will look like the following screenshot:
Step 2: Salesforce Flow – Using Decision Element to Check if Opportunity is Closed
Now we will update the existing Decision element to check, if opportunity is closed. You can take help from the screenshot below:
- Double clicked on the Decision element, Open vs Closed.
- Under Outcome Details, click on the +
- Enter a name in the Label (Closed) field; the API Name will auto-populate.
- Condition Requirements to Execute Outcome: All Conditions Are Met (AND)
- Row 1:
- Resource: {!$Record.IsClosed}
- Operator: Is Changed
- Value: {!$GlobalConstant.True}
- Click + Add Condition
- Row 2:
- Resource: {!$Record.IsClosed}
- Operator: Equals
- Value: {!$GlobalConstant.True}
- Row 1:
- Click Done.
Step 3: Salesforce Flow – Adding a Get Record Element to Find all Opportunity Followers (Subscribers)
The next step is to get all Opportunities follower records from EntitySubscription object. We will use the Get Records element. To do this drag-and-drop Get Records element onto the canvas and map the fields according to the details below:
- Under Toolbox, select Element.
- Drag-and-drop Get Records element onto the Flow designer.
- Enter a name in the Label field; the API Name will auto-populate.
- Select the Entity Subscription object from the drop-down list.
- Select All Conditions Are Met (AND).
- Set Filter Conditions
- Row 1:
- Field: ParentId
- Operator: Equals
- Value: {!$Record.Id}
- Row 1:
- How Many Records to Store:
- select All record
- How to Store Record Data:
- Choose the option to Automatically store all fields.
- Click Done.
Step 4: Salesforce Flow – Using Decision Element – Does Opportunity Followers Found?
Now add the Decision element to decide whether or not we found the opportunity followers (subscribers) records. If the Resource (record collection variable) is null, then it means there was found. You can take help from the screenshot below:
- Under Toolbox, select Element.
- Drag-and-drop Decision element onto the Flow designer.
- Enter a name in the Label field; the API Name will auto-populate.
- Under Outcome Details, enter the Label the API Name will auto-populate.
- Condition Requirements to Execute Outcome: All Conditions Are Met (AND)
- Row 1:
- Resource: {!Get_Opportunity_Followers}
- Operator: Is Null
- Value: {!$GlobalConstant.False}
- Row 1:
- Click Done.
Step 5: Salesforce Flow – Loop to Retrieve Records from Collection Variable (Step 3)
- Drag-and-drop the Loop element onto the Flow designer.
- Enter a name in the Label (Loop Through Followers) field – the API Name will auto-populate.
- For Collection Variable select {!Get_Opportunity_Followers}.
- For Specify Direction for Iterating Over Collection select the option First item to last item.
- Click Done.
Step 6: Salesforce Flow – Loop to Retrieve Records from Collection Variable (Step 2.3 – from blog 65)
- Drag-and-drop the Loop element onto the Flow designer.
- Enter a name in the Label (Loop through Preferences – Nested Loop) field – the API Name will auto-populate.
- For Collection Variable select {!Get_Follow_Unfollow_Preferences}.
- For Specify Direction for Iterating Over Collection select the option First item to last item.
- Click Done.
Step 7: Salesforce Flow – Using Decision Element – Does Opportunity Follower Matches with a Record in Opportunity Follow/Unfollow Preference?
Now add the Decision element to check if Opportunity follower (subscribers) data found in Opportunity Follow/Unfollow Preference or not. If a match was found then we want to remove the followers from opportunity, else we don’t want to take any action. You can take help from the screenshot below:
- Under Toolbox, select Element.
- Drag-and-drop Decision element onto the Flow designer.
- Enter a name in the Label field; the API Name will auto-populate.
- Under Outcome Details, enter the Label the API Name will auto-populate.
- Condition Requirements to Execute Outcome: All Conditions Are Met (AND)
- Row 1:
- Resource: {!Loop_through_Followers.SubscriberId}
- Operator: Equals
- Value: {!Loop_through_Preferences_Nested_Loop.Follower_Name__c}
- Row 1:
- Click Done.
Step 8: Salesforce Flow – Adding Values to a Record Collection Variable
- Create a Record Collection Variable varRSubscriberToDelete of type Entity Subscription to store record variable (loop variable from step 5) for the bulk process.
- Drag-and-drop the Assignment element onto the Flow designer.
- Enter a name in the Label (Add Subscribers to a List) field – the API Name will auto-populate.
- Set Variable Values
- Row 1:
- Field: {!varRSubscriberToDelete}
- Operator: Add
- Value: {!Loop_through_Followers}
- Row 1:
- Click Done.
Step 9: Salesforce Flow – Add a Delete Records Element to Remove Followers from Opportunity
- Drag-and-drop the Delete Records element onto the Flow designer.
- Enter a name in the Label (Delete Subscribers) field- the API Name will auto-populate.
- For How Many Records to Create select Multiple.
- Map Record Collection: {!varRSubscriberToDelete}
- Click Done.
In the end, Brenda’s Flow will look like the following screenshot:
Once everything looks good, perform the steps below:
- Click Save.
- Click Show Advanced.
- API Version for Running the Flow: 52
- Interview Label: Record-Triggered After Save: Opportunity Follow {!$Flow.CurrentDateTime}
- Click Save.
Almost there! Once everything looks good, click the Activate button.
Proof of Concept
- Now create a few records in the object Opportunity Follow/Unfollow Preference to save criteria to Auto Follow records, it will look similar to the screenshot below:
- Opportunity KapStone Paper & Packaging now has two followers:
- Now update the opportunity stage to Closed Lost and the check the followers count.
Formative Assessment:
I want to hear from you!
What did you learn from this post, is it relevant to you, and how will you modify the concepts taught in the post for your own business processes?
Make a post and tag me on Twitter @automationchamp, using #AutomationChampion.
Hi Rakesh! I just finished up creating auto follow and auto unfollow objects per this design (using for a custom project management object instead of opps so our boss auto follows everything and then is removed once the status is closed!). Waiting to see if the time based flows work, they are in the queue so hopefully my flows are right 🙂 Thanks so much for these detailed instructions, they were easy to follow!!
Thank you so much for your comment Melissa!
Rakesh – I might have messed something up with the loop or workflow? Everything worked great yesterday, the auto follow was confirmed and the auto unfollow was completed, but now it seems that it keeps looking for more record to unfollow and there are none… do you know what I did wrong? Thanks!!
Sandbox
Encountered unhandled fault when running process AutoUnfollowProjects/301110000004LRL exception by user/organization: 00D110000007sqT/{4} Source organization: 00D6000000072fJ (null) DELETE — There is nothing in Salesforce matching your delete criteria
caused by element : FlowRecordDelete.Delete_all_records
caused by: DELETE — There is nothing in Salesforce matching your delete criteria
Salesforce Error ID: 1611219639-38560 (-1205603607)
Thanks Melissa. Do one thing before Fast delete add one decision element and check if Sobject collection {!Del} not equal to null then only perform the deletion operation else skip it.