Asynchronous Processing Basics || Use Future Methods
生活随笔
收集整理的這篇文章主要介紹了
Asynchronous Processing Basics || Use Future Methods
小編覺得挺不錯的,現(xiàn)在分享給大家,幫大家做個參考.
異步處理的主要優(yōu)點包括:
用戶效率
可擴展性
更高的限制
Use Future Methods
Future methods?
?必須是靜態(tài)方法,并且只能返回void類型。
指定的參數(shù)必須是原始數(shù)據(jù)類型,原始數(shù)據(jù)類型的數(shù)組或原始數(shù)據(jù)類型的集合
不能將標準或自定義對象作為參數(shù)
一種常見的模式是向該方法傳遞要異步處理的記錄ID列表
Create an Apex class that uses the @future annotation to update Account records
AccountProcessor.apxc
public class AccountProcessor {@futurepublic static void countContacts(Set<id> setId){List<Account> lstAccount = [select Number_of_Contacts__c,(select id from contacts ) from account where id in :setId ];for( Account acc : lstAccount ){List<Contact> lstCont = acc.contacts ;acc.Number_of_Contacts__c = lstCont.size();}update lstAccount;} }AccountProcessorTest.apxc
@IsTest public class AccountProcessorTest {public static testmethod void TestAccountProcessorTest(){Account a = new Account(name='Test Account');Insert a;Contact cont = New Contact();cont.FirstName ='Bob';cont.LastName ='Masters';cont.AccountId = a.Id;Insert cont;set<ID> setAccId = new Set<ID>();setAccId.add(a.id);Test.startTest();AccountProcessor.countContacts(setAccId);Test.stopTest();Account ACC = [select Number_of_Contacts__c from Account where id = :a.id LIMIT 1];System.assertEquals ( Integer.valueOf(ACC.Number_of_Contacts__c) ,1);}}?
總結(jié)
以上是生活随笔為你收集整理的Asynchronous Processing Basics || Use Future Methods的全部內(nèi)容,希望文章能夠幫你解決所遇到的問題。
- 上一篇: Get Started with Ape
- 下一篇: Use Batch Apex