Created
October 27, 2015 13:53
-
-
Save tyoshikawa1106/6e1400faaff43fe8ac83 to your computer and use it in GitHub Desktop.
非同期処理をつかったApexのレコードロック解除
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 AccountTrigger on Account (after update) { | |
private AccountTriggerHandler handler = new AccountTriggerHandler(); | |
if (Trigger.isAfter) { | |
if (Trigger.isUpdate) { | |
handler.doUnlock(Trigger.new, Trigger.oldMap); | |
} | |
} | |
} |
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 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