0

I am trying to sync opportunity to third party only when fields of opportunity gets changed. By comparing the oldMap and newMap, I determine whether there is a change in fields or not, and if I suspect any change, I call the HTTP service to sync opportunity data.

Due to workflow rules, my trigger is getting called multiple times/recursively – which is obvious and for the fields, those are not part of the workflow in that case, the trigger is getting called only once. I have written opportunity after insert and after update trigger and to control the recursion I have tried two different approaches

  1. Static variable
  2. Adding and removing the Map<Id, Opportunity>

The biggest hurdle is if I use a static variable approach then it works only for the fields that are part of workflow because when the static flag is true I am not performing any comparison checks and calling webservice.

if I use the Adding and removing the Map<Id, Opportunity> approach I do not get the proper oldMap and newMap value to compare. this approach overrides the change and eventually oldMap and newMap have the same values.

Map<Id, Opportunity> recordsNew = trigger.newMap; Map<Id, Opportunity> recordsOld = trigger.oldMap;

 if(SyncInfoController.isExecute){


      // this condition met only when the fields those are not part of workflow gets changed 
      // but if i call SyncInfoController.createUpdateSyncInfo here too , it will call the else part when fields those are part of workflows gets changed
      SyncInfoController.isExecute=false;

   }else{

        // works well for fields those are part of workflows as trigger is getting called twice/thrice
         SyncInfoController.createUpdateSyncInfo(recordsNew,recordsOld  ,false,Datetime.now()); 

        
      
  }

here is the flow

enter image description here

So I am totally in soup and not sure how to swim out of that

3
  • If workflow rules customization ability is not a must, I'd move your workflow rules logic to the trigger. That way you'd control the execution flow and make the process more performant.
    – Alba Rivas
    Commented Sep 28, 2020 at 9:41
  • won't be possible that
    – Hunt
    Commented Sep 28, 2020 at 9:47
  • @AlbaRivas client has around 26 workflows on opportunity object so I can not accommodate all in my trigger :) and this issue is now eating me up since long
    – Hunt
    Commented Sep 28, 2020 at 13:16

1 Answer 1

1

Use a Change Data Capture Event Trigger instead. This will give you the cumulative updates that occurred without worrying about multiple calls or recursion. The proper solution as a direct trigger would be to instead store all cumulative updates into an object and call asynchronous code to perform the update later.

2
  • I tried Changed Event and it works well so far, I had to change the entire approach and had to make changes in my other 4 files :) but it is worth changing. at least i don't have to deal with all those static variables and recursion issues. Thank You !
    – Hunt
    Commented Oct 1, 2020 at 15:15
  • @Hunt Glad I could help!
    – sfdcfox
    Commented Oct 1, 2020 at 15:32

You must log in to answer this question.

Not the answer you're looking for? Browse other questions tagged .