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
}
}