Asynchronous Apex 04

Jaehyun_Banยท2022๋…„ 4์›” 18์ผ
0

๐Ÿš€ Control Processes with Queueable Apex

ํ”„๋กœ์„ธ์Šค ์ œ์–ด with ๋Œ€๊ธฐ ๊ฐ€๋Šฅํ•œ Apex

๐Ÿ“š Learning Objectives

  • When to use the Queueable interface
    ์–ธ์ œ ์‚ฌ์šฉํ•˜๋Š”๊ฐ€ ๋Œ€๊ธฐ๊ฐ€๋Šฅํ•œ(?) ์ธํ„ฐํŽ˜์ด์Šค๋ฅผ
  • The differences between queueable and future methods
    ์ฐจ์ด์  - ๋Œ€๊ธฐ๊ฐ€๋Šฅํ•จ๊ณผ future ๋ฉ”์„œ๋“œ์˜
  • Queueable Apex syntax
    Queueable Apex์˜ ๋ฌธ๋ฒ•
  • Queueable method best practices
    Queueable ๋ฉ”์„œ๋“œ ์ตœ๊ณ ์˜ ์—ฐ์Šต

๐Ÿ“„ Queueable Apex


๐ŸŽฏ Challenge

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')]);
    }
    
    
}

๐Ÿค” Words

  • Queueable: ๋Œ€๊ธฐ๊ฐ€๋Šฅํ•œ(? ์ด๊ฒŒ ๋ฌด์Šจ๋‹จ์–ด๋žŒ???)

0๊ฐœ์˜ ๋Œ“๊ธ€