2013-07-02 88 views
0

我從查詢中獲取1561條記錄,現在我想在其他函數中傳遞記錄標識,並根據我們傳遞給此函數的特定標識檢索其他記錄System.LimitException:太多的SOQL查詢:101

我不明白,我可以使用地圖或其他方法來標識傳遞到其他功能,使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 ; 
    } 

}  
+0

在workflowChecker中會發生什麼?嘗試設計該功能,以便可以根據批量接收的數據處理其邏輯。 –

回答

3

你不能做SOQL查詢的「for」循環,因爲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 ; 
    } 

} 
2

的Apex代碼應始終批量處理記住,寫手段你永遠不應該在循環內執行DML或SOQL。

這裏是一個很好的資源,我強烈建議你閱讀:Apex Code Best Practices

0

正如其他人所提到的,您需要避免在循環中調用執行查詢的方法。如果你是循環調用這個方法100次,你會超過你的限制。

另一種方法是考慮Etrac_Orders_ c和工作流程 _c之間的關係?它們之間是否存在查找關係或主從關係?如果是這樣,那麼在單個查詢中檢索所有這些數據會更加高效。

這是一個很好的資源,幫助您瞭解SOQL手柄如何加入/子查詢的/ etc:

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

我不明白你的數據模型足以知道它是否可以檢索所有在單個查詢中的相關記錄,但值得一看。我認爲Shimshon正在試圖在他的回答中展示這種類型的解決方案。

Andrew