0

我在尝试加载 8,000 条记录时遇到数据加载器问题,该记录触发以下触发器,给我一个错误 System.LimitException: Too many SOQL queries: 101 because of this issue ..

trigger BeforeTaskTrigger on Task (after insert, after update) {
    for(Task s : Trigger.new)
    {

        if ((s.Type == 'QRC')&&(s.Status=='Completed')) {
                BusinessLogic.processUpdateInsertTask(s);
    }
}
}


public static void processUpdateInsertTask (Task s){
         List<Task> itemList = [select Id, Type, Status, ActivityDate, OwnerId from Task where accountId = :s.AccountId and status = 'Completed' and Type ='QRC' Order By ActivityDate Desc ];
     List<Event> eventList = [select Id, Type, Status__c, ActivityDate, OwnerId, endDateTime from Event where accountId = :s.AccountId and Status__c = 'Completed' and Type ='QRC' Order By endDateTime Desc ];

        List<Account> accountData = [Select Id, Last_QRC_Date__c, Last_QRC_FA__c from Account where Id = :s.AccountId];
        if ((accountData!=null)&&(accountData.size()>0)){
            Date eventDate;
            if (eventList != null && eventList.size()>0){
         eventDate = date.newinstance(eventList.get(0).endDateTime.year(), eventList.get(0).endDateTime.month(), eventList.get(0).endDateTime.day());
        }
        if ((itemList != null)&&(itemlist.size()>0)&&(eventList!=null)&&(eventList.size()>0)){

        if (itemList.get(0).ActivityDate >= eventDate){
                accountData.get(0).Last_QRC_Date__c = itemList.get(0).ActivityDate;
                accountData.get(0).Last_QRC_FA__c = itemList.get(0).OwnerId;
                                update accountData;

        }
      else {
                accountData.get(0).Last_QRC_Date__c = eventDate;
                accountData.get(0).Last_QRC_FA__c = eventList.get(0).OwnerId;
                                update accountData;

      }
     }
     else if ((itemList != null)&&(itemlist.size()>0)){
                   processTaskSpecialCases(accountData, itemList);

     }
     else if ((eventList!=null)&&(eventList.size()>0)){
            processEventSpecialCases(accountData, eventDate, eventList);


       }
          else {
            processDeletionCases (accountData);

      }


      }
  }

如果您能帮助我改写 SOQL 查询以提高效率,我会很高兴。

4

1 回答 1

2

您需要将填充 itemList 和 eventList 的查询移到for 循环之外。传统上,当您需要这样的信息时,您只需查询一次所需的所有信息,然后将其放入地图中以供以后查找。

例如:

// Get all the Account Ids
List<String> accountIds = new List<String>();
for (Task t : Trigger.new)
{
  accountIds.add(t.AccountId);
}

Map<String, List<Task>> taskMap = new Map<String, List<Task>>(); // keyed by AccountId
for (Task t : [select Id, AccountId, Type, Status, ActivityDate, OwnerId from Task where accountId = :accountIds and status = 'Completed' and Type ='QRC' Order By ActivityDate Desc ])
{
  List<Task> tasks = new List<Task>();
  if (taskMap.containsKey(t.AccountId))
  {
    tasks = taskMap.get(t.AccountId);
  }
  tasks.add(t);
  taskMap.put(t.AccountId, tasks);
}

这个基于上面 itemList 的示例为您提供了一个由属于该帐户的所有任务的帐户 ID 键入的 Map。当您需要引用该列表时,您只需键入映射并获取值(最重要的是,它只算作一个 SOQL 查询,您可以在整个触发器中使用)。

查看APEX 最佳实践,批量化您的代码是开发可扩展 Salesforce 应用程序的重要组成部分。

于 2014-01-14T01:20:02.740 回答