+ Start a Discussion
Marcel dos SantosMarcel dos Santos 

Shared resources and flow bulkification

I have a Custom Object with a numeric field to be used as a counter. The reason for that is not important now.

I'm trying to create a Flow to update the counter for each new object I create.

I have now a Process and a Flow to achieve that. Whenever an object is inserted, the flow gather it's id and passes it as a paramter to the flow.

The flow has 3 components executing the following actions:
  1. Lookup for the object just inserted, using the id informed by the process;
  2. Lookup for any object of that type sorting on the counter field desc. That will get the max value;
  3. Increments the value and update the newly inserted recorde with that counter.
That flow is working as expected when I insert only one record. Because Salesforce bulkify the execution of queries in flows, when I insert more than one object in the same transaction, it groups the queries and run them all at once. That means that it will select the max counter once and increment the same value by 1 for all the new objects I'm inserting.

As an example, if the current max counter for the object is 4 and I insert 10 new objects, all the new objects will have the same counter 5, instead of 5 through 14.

Is there any way I can have a static variable there shared accross all flow instances in that transaction?
Gaurav HandooGaurav Handoo
Hi Marcel

Faced a similar issue in one of my project and the solution was pretty tricky. You would need to maintain a flag against the records of the object and manipulate the same via flow whenever you are updating the record with incremental number and while you are running an update via flow you need to add another check there to confirm if that flag has been changed for the current record or not and run the counter search loop again.

Hope this helps. Please mark as best answer if it does.

In case you need more details you can connect with me at gaurav.handoo@gmail.com


Abrar Khan 4Abrar Khan 4
@gaurav - Can you please explain how you acheived this a bit more please?
Abrar Khan 4Abrar Khan 4
Marcel dos Santos - Did you figure out a way to this problem?
Gaurav HandooGaurav Handoo
Hi Abrar

The additional flag being mentioned is a checkbox field that was created on the object for maintaining a check in bulk scenario. Whenever you create temp record in SObj, add another criteria check for this checkbox to be false and mark the checkbox as true once the incremented value is calculated; this ways, whenever you are trying to refer the record again, it will not reuse the previous number.

Hope this helps.