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

 

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值