我在删除触发器之前运行 Apex 作业,并注意到通过 Salesforce 界面删除单个营销活动大约需要 34 秒,并且当营销活动被删除时,Apex 作业已完成运行。当我使用 VisualForce 页面在相同数量的记录 (15,000) 上运行相同的 Apex 作业时,Apex 作业会排队并在 VisualForce 页面完成后运行。
在此屏幕截图中,第一个作业使用 VisualForce 排队,第二个和第三个作业使用触发器排队:
如您所见,虽然相同数量的记录 (15,000) 在使用触发器排队时由相同的 Apex 作业处理,但没有创建批次,提交和完成日期完全不同。
Apex 作业在触发器中排队:
trigger DeleteChilds on Campaign (before delete) {
public static final Integer MAX_DELETED = 500;
Set<ID> ids = new Set<Id>();
List<Parent__c> objs = new List<Parent__c>();
for(Campaign c : Trigger.old)
ids.add(c.Id);
objs = [SELECT Id FROM Parent__c WHERE Campaign__c IN :ids];
ids = new Set<Id>();
for(Parent__c s : objs)
ids.add(s.Id);
Integer childCount = [SELECT count() FROM Childs__c WHERE Parent__c IN :ids];
if(childCount < MAX_DELETED){
List<Childs__c> childs = [SELECT Id FROM Childs__c WHERE Parent__c IN :ids];
delete childs;
} else {
String deleteQuery = 'SELECT Id FROM Childs__c WHERE ';
for(ID id : ids){
deleteQuery += String.format(' Parent__c = \'\'{0}\'\' OR', new String[] {id});
}
if(deleteQuery.endsWith('OR')){
deleteQuery = deleteQuery.substring(0, deleteQuery.length() - 2);
}
BatchDeleteChilds batch = new BatchDeleteChilds();
batch.deleteQuery = deleteQuery;
ID batchId = Database.executeBatch(batch);
}
delete objs;
}
这是 Apex 作业:
global class BatchDeleteChilds implements Database.Batchable<sObject> {
public String deleteQuery;
global Database.QueryLocator start(Database.BatchableContext context){
return Database.getQueryLocator(deleteQuery);
}
global void execute(Database.BatchableContext context, List<sObject> records){
delete records;
DataBase.emptyRecycleBin(records);
}
global void finish(Database.BatchableContext context){
}
}
有没有其他人经历过这样的事情?
谢谢,