SFDC Stop - Always the latest about Salesforce


Full Tutorial Series with videos, free apps, live sessions, salesforce consulting and much more.


Telegram logo   Join our Telegram Channel

Saturday, 28 March 2020

Are Scheduled Flows Bulkified ? Let's Find Out...!!

Hi Trailblazers,

Have you heard about scheduled flows in Salesforce ? If not, let me tell you a brief about them. Scheduled Flows are flows that are scheduled to run on a specific date and time either once or daily or weekly. So, the question arises that if the flows are scheduled then how do they process records ? Do they process records one by one or they're bulkified and process many records at a time ?

In this post, we're going to create a scheduled flow and will check wether the scheduled flows are bulkified or not.

Use Case / Scenario:- Let's say for reporting purposes you need to find out for each account which is Active, how many contacts have opted out of email. So, as an awesome admin you designed a solution for it and you decided to create a scheduled flow that will run daily at 12:00 A.M. and will check how many contacts have opted out of email for each account and finally, it'll update the count.

It's a custom Roll-Up kind of thing with the fact that it will update all accounts once on a daily basis. Before we begin, I have created two custom fields on Account one is a custom Number field on Account object with API name as:- NumberOfContactsOptedOutOfEmail__c as we're going to update this field using our flow and the second is a custom Checkbox field with API Name as:- IsActive__c as we're only going to check for accounts that are active. You can create the number field as shown below:-


Also the checkbox field is shown below:-


So, we have our fields ready. Now it's time to make a scheduled flow.

1. Go to setup and search for flows.


2. Click on the New Flow button. This will open the flow builder. In the new flow dialog, choose Autolaunched Flow and click on Create button.


3. Double click on the Start button that's already available in the flow builder and you'll see a dialog where you can schedule a flow. As you can see below, In the radio button you should select Scheduled jobs—flow runs for a batch of records, in the Set a Schedule section, you need to enter the Start Date and Start Time. You can set it to the next day's date and time as 12:00 AM as we want our scheduled flow to run at 12:00 AM and also you should set the Frequency as Daily.


Also, in the Run the Flow for a Set of Records (Optional) section. Set the object as Account and in Condition Requirements set it to Conditions are Met (AND). We're adding a condition to consider only active accounts here so we have set the field as IsActive__c, set the Operator to Equals and Value to {!$GlobalConstant.True}. Click on Done.

Now, this flow will run daily at 12:00 A.M. and will get all accounts that are active. Each account in this flow will be stored in the {!$Record} variable automatically. So, our next step is to get all contacts that have opted out of email and are related to the current account record.

4. For this, drag and drop the Get Records element on the flow builder from the left and set the label, api name, description as shown in the below image:-


As you can see above, we've selected the object as Contact as we're getting contact records here and below that in the condition, we've selected Conditions are Met. I have added two conditions as shown below, 1st is to check that contact has opted out of email so that checkbox should be true and the second one is to confirm that we're only getting contact records related to the current account record, so here the AccountId Equals {!$Record.Id}


The contact records are not sorted in our case as we don't want that. Below that we have few more options:-


As you can see above, we need to get all records that specify the above condition so we've selected All records radio button and to store the data, we're only querying the ID field as we just need to calculate the count of these contacts. Also, salesforce will automatically assign a variable for these queried contacts.

5. Now, we queried those contacts related to current account record who have opted out of email. It's time to run a Loop on those contacts and count the records. Let's have a look at the below image, where we dragged and dropped a Loop element of flow builder. I have given a name and description and in the Collection Variable, we need to select the variable containing the list of contacts. As we have given that responsibility to salesforce so you can see that Salesforce has automatically created a variable named as:- Contacts from Get_contacts_who_has_opted_out_of_Email. Select this collection variable.


We've then selected to loop from First item to last item as order doesn't matter to us now. For Loop Variable, I am going to create a new Loop Variable here to refer the current contact in the loop.


6. We've created a new Loop Variable of type Record and Object as Contact which is Available for Output as we just need it to count the records.


Click on Done and you'll see our new variable is set.


Click on Done again.

7. Now we're looping contacts and we'll get each contact record in Contact variable. So, it's time to count those contacts and store the total count in a variable. So, I dragged and dropped an Assignment element on the flow builder as shown below:-


If you click on the Variable field, you'll see that you have the Contact variable in which we'll be having one contact at a time inside the loop.


But here, we need to assign the count so, we're going to create a New Resource for that. Click on New Resource and set the values as shown below:-


So, we've made a new resource of type Variable which has an API name of ContactsCount and has a Data Type as Number with 0 decimal values and the default value is also 0. This variable is available for both input and output as we'll be updating this variable for each contact and we'll be using this variable to update our account at the end. Once you've filled these values, click on Done.

8. You'll see that the ContactsCount variable is available on the assignment. For each contact we encounter in the loop, we need to Add 1 to the ContactsCount variable. You can set this as shown below.


So, the ContactsCount variable will calculate the total number of contacts who have opted out of email and are related to the current Account. Click on Done.

9. We'll be having the above assignment inside the loop and outside the loop when we have the Total Contacts Count, we need to assign that count to the NumberOfContactsOptedOutOfEmail__c field of current Account record. You can see another new assignment element below for this purpose.


Now, we need to update the current account record. So, click on Done and drag and drop the Update Records element on the flow builder and select our {!$Record} variable which is specifying the current account record in the scheduled flow as shown below:-


Click on Done. And connect all the elements together. Your final flow will look like as given below:-


Note in the above image, that the assignment which is updating the ContactsCount variable is inside the loop and the assignment which is updating the NumberOfContactsOptedOutOfEmail__c field is outside the loop so that the total is calculated assigned to the current account field after the last item in the loop. Finally, we updated the current account record.

10. Save the flow by clicking on the Save button on the top right with values as shown below:-


11. Activate the flow by clicking on the Activate button on the top right as shown below:-


Congratulations..!! Your scheduled flow is activated that will update all the active accounts with the number of related contacts who have opted out of email.

Note:- Have you noticed that we've made the whole flow considering that we're only dealing with the current account record that is stored in the {!$Record} variable. But in actual, there may be hundreds of active accounts that are stored in salesforce. So, in order to check how scheduled flows deal with bulk records. I created about 500 accounts and each account have 4 contacts out of which 2 of them have email opt out as shown below.


The flow was already activated and it ran at 12:00 A.M. Now, It's time to check the logs.


I checked that a total of 4 logs were created for 500 Accounts and about 2000 contacts that were there in my org as each account was linked to 4 contacts.

I also queried the accounts and checked that all accounts were updated with value 2 in number of contacts opted out of email field as shown below:-


So let's have a look at all 4 logs one by one:-

Log No. 1


We got to know that 500 accounts were retrieved for our scheduled flow.

Log No. 2


In this log we got to know that our flow begin processing 100 account records and for these 100 account records, 200 contacts were queried as each account has 2 contacts that has opted out of email.


Also, at the end 100 accounts were updated in a single DML statement as shown below:-


So, our log no. 2 actually processed 100 accounts in which 1 SOQL query was used to query 200 contacts and 1 DML statement was used to update 100 accounts. Let's have a look at Log No. 3.

Log No. 3


In this log we got to know that our flow begin processing 200 account records and for these 200 account records, 400 contacts were queried as each account has 2 contacts that has opted out of email.


Also, at the end 200 accounts were updated in a single DML statement as shown below:-


So, our log no. 3 actually processed 200 accounts in which 1 SOQL query was used to query 400 contacts and 1 DML statement was used to update 200 accounts.

Log No. 4 is exactly similar to Log No. 3 and it also processes 200 accounts. So, in this way our 500 accounts were processed in a batch of 100, 200 and 200 accounts respectively.

Therefore, we conclude that Scheduled Flows are Bulkified and process records in a batch of 200 records.

Note:- In case your flow is not running on the scheduled time, make sure that you're setting the right time for your flow based on the default time zone in Company Information (Go to Setup -> Company Information)

If you liked this post, do share it in your network and let me know your feedback in the comments down below.

Happy Trailblazing..!!

2 comments:

  1. Very Interesting and helpful blog !! Thanks Rahul.

    ReplyDelete
    Replies
    1. So happy to know that it helped :-) Thanks for the feedback Tanu :-)

      Delete