Play Games

Search This Blog

Monday, October 25, 2021

How to lock sobject records in Apex - Salesforce Globe For You

How to lock sobject records in Apex - Salesforce Globe For You

Solution: Use FOR UPDATE keyword in inline SOQL queries to lock sObject records.

Example: Select id,name from Lead Limit 1 FOR UPDATE

Sample Code:

List<Lead> lstLead = new List<Lead>();

for(Lead objLead : [Select Id,lastName from Lead Limit 1 FOR UPDATE]) {

objLead.lastName = 'Lead '+objLead.lastName;  

    lstLead.add(objLead);

}

if(!lstLead.isEMPTY()) {

update lstLead;

    system.debug('lstLead '+lstLead[0].lastName);

}

Output:



Note: You can’t use the ORDER BY keywords in  SOQL query where FOR UPDATE is used.

No comments:

Post a Comment