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
sf_evolutionsf_evolution 

Serialized update request

Something weird is happening...

I have a custom object that has a single integer counter field that's updated when a lead is inserted.

 

If records are inserted rapidly all records are inserted, but the integer counter is not updated.

This isn't the actual code, but it's indicative of what I'm trying to accomplish;

 

trigger leadAssign on Lead (before insert, after insert) {

     if (Trigger.isBefore && Trigger.isInsert) {
        for (Lead l : System.Trigger.new) {   // build keyset of all emails in lead(s)
	    CustomObject.Last_Assign__c++;
	    update CustomObject;
	    break;
	    }
	}
} // end of ATL Assignment
 

 

If I insert one lead at a time via the API it work fine, but if I stack up the requests asychronously, CustomObject.Last_Assign__c doesn't get updated... 

 

Any help is greatly appreciated.

 

 

 

SuperfellSuperfell

presumably somewhere in the code you didn't post you read the custom object row ? so when there are concurrent requests you have multiple threads doing

read x

x = x + 1

update x

 

if both threads read x at the same time, then you have 2 request but x has only gone up by one, you'll need to lock your custom object row when you read it (using FOR UPDATE in the query), this will of course impact performance when you do have concurrent inserts going on, as now everything is serialed on that custom object row lock.

kevindotcarkevindotcar

Thank you for the insight Simon.

 

If I may ask, 

 

1.   If I put the counter in custom settings would that work?  ie;  are custom settings automatically serialized?

 

2.  I must profess ignorance; if I don't actually updtate the record, will the "FOR UPDATE" lock be released when I exit the trigger?

 

Thanks again