Control Processes with Queueable Apex



We took the simplicity of future methods and the power of Batch Apex and mixed them together to form Queueable Apex

 It gives you a class structure that the platform serializes for you, a simplified interface without start and finish methods and even allows you to utilize more than just primitive arguments

 simple System.enqueueJob() method, which returns a job ID that you can monitor



Queueable Apex 的優點 









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


Queueable Apex的最佳功能之一是作業鏈接。

如果您需要按順序運行作業,那麼Queueable Apex可以使您的生活更加輕鬆。

要將一個工作鏈接到另一個工作,submit the second job from the execute() method of your queueable class. 



Create an Queueable Apex class that inserts Contacts for Accounts

AddPrimaryContact.apxc

public class AddPrimaryContact implements Queueable {
    
    private String st;
    private Contact primecontact;
    
    public AddPrimaryContact(Contact aContact, String aState) {
        this.st=aState;
        this.primecontact = aContact;
    }
    public void execute(QueueableContext context) {
        List<Account> accounts = [select name from account where billingstate=:st LIMIT 200];
        List<Contact> contacts = new List<Contact>();
        for (Account acc : accounts) {
            contact con=primecontact.clone(false,false,false,false);
            contacts.add(con);
        }
        insert contacts;
    }
}

AddPrimaryContactTest.apxc

@isTest
public class AddPrimaryContactTest {
    @testSetup 
    static void setup() {
        List<Account> accounts = new List<Account>();
        // add 50 NY account
        for (Integer i = 0; i < 50; i++) {
            accounts.add(new Account(Name='NY'+i, billingstate='NY'));
        }
        // add 50 CA account
        for (Integer j = 0; j < 50; j++) {
            accounts.add(new Account(Name='CA'+j, billingstate='CA'));
        }
        insert accounts;
    }
    static testmethod void testQueueable(){
        contact a=new contact(Lastname='mary', Firstname='rose'); 
        Test.startTest();        
        AddPrimaryContact updater=new AddPrimaryContact(a, 'CA');
        System.enqueueJob(updater);
        Test.stopTest(); 
        
        System.assertEquals(50, [SELECT count() FROM Contact WHERE Lastname='mary' AND Firstname='rose']) ;                      
    }   
}

 

發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.
相關文章