System.LimitException:SOQL查询太多:101

时间:2013-07-02 11:43:46

标签: salesforce apex-code

我从一个查询中获取1561条记录,现在我想在其他函数中传递记录ID,根据我们传递给此函数的特殊ID检索其他记录

我不明白我可以使用地图或其他方法将Id传递给其他功能,因此不会影响salesforce限制

我也分享了我的一小段代码:

SendUpdateEmailList = [SELECT Id,
                              Update72Hrs_email__c,
                              File_Number__c,
                              Loan_Number__c,
                              Client_ID__c,
                              Borrower_Name__c,
                              Order_Status__c,
                              Current_Status_Notes__c,
                              Time_Since_Last_Updated__c
                       FROM Etrac_Orders__c 
                       WHERE Order_Status__c!='Completed' 
                           AND Order_Status__c!='Cancelled' 
                           AND Order_Status__c!='Delayed by Borrower' 
                           AND Order_Status__c!='On Hold' 
                           AND Time_Since_Last_Updated__c > 72 
                           AND IsDeleted <> true ];
        system.debug('SendUpdateEmailList>>>>>>>>>>'+SendUpdateEmailList);

     //   map<id,string> myAMap = new map<id,string>();
    for ( Etrac_Orders__c c:SendUpdateEmailList ){
        myAMap.put(c.Id,c.File_Number__c);
    }

    for (ID aID : myAMap.keySet()){
        myAMap.get(aID);
        system.debug('<<<<<<<<<<aID'+aID);
        workFlowChecker(aID); //this statement creates the limitation problem
    }
 public void WorkFlowChecker(ID id) {
     system.debug('WorkFlowChecker-id>>>>>>>' + id);

     datetime systemtime = System.now();
     system.debug('systemtime>>>>>>>>>>>' + systemtime);
     //date mydate = Date.ValueOf();
     //datetime systemdate = dDate.format('MM/dd/yyyy hh: mm: ss a');
     try {
         workFlow = [SELECT Id,
                     Executed__c,
                     OrdersID__c,
                     Executed_Date__c,
                     WorkflowName__c From Workflow__c Where WorkflowName__c = 'Update72Hrs_email' AND OrdersID__c = : id Order by Executed_Date__c DESC Limit 1];

         system.debug('workFlow>>>>>>>' + workFlow);
         // system.debug('Executed_Date__c>>>>>>>>'+workFlow.Executed_Date__c);


         for (Workflow__c record : workFlow) {
             string WorkFlowId        = record.Id;
             Boolean Executed         = record.Executed__c;
             string OrdersID          = record.OrdersID__c;
             datetime Executed_Date   = record.Executed_Date__c;
         }
         return;

     } catch (QueryException e) {
         ApexPages.addMessage(new ApexPages.message(ApexPages.severity.ERROR, 'No Record Found'));
         return ;
     }

 }     

3 个答案:

答案 0 :(得分:3)

你不能在“for”循环中进行SOQL查询,因为SOQL查询执行调控器,将代码改为这样的

map<Id,Etrac_Orders__c > SendUpdateEmailMap = new map<Id,Etrac_Orders__c >([SELECT Id,
                              Update72Hrs_email__c,
                              File_Number__c,
                              Loan_Number__c,
                              Client_ID__c,
                              Borrower_Name__c,
                              Order_Status__c,
                              Current_Status_Notes__c,
                              Time_Since_Last_Updated__c,
                       (SELECT Id,
                     Executed__c,
                     OrdersID__c,
                     Executed_Date__c,
                     WorkflowName__c From " Child Relationship Name " Where WorkflowName__c = 'Update72Hrs_email' AND Order by Executed_Date__c DESC Limit 1)
                       FROM Etrac_Orders__c 
                       WHERE Order_Status__c!='Completed' 
                           AND Order_Status__c!='Cancelled' 
                           AND Order_Status__c!='Delayed by Borrower' 
                           AND Order_Status__c!='On Hold' 
                           AND Time_Since_Last_Updated__c > 72 
                           AND IsDeleted <> true ]);

for ( Etrac_Orders__c c:SendUpdateEmailMap.values()){
        myAMap.put(c.Id,c.File_Number__c);
    }

for (ID aID : myAMap.keySet()){
        myAMap.get(aID);
        system.debug('<<<<<<<<<<aID'+aID);
        workFlowChecker(aID,SendUpdateEmailMap.get(aID)." Child Relationship Name "); 
    }



public void WorkFlowChecker(ID id,Workflow__c workFlow) {
     system.debug('WorkFlowChecker-id>>>>>>>' + id);

     datetime systemtime = System.now();
     system.debug('systemtime>>>>>>>>>>>' + systemtime);
     //date mydate = Date.ValueOf();
     //datetime systemdate = dDate.format('MM/dd/yyyy hh: mm: ss a');
     try {
          system.debug('workFlow>>>>>>>' + workFlow);
         // system.debug('Executed_Date__c>>>>>>>>'+workFlow.Executed_Date__c);

             string WorkFlowId        = workFlow.Id;
             Boolean Executed         = workFlow.Executed__c;
             string OrdersID          = workFlow.OrdersID__c;
             datetime Executed_Date   = workFlow.Executed_Date__c;
         return;

     } catch (QueryException e) {
         ApexPages.addMessage(new ApexPages.message(ApexPages.severity.ERROR, 'No Record Found'));
         return ;
     }

 }  

答案 1 :(得分:2)

Apex Code应始终以批量处理的方式编写,这意味着您永远不应在循环内执行DML或SOQL。

我强烈建议您阅读以下资料:Apex Code Best Practices

答案 2 :(得分:0)

正如其他人提到的,您需要避免在循环中调用执行查询的方法。如果您循环调用该方法100次,则会超出限制。

另一种方法是考虑Etrac_Orders_ c和Workflow _c之间的关系?它们之间是否存在查找关系或主 - 细节关系?如果是这样,在单个查询中检索所有这些数据会更有效。

这是一个很好的资源,可以帮助您了解SOQL如何处理连接/子查询/等等:

http://wiki.developerforce.com/page/A_Deeper_look_at_SOQL_and_Relationship_Queries_on_Force.com

我不清楚您的数据模型是否足以知道是否可以在单个查询中检索所有相关记录,但值得一看。我认为Shimshon试图在他的回答中展示这种解决方案的一个例子。

安德鲁