Play Games

Search This Blog

Sunday, September 6, 2020

How to lock a salesforce record or records in Apex? - Salesforce Globe For You

How to lock a salesforce record or records in Apex?  - Salesforce Globe For You 

Solution: Use 'FOR Update' keyword after the SOQL query to lock the records.

Example:If you want to lock the records returned by the following query

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

lstLead = [Select id,name from Lead];

then modify the query as shown below

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

lstLead = [Select id,name from Lead For UPDATE];

When records are locked,no other user is allowed to update this records until the lock gets released.The lock automatically gets released after transaction completes.

Note:You cannot use 'Order By' when you are using 'FOR UPDATE' in SOQL query.

No comments:

Post a Comment