0
votes

I fetch 1561 records from a query and now i want to pass the record ID in other function where it retrieve other records on the basis of Particular Id which we pass to this function

I'm not understand that i can use map or other methods to pass Id to an other function so the salesforce limitation not be effected

I also share my small piece of code:

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
What happens in workflowChecker .. ? Try designing that functionality in a way it can process its logic based on data received in bulk. - Samuel DR

3 Answers

3
votes

You can not do SOQL query in a "for" loop, because of SOQL queries execution Governors, change the code into something like this

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
votes

Apex Code should always be written with bulk processing in mind, this means that you should never perform DML or SOQL inside of loops.

Here is a good resource that I strongly suggest you read: Apex Code Best Practices.

0
votes

As everyone else mentioned, you need to avoid calling, in a loop, a method that performs a query. If you're loop calls that method 100 times, you'll run over your limit.

Another approach is to consider the relationship between Etrac_Orders_c and Workflow_c? Is there a lookup relationship between them or a master-detail relationship? If so, it would be even more efficient to retrieve all of this data in a single query.

Here is an excellent resource to help you understand how SOQL handles joins/sub-queries/etc:

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

I don't understand your data model enough to know if it's possible to retrieve all the related records in a single query, but it's worth a look. I think Shimshon is attempting to show an example of this type of solution in his answer.

Andrew