Skip to content

Instantly share code, notes, and snippets.

@tyoshikawa1106
Created October 27, 2015 13:53
Show Gist options
  • Save tyoshikawa1106/6e1400faaff43fe8ac83 to your computer and use it in GitHub Desktop.
Save tyoshikawa1106/6e1400faaff43fe8ac83 to your computer and use it in GitHub Desktop.
非同期処理をつかったApexのレコードロック解除
trigger AccountTrigger on Account (after update) {
private AccountTriggerHandler handler = new AccountTriggerHandler();
if (Trigger.isAfter) {
if (Trigger.isUpdate) {
handler.doUnlock(Trigger.new, Trigger.oldMap);
}
}
}
public class AccountTriggerHandler {
public AccountTriggerHandler() {
}
public void doUnlock(List<Account> newAccounts, Map<Id, Account> oldAccountMap) {
// 対象の取引先を取得
List<Account> accounts = new List<Account>();
List<ID> accountIds = new List<ID>();
for (Account a : newAccounts) {
System.debug('New Account IsUnlock = ' + a.IsUnlock__c);
System.debug('Old Account IsUnlock = ' + oldAccountMap.get(a.Id).IsUnlock__c);
if (a.IsUnlock__c == true && !oldAccountMap.get(a.Id).IsUnlock__c) {
accounts.add(a);
accountIds.add(a.Id);
}
}
System.debug('Account Size = ' + accounts.size());
AccountTriggerHandler.doUnlockByFuture(accountIds);
}
@future
public static void doUnlockByFuture(List<ID> accountIds) {
// Unlock操作
List<Account> accounts = [SELECT Id FROM Account WHERE ID IN: accountIds];
Approval.UnLockResult[] results = Approval.unlock(accounts, false);
System.debug('非同期処理によるロック解除操作の対象件数 = ' + results.size());
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment