+ Start a Discussion
Apple88Apple88 

How to impose concurrent update control in controller extension?

Hi,

 When two users use the standard Edit page to edit the same record of a custom object at the same time, Force.com does control the concurrent update properly and display the error message as below when the second user tries to save his changes: 
Your Changes Cannot Be Saved
The record you were editing was modified by Apple88 during your edit session.

Please re-display the record before editing again.
However, when I built a custom Visualforce page and override the standard Edit button of the custom object with the custom page, the custom page failed to manage the concurrent update properly.  Both users are able to retrieve and edit the same record at the same time, and able to save their changes successfully.  As the result, the changes made by the second user overwrite the changes of the first user without knowing. Below is my custom page: <apex:page standardController="Customer__c" extensions="CustomerEditCX"><apex:variable var="CS" value="{!Customer__c}"/><apex:sectionHeader title="Customer Edit" subtitle="{!CS.Name}"/><apex:form ><apex:pageMessages ></apex:pageMessages><apex:pageBlock mode="edit"><apex:pageBlockButtons ><apex:commandButton value="Save" action="{!save}"/><apex:commandButton value="Cancel" action="{!cancel}"/></apex:pageBlockButtons><apex:pageBlockSection ><apex:inputField value="{!CS.Name}" required="true"/><apex:outputField value="{!CS.Status__c}"/><apex:inputField value="{!CS.Number__c}"/><apex:inputField value="{!CS.Owner__c}" required="true"/></apex:pageBlockSection><apex:pageBlockSection title="Contact Details"><apex:inputField value="{!CS.Contact_Person__c}"/><apex:inputField value="{!CS.Contact_Phone__c}"/><apex:inputField value="{!CS.Fax_Number__c}"/><apex:inputField value="{!CS.Mobile_Phone__c}"/><apex:inputField value="{!CS.Corresponding_Address__c}"/><apex:inputField value="{!CS.Email_Address__c}"/></apex:pageBlockSection></apex:pageBlock></apex:form></apex:page> I have also tried to add a SOQL with “for update” option in the controller extension to lock the record.  However, it still failed to control the concurrency update properly.  Below is the controller extension: public with sharing Class CustomerEditCX {    Customer__c CS = new Customer__c();    public CustomerEditCX(ApexPages.StandardController StdController) {        CS = (Customer__c)Stdcontroller.getRecord();        if (CS.ID != Null) {            CS = [select Owner__c, Status__c from Customer__c                  where ID = :CS.ID for update];            system.debug('Supposed lock successfully: ' + CS.ID);             }                  if (CS.Status__c == Null) {            CS.Status__c = 'Draft';        }          }} Would anyone let me know how to fix this problem? Thanks for your help in advance. Apple88
bob_buzzardbob_buzzard

I'd assume that the code that is backing the standard pages is checking that the page has not been updated before applying the changes, so you just need to add something similar.

 

It should just be a case of capturing the last modified date when the record is retrieved for editing and when a user clicks save, execute a soql query to see if the database has a later version of the record.  If it does, you can then block the save.