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
- Static variable
- 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
So I am totally in soup and not sure how to swim out of that