ํ๋ก์ธ์ค ์ ์ด with ๋๊ธฐ ๊ฐ๋ฅํ Apex
AddPrimaryContact
public class AddPrimaryContact implements Queueable {
private Contact con;
private String state;
//์์ฑ์ ์์ฑ
public AddPrimaryContact(Contact con, String state) {
this.con = con;
this.state = state;
}
//Queueable ์คํ์ฅ์
public void execute(QueueableContext context) {
LIST<Account> accounts = [Select Id, Name, (Select FirstName, LastName, Id From contacts)
From Account Where BillingState = :state Limit 200];
LIST<Contact> primaryContacts = new LIST<Contact>();
for(Account acc : accounts){
Contact c = con.clone();
c.AccountId = acc.Id;
primaryContacts.add(c);
}
if(primaryContacts.size() > 0){
insert primaryContacts;
}
}
}
AddPrimaryContactTest
@isTest
public class AddPrimaryContactTest {
static testmethod void testQueueable(){
LIST<Account> testAccounts = new LIST<Account>();
for(Integer i = 0; i < 50; i++){
testAccounts.add(new Account(Name = 'Account ' + i, BillingState = 'CA'));
}
for(Integer j = 0; j < 50; j++){
testAccounts.add(new Account(Name = 'Account ' + j, BillingState = 'CA'));
}
insert testAccounts;
Contact testContact = new Contact(FirstName = 'John', LastName = 'Doe');
insert testContact;
AddPrimaryContact addit = new addPrimaryContact(testContact, 'CA');
Test.startTest();
System.enqueueJob(addit);
Test.stopTest();
System.assertEquals(50, [Select count() From Contact Where accountId in
(Select Id From Account Where BillingState = 'CA')]);
}
}