Bulkification
如果你在 Salesforce 中執行逐行處理,則可能很快就會達到調控器限制。對於觸發器和在你不期望它們時觸發的事物尤其如此。一種記錄的轉義州長限制的方法是批量化。
注意: 以下資訊基於官方 Salesforce 文件。
批量化 Apex 程式碼意味著確保程式碼一次正確處理多個記錄。當一批記錄啟動 Apex 時,將執行該 Apex 程式碼的單個例項,但該例項需要處理該給定批次中的所有記錄。
不整理:
trigger accountTestTrggr on Account (before insert, before update)
{
//This only handles the first record in the Trigger.new collection
//But if more than 1 Account initiated this trigger, those additional records
//will not be processed
Account acct = Trigger.new[0];
List<Contact> contacts = [select id, salutation, firstname, lastname, email
from Contact where accountId = :acct.Id];
}
Bulkified:
trigger accountTestTrggr on Account (before insert, before update)
{
List<String> accountNames = new List<String>{};
//Loop through all records in the Trigger.new collection
for(Account a: Trigger.new){
//Concatenate the Name and billingState into the Description field
a.Description = a.Name + ':' + a.BillingState
}
}