Process builder and Trigger possible recursion

442 Views Asked by At

I have created a Trigger on Order object. There is a field called pacing in both Order and Order product Objects. When the Pacing is updated in the Order, I am updating the Pacing field in the Order Products too that are related to that particular Order, using the below code.

if(ord.Pacing__c != oldOrd.Pacing__c) {
      changedPacing.put(ord.Id, ord);
      pacingMapVal.put(ord.Id, oldOrd.Pacing__c);
}

if(changedPacing.size() > 0) {
      OrderToOrderProductAutomation.updateLineItemRecord(changedPacing, null, null, null, pacingMapVal);
}

if(pacingArgMapVal != null && pacingArgMapVal.size() > 0) {
       List<OrderItem> updates = new List<OrderItem>();
       for (OrderItem detail : [SELECT Id, OrderId, Customer_Success_Manager__c, Line_Item_Start_Date__c, Line_Item_End_Date__c, Pacing__c
                                            FROM OrderItem
                                            WHERE OrderId IN :ordersListMap.keySet()]) {
            Order oso = ordersListMap.get(detail.OrderId);
            String pacing = oso.Pacing__c;
            if (detail.Pacing__c == pacingArgMapVal.get(detail.OrderId)) {
                 detail.Pacing__c = pacing;
                 updates.add(detail);
            }
        }
        update updates;
  }

I have created Process builder which creates a record in a custom Object called History__c whenever the a particular 6 fields are updated in Order, including the Pacing, by calling a flow using process builder.

Now whenever I am updating the pacing. 3 identical records are getting created in History Object. When I comment my trigger code, one History record is getting created as expected. Can anyone please let me know why this is happening and How can I sort this out.

Note: There are 3 roll-up summary fields in Order Object, to the Order Product object.

This is how my process builder conditions looks like.

enter image description here

1

There are 1 best solutions below

8
On

Hard to say without debug log so this is more of couple ideas. Is this one block of code or did merge 2? Variable names are bit confusing.

How does this run? Only "after update" on Order?

You write values to pacingMapVal but read values from pacingArgMapVal. Is it same variable (just renamed when passed to function or something)? If these are separate variables my guess is the pacingArgMapVal is empty. Any get on it will return null so you're comparing with null. Was your intention to cascade this value only to lines with Pacing = null? You could have done it bit smarter with WHERE OrderId IN :ordersListMap.keySet() AND Pacing__c = null or WHERE OrderId IN :ordersListMap.keySet() AND Pacing__c IN :pacingArgMapVal.values()...

Similarly ordersListMap - where is this being set?

You (probably) have 1 update that happens inside OrderToOrderProductAutomation.updateLineItemRecord. We can't tell what's inside, any chance you duplicated the code?

Then another update that happens in update updates; (except there's chance it doesn't update anything).

How does your flow / process builder decide when to write new rows?