Asynchronous Apex 04

12930 ワード

🚀 Control Processes with Queueable Apex


スタンバイ電源付きプロセス制御

📚 Learning Objectives

  • When to use the Queueable interface
    いつでも利用可能(?)
  • インタフェース
  • The differences between queueable and future methods
    差異:
  • スタンバイおよび将来のアプローチ
  • Queueable Apex syntax
    クエリ可能なApex構文
  • Queueable method best practices
    クエリ可能メソッドベストエクササイズ
  • 📄 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

  • キュー可能:待機可能(?これは何の言葉ですか?)