0

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

Akshay Vasu
  • 445
  • 1
  • 12
  • 33

1 Answers1

1

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?

eyescream
  • 18,088
  • 2
  • 34
  • 46
  • Hi Sorry for the delayed reply. The code that you see above have been taken from two different places. The first two if blocks are from trigger and the third block is from associated apex class that handles the trigger call. I am passing the arguments from trigger and using it in the class, hence the difference in the name of variables. – Akshay Vasu Nov 30 '20 at 10:39
  • My process builder creates a custom object record when the Pacing at the Order level is updated from one picklist value to another picklist value. Also I had trigger logic in place which updates the Pacing field that exists in Order Item with the Pacing field values of the Order on update at the Order. I am beginning to think, that this what causing the issue. There are 3 rollup summary fields in Order to Order Line item. They might the culprits in calling the process builder again when trigger updates line item values. – Akshay Vasu Nov 30 '20 at 10:43
  • What do you have in process builder's decision nodes (blue diamonds). if you put some ISCHANGED() formulas in there then it shouldn't execute during rollups. – eyescream Nov 30 '20 at 12:04
  • Actually I've been using the ISCHANGED() Conditions only. I have edited and added the image of how the conditions in the decision node looks like in the question. – Akshay Vasu Nov 30 '20 at 12:43
  • Interesting. And the rollups shouldn't impact these 6 fields in any way? None of these 6 fields is a rollup, none is updated with a workflow or something that fires when rolled up field changes etc? You might have to look at debug log, maybe there are other triggers/workflows/flows/processes at play. What if you eliminate it a bit, make a new version of this process that executes only on 1st (Pacing) change and see if it writes one or 3 entries. Also - you might be victim of https://help.salesforce.com/articleView?id=new_order_save_behavior_intro.htm&type=5, see if you can temp disable & test. – eyescream Nov 30 '20 at 13:04
  • I disabled my trigger and Process builder one after the other to test it. When I disabled my trigger the process builder began behaving the way I expected it to, and is creating a single record now, instead of 3. I am planning to create History record for Pacing field change only through trigger itself, and leave every other field the way it is already. As Updating of Pacing field is what calling my trigger too. And I guess that would resolve this creation of identical records issue. Would there be any other work around? – Akshay Vasu Nov 30 '20 at 16:15
  • Nothing comes to mind, you'd need to analyse debug log in detail. If you put everything in trigger (handler) you lose some flexibility but you could always put some flags `public static Boolean dontRun;` and `dontRun = true; update products; dontrun = false;` hacks to conditionally suppress creation of the history records in complex scenarios... – eyescream Nov 30 '20 at 18:08
  • Sure I'll try doing that. Thank you – Akshay Vasu Dec 01 '20 at 09:55