function readOnly(count){ }
Starting November 20, the site will be set to read-only. On December 4, 2023,
forum discussions will move to the Trailblazer Community.
+ Start a Discussion
CarlBCarlB 

Concurrent trigger execution

Hello.

Is it possible for a trigger to run more than once concurrently?
 
trigger AfterInsert on A__c (after insert) {

	    Set<Id> ids = new Set<Id>();

	    for (A__c a : Trigger.new) {
	        if (a.Link__c!=null) {
	            ids.add(a.Link__c);
	        }
	    }

	    Map<Id, B__c> bs = new Map<Id, B__c>([ Select id, total__c, number__c From B__c Where id in :ids]);
	    for (A__c a : Trigger.new) {
	        B__c b = bs.get(a.Link__c);
	        b.Number__c++;
	        b.Total__c = b.Total__c + a.Amount__c;
	    }
	 
	    update bs.values();
}
In the above example, after I insert an A__c record, the above trigger runs and looks for the B__c record that the A__c record was linked to and updates some fields in it.

If I insert two A__c records very close together which are both linked to the same B__c record, is it possible that the above trigger could run twice concurrently, once processing the first record and once processing the second record, so the change to the B__c record made by the first trigger run is overwritten by the second trigger run?

If so, should I actually select B__c FOR UPDATE, to prevent this happening?

Regards,

Carl

 
NagaNaga (Salesforce Developers) 
Hi Carl,

1. Is it possible for the execute() method to be running more than once at the same time? (I've already confirmed that the batch job was launched only once.)

A:
All execute methods for batches within a batch job are synchronous so they will not "trip" over each other.

2. If the only Apex code that touches these records is in the execute() method, what else might be responsible for this error?

A:

Possible actions which execute outside of the scope of transaction for the batch operation.

: rollup summary fields This is a common occurrence

: Time based workflow which execute outside of the scope of transaction for the batch operation. This is not common.

For the rollup summary fields, a good practice to avoid this is to process the records grouped by the rollup parent record.

Also, an additional good practice is to first try an upsert in a try catch block. If you receive an error, reissue the upsert using the database method and then loop through the results to determine which records failed. You can either save the id's for later processing or logging of failure.

Please let me know if this helps

Best Regards
Naga Kiran
CarlBCarlB
Hello.

I think you mis-read my question - this is not about batches, it is about triggers.

Also, the reason that I am not using a rollup summary field is that there is not a master-detail relationship between the two objects.

Regards,

Carl
 
Nitin PaliwalNitin Paliwal
Hi,
I think if you try to update the B__c record again, you would get "UNABLE_TO_LOCK_ROW, unable to obtain exclusive access to this record" error.
So I am certain that your record wouldnt be updated second time and the insert operation of second A__c would fail.

Thanks
Nitin