Created
June 13, 2016 17:58
-
-
Save brianmfear/4a5dc0a08de81c57961be1f9da8c7751 to your computer and use it in GitHub Desktop.
Example Lock by Code
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
trigger Obj1 on Obj1 (after insert, after update) { | |
Obj1Trigger.handle(Trigger.new); | |
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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.'); | |
} | |
} | |
} | |
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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) { | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
thanks a lot @brianmfear