Skip to content

Instantly share code, notes, and snippets.

@brianmfear
Created June 13, 2016 17:58
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save brianmfear/4a5dc0a08de81c57961be1f9da8c7751 to your computer and use it in GitHub Desktop.
Save brianmfear/4a5dc0a08de81c57961be1f9da8c7751 to your computer and use it in GitHub Desktop.
Example Lock by Code
trigger Obj1 on Obj1 (after insert, after update) {
Obj1Trigger.handle(Trigger.new);
}
public class Obj1Trigger {
public static Boolean enforceLock = true;
public static void handle(Obj1[] records) {
if(enforceLock) {
Map<Id, Obj11> parents = new Map<Id, Obj11>(
[SELECT Lock__c FROM Obj11 WHERE Id IN (SELECT Obj11__c FROM Obj1 WHERE Id IN :records)
);
for(Obj1 record: records) {
if(parents.get(record.Obj11__c).Lock__c) {
record.addError('You cannot edit this record while the parent is locked.');
}
}
}
}
global class SomeBatchableUpdate implements Database.Batchable<Obj1> {
global Database.QueryLocator start(Database.BatchableContext context) {
return Database.getQueryLocator([SELECT ... FROM Obj1 WHERE ...]);
}
global void execute(Database.BatchableContext context, Obj1[] records) {
Obj1Trigger.enforceLock = false;
for(Obj1 record: records) {
// Do stuff
}
update records;
}
global void finish(Database.BatchableContext context) {
}
}
@Debasmita-Nandi
Copy link

thanks a lot @brianmfear

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment