Warning: file_get_contents(/data/phpspider/zhask/data//catemap/2/joomla/2.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Triggers System.LimitException:太多SOQL查询:101数据加载程序问题_Triggers_Salesforce_Apex - Fatal编程技术网

Triggers System.LimitException:太多SOQL查询:101数据加载程序问题

Triggers System.LimitException:太多SOQL查询:101数据加载程序问题,triggers,salesforce,apex,Triggers,Salesforce,Apex,我在尝试加载8000条记录时遇到了dataloader问题,该记录触发了以下触发器,从而导致系统出错。LimitException:太多SOQL查询:101正是由于此问题 trigger BeforeTaskTrigger on Task (after insert, after update) { for(Task s : Trigger.new) { if ((s.Type == 'QRC')&&(s.Status=='Completed')

我在尝试加载8000条记录时遇到了dataloader问题,该记录触发了以下触发器,从而导致系统出错。LimitException:太多SOQL查询:101正是由于此问题

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);

      }


      }
  }
trigger BeforeTaskTrigger on Task(插入后、更新后){
for(任务s:Trigger.new)
{
如果((s.Type=='QRC')&&(s.Status=='Completed')){
BusinessLogic.processUpdateInsertTask;
}
}
}
公共静态void processUpdateInsertTask(个任务){
List itemList=[从任务中选择Id、类型、状态、ActivityDate、OwnerId,其中accountId=:s.accountId和状态='Completed'和类型='QRC'Order By ActivityDate Desc];
List eventList=[从事件中选择Id、类型、状态、活动日期、所有者Id、endDateTime,其中accountId=:s.accountId和状态c='Completed'并按endDateTime Desc键入='QRC'顺序];
List accountData=[从Id=:s.AccountId的帐户中选择Id、最后一个日期、最后一个日期];
如果((accountData!=null)&&(accountData.size()>0)){
日期事件日期;
if(eventList!=null&&eventList.size()>0){
eventDate=date.newinstance(eventList.get(0.endDateTime.year()、eventList.get(0.endDateTime.month()、eventList.get(0.endDateTime.day());
}
如果((itemList!=null)&&&(itemList.size()>0)&&(eventList!=null)&&(eventList.size()>0)){
if(itemList.get(0.ActivityDate>=eventDate){
accountData.get(0).Last\u QRC\u Date\uu c=itemList.get(0).ActivityDate;
accountData.get(0).Last\u QRC\u FA\u c=itemList.get(0).OwnerId;
更新会计数据;
}
否则{
accountData.get(0).Last_QRC_Date_uc=eventDate;
accountData.get(0).Last\u QRC\u FA\u c=eventList.get(0).OwnerId;
更新会计数据;
}
}
else如果((itemList!=null)&&(itemList.size()>0)){
processTaskSpecialCases(accountData、itemList);
}
else如果((eventList!=null)和(&(eventList.size()>0)){
processEventSpecialCases(accountData、eventDate、eventList);
}
否则{
processDeletionCases(accountData);
}
}
}

如果您能帮助我重新表述SOQL查询以提高效率,我将非常高兴。

您需要将填充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);
}
//获取所有帐户ID
List accountIds=新列表();
for(任务t:Trigger.new)
{
AccountId.add(t.AccountId);
}
Map taskMap=新映射();//由AccountId键入
对于(任务t:[从任务中选择Id、AccountId、类型、状态、ActivityDate、OwnerId,其中AccountId=:AccountId和状态='Completed'和类型='QRC'Order By ActivityDate Desc])
{
列表任务=新列表();
if(taskMap.containsKey(t.AccountId))
{
tasks=taskMap.get(t.AccountId);
}
任务。添加(t);
taskMap.put(t.AccountId,tasks);
}
这个基于上面itemList的示例为您获取一个映射,该映射由属于该帐户的所有任务的帐户ID键入。当您需要引用该列表时,只需输入映射并获取值(最重要的是,它只算作一个SOQL查询,您可以在整个触发器中使用)

看看下面的例子,扩展代码是开发可伸缩Salesforce应用程序的一个重要部分