5-1-4-10. Exclusive lock at data renewal

An error message similar to standard page layout will show when the data is renewed (=clicking save button) by other user.

As of today (10/03/2015), the difference between the standard page layout is as below:

  • Data will be re-written if it is renewed by the same user
  • Show message at the upper section of the existing page.

Apex class for extension page is Exclusive.

Apex class for extending page : Exclusive

global with sharing class ExclusiveExtender extends SkyEditor2.Extender{
    Exclusive extension;  // ApexClass
    String sobjectName;   // Main Object
    String recordId;      // Main Object Id
    Datetime updateDate;  //MainObject last updated time.
    public ExclusiveExtender(Exclusive extension){
        this.extension = extension;
        //Add SystemModStamp to the Query
        extension.mainQuery.addField('SystemModStamp');
    }
    global override void init() {
        sobjectName = extension.mainSObjectType.getDescribe().getName();
        recordId = extension.mainRecord.Id;
        if (recordId != null) {
            updateDate = (Datetime)extension.mainRecord.get('SystemModStamp');
        }
    }
    global override void preSave() {
        if (recordId != null) {
            // Before saving, obtain record info
            SObject[] modRecord = Database.query('SELECT Id, SystemModStamp, LastModifiedById, LastModifiedBy.Name FROM ' + sobjectName + ' WHERE Id = :recordId');
            // If record has been deleted
            if (modRecord.isEmpty()) {
                return;
            // If record has been changed
            } else if ((DateTime)modRecord[0].get('SystemModStamp') > updateDate) {
                String lastModId  = (String)modRecord[0].get('LastModifiedById');
                String lastModName = (String)modRecord[0].getSObject('LastModifiedBy').get('Name');
                throw new SkyEditor2.ExtenderException('Your Changes Cannot Be Saved<BR>The record you were editing was modified by <a href="/' + lastModId + ' " target="_blank">' + lastModName + '</a> during your edit session.<BR><BR>Please <a href="/' + recordId + '">re-display the record</a> before editing again.');
            }
        }
    }
}


*Test Class sample of the Account Main Object

@isTest
private class ExclusiveExtenderTest{
    private static testMethod void ExclusiveExtenderTest() {
        // Create Main Object
        Account obj = new Account(Name='Test');
        insert obj;
        Exclusive extension = new Exclusive(new ApexPages.StandardController(obj));
        // If the data is modified
        update obj;
        PageReference page = extension.doSave();
        System.assert(SkyEditor2.Messages.hasErrorMessage());
        // If the data is deleted
        delete obj;
        page = extension.doSave();
        System.assert(true);
    }
}