Created
March 9, 2016 08:46
-
-
Save tyoshikawa1106/e3749604467144a1deb1 to your computer and use it in GitHub Desktop.
Queueableサンプル
This file contains hidden or 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
List<Account> accounts = [select id from account limit 5]; | |
Id parentId = [select id from account where id not in: accounts limit 1][0].Id; | |
// instantiate a new instance of the Queueable class | |
UpdateParentAccount updateJob = new UpdateParentAccount(accounts, parentId); | |
// enqueue the job for processing | |
ID jobID = System.enqueueJob(updateJob); |
This file contains hidden or 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 UpdateParentAccount implements Queueable { | |
private List<Account> accounts; | |
private ID parent; | |
public UpdateParentAccount(List<Account> records, ID id) { | |
this.accounts = records; | |
this.parent = id; | |
} | |
public void execute(QueueableContext context) { | |
for (Account account : accounts) { | |
account.parentId = parent; | |
// perform other processing or callout | |
} | |
update accounts; | |
} | |
} |
This file contains hidden or 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
@isTest | |
public class UpdateParentAccountTest { | |
@testSetup | |
static void setup() { | |
List<Account> accounts = new List<Account>(); | |
// add a parent account | |
accounts.add(new Account(name='Parent')); | |
// add 100 child accounts | |
for (Integer i = 0; i < 100; i++) { | |
accounts.add(new Account( | |
name='Test Account'+i | |
)); | |
} | |
insert accounts; | |
} | |
static testmethod void testQueueable() { | |
// query for test data to pass to queueable class | |
Id parentId = [select id from account where name = 'Parent'][0].Id; | |
List<Account> accounts = [select id, name from account where name like 'Test Account%']; | |
// Create our Queueable instance | |
UpdateParentAccount updater = new UpdateParentAccount(accounts, parentId); | |
// startTest/stopTest block to force async processes to run | |
Test.startTest(); | |
System.enqueueJob(updater); | |
Test.stopTest(); | |
// Validate the job ran. Check if record have correct parentId now | |
System.assertEquals(100, [select count() from account where parentId = :parentId]); | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment