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
swethasfdcswethasfdc 

Locking syntax that prevents record update conflicts

Can anyone explain about the locking syntax in a trigger with an example that prevents record update conflicts???

 

Thank you,

swetha.

 

Navatar_DbSupNavatar_DbSup

Hi,

 

You can create condition in trigger .if the condition will not meets your criteria then generate the error message.

 

For example refer the below code trigger on account to prevent the changing of the account name

 

trigger duplicateaccount on Account (Before upadate)

{

    if(trigger.isupdate)

                {

                                if(trigger.new[0].name!=trigger.old[0].name)

                                {

                                                trigger.new[0].name.addError('you can not change the account name');

                                }

                }

   

}

 

Did this answer your question? If not, let me know what didn't work, or if so, please mark it solved. 

 

 

 

 

Andy BoettcherAndy Boettcher

If I'm hearing you right - you're talking more about DBMS record update collisions (row locking, etc)?

 

Two approaches:

 

1)  Standard SF interface - there is no record locking.  If Jane views the details of a record and begins editing...and in the meantime Tom goes in and performs a quick inline update....Jane will get an error when she saves.  There is no locking - first to update wins...the loser gets a "stale record" error.

 

2)  Apex / Visualforce - you CAN lock a record if you're going the custom route.  In your SOQL statement, you can designate the records being retrieved as LOCKED for update.  As with any DBMS - use caution when you're choosing to lock records.

 

http://www.salesforce.com/us/developer/docs/apexcode/Content/langCon_apex_locking_statements.htm

 

SOQL Example:

Account [] accts = [SELECT Id FROM Account LIMIT 2 FOR UPDATE];

 

Does that help?

 

-Andy