I have a Trigger on an object, which is calling a method of other class(passing Trigger.New so that some functionality is executing depending on values of passed record) which should be called after insert
, after update
, after delete
or after undelete
of a record. I am using this trigger for rollup purpose, and cannot use standard salesforce rollup functionality for some reasons like no master-detail relationship.
This method is added to the trigger so that when the record is inserted or updated from Data Loader
or from a Visualfore Page
it should be executed and do some functionality depending on the values of the record.
Now from Visualforce I'm inserting a new record and updating another record(let's say Old record) of the same object in one transaction.
This object has some Workflow Rules and Process Builder flows created on it.
Now the issue is that this method is getting executed multiple times in same transaction since the new record is getting inserted(updated from WF and Process Builder), Old record is getting updated (updated once again from WF and process builder).
The method will execute for 4 times which is a cause for a governor error (SOQ) in my case.
Solution using to resolve:
- Create a static boolean variable(default:- false) and set it to true in VF controller.
- In trigger do not execute this method if the boolean variable is true(execute only if it is false:-for data loader).
- After completing all trigger operation (insert new record, update old record) in vf controller call that method and pass both records details(newly inserted and updated old record)so that it will be called only once in a complete transaction that to at the end of the transaction for updated values of New and old record.
Questions:
- Is above solution feasible or does this has any issues which you think of?
- Is there any other best approach to follow?