[SalesForce] Roll-up summary field using PB + Flow

We have 2 objects – ObjectA and ObjectB. Object B has a lookup to ObjectA (so ObjectA is parent and ObjectB is child). We need to calculate no. of Active ObjectB records and show it in ObjectA record page. If it was M-D relationship, roll-up summary field was the solution, unfortunately it is not and it cannot be. We cannot have a report chart shown since this field is used for various other calculations.

So, we used Process Builder + Flow to calculate the no. of Active ObjectB records. On create/update of ObjectB, we pass the ObjectA__c reference to the flow, get all the ObjectB filtered on Status = Active and store it in a list variable. This happens in the loop element of flow and outside the loop element (after last), we update ObjectA's ActiveCount__c field.

References – https://success.salesforce.com/answers?id=90630000000476cAAA
https://admin.salesforce.com/use-flow-builder-to-create-a-roll-up-summary-field
Create a roll-up summary field on Lookup without coding

This was all good when we tested by creating ObjectB records. But now when do proper testing, where we had 20 ObjectB records inserted, we got DUPLICATE_VALUE: Maximum number of duplicate updates in one batch (12 allowed). Attempt to update Id more than once in this Api call: ObjectAId

I am wondering if this is known issue. But as per documentations, Salesforce suggests that Flows are bulkified. Is this truly bulkified or am I doing something wrong?

Best Answer

Process Builder and Flows are bulkified (e.g. you can safely update a field on 200 records at once), but they can't handle the situation where a single record is updated a large number of times in the same transaction (more than 12). Unfortunately, Flows and Process Builder aren't great at updating parent records in a bulk pattern, and there's no way to make something that resembles a "map" in the Flow engine. You will need to use Apex, either by way of an InvocableMethod or just writing a trigger to enable mass updates in a safe manner.