function readOnly(count){ }
Starting November 20, the site will be set to read-only. On December 4, 2023,
forum discussions will move to the Trailblazer Community.
+ Start a Discussion
rtyanasrtyanas 

Number of SOQL queries: 101 out of 100 ******* CLOSE TO LIMIT

Close is enough to stop my code.

I need help with my Apex code, I am building a list to display to the customer using the only way I know how. 
Creating a list of high level data then building the child records within each node; this is working fine with a lesser number of records.

 

    public class API_DocumentRqAndRs {
        public API_Document__c apiDoc{get; set;}
        public List<Rq_Argument_Description__c> rqArgList{get; set;}
        public List<Rs_Argument_Description__c> rsArgList{get; set;}
    }

    public List<API_DocumentRqAndRs> getApiDocumentListRqAndRs(){
        List<API_DocumentRqAndRs> apiDocRqRsList = new List<API_DocumentRqAndRs>() ;
// Build master list using this list List<API_Document__c> apiList = getApiDocumentList(); API_DocumentRqAndRs apiDocRqRs; for(API_Document__c apiDoc : apiList) { apiDocRqRs = new API_DocumentReqAndResp(); apiDocRqRs.apiDoc = apiDoc;
// Sets the key to query on fApiController.setapiDocId(apiDoc.id);
// This returns a query apiDocRqRs.rqArgList = fApiController.getRqArg();
// This returns a query apiDocRqRs.rsArgList = fApiController.getRsArg(); apiDocRqRsList.add(apiDocReqResp); } return apiDocRqRsList; }

 

 

Is there a way to increase my number of queries?   I have put much time into getting to this point and need it to work without a major rewrite.  Unless someone has a good solution?

 

Please help!  Thanks.

 

 

 

Suresh RaghuramSuresh Raghuram
for(API_Document__c apiDoc : apiList) {
            apiDocRqRs = new API_DocumentReqAndResp();
            apiDocRqRs.apiDoc = apiDoc;
// Sets the key to query on fApiController.setapiDocId(apiDoc.id);
// This returns a query apiDocRqRs.rqArgList = fApiController.getRqArg();
// This returns a query apiDocRqRs.rsArgList = fApiController.getRsArg(); apiDocRqRsList.add(apiDocReqResp); }
As you said in the comments that is returning the query.
If any query is written in the for loop it will hit the governor limits quickly.
avoid it.


rtyanasrtyanas

Are there any suggestions?  I put time into this solution and out of ideas as to a different solution.  The list I am using is straight forward and works nicely except for the limit.

 

Is this 100 query limit realistic?  The user will not reach this limit often and the solution is simple.

 

I am trying to retreive child records for each document record.  What is the difference is I use a loop or some other way?  I still must have the same set of records for each document!

 

Please help with solution.

 

Sean TanSean Tan

100 queries is quite realistic. You have to keep in mind Apex code is run in a multitenant environment so the governor limits are there to ensure you don't consume to many resources all at once.

 

To your question on how to optimize this, we would need a little more detail on the under pinnings of what your code is doing in the loop (e.g. the internal class queries and such). Here is a very basic example of right and wrong:

 

Wrong:

 

for (Account a : accountList)
{
    Contact[] contactList = [ SELECT Id from Contact WHERE AccountId = :a.Id ];
    //Do stuff with contacts for that account
}

 Right:

Set<Id> accountIds = new Set<Id>{};

for (Account a : accountList)
{
    accountIds.add(a.Id);
}

for (Contact c : [ SELECT Id, AccountId FROM Contact WHERE AccountId IN :accountIds ])
{
    //Do stuff with contacts based off Account Id
}

Again this is a very basic example of right and wrong. For any of us to help you optimize your code we'll need to see more of the underpinnings.

rtyanasrtyanas

 

I query for a list from the documents then using that list I create another list of documents with child records.  Right now I cannot hink of another way to do this since I need all the child records for each document.  For every document I have to have the related child records.  Then I display all this to the user.

 

The records will have to be queried eventually to match the parent.  Why would I need a DB if I was going to read all the child records into a flat file and match the records on my own?  Why do I need a DB when I cannot make queries??

themattythematty

Sean's way is the best.  Always avoid SOQL in a for loop.  Apex has realistic govenor limits, and the DB is there for you to make your proper lists or maps, then go through them as needed.  Why would you use up so much resources?  Your code would be slow and bloated.  Getting the list of Parents then querying the Children is the fastest and easiet way to do it.  It shouldn't take you that long to rebuild your class.