Activity Forums Salesforce® Discussions How to handle "Too many soql queries:101" problem in Salesforce?

  • Guha

    Member
    February 10, 2018 at 2:15 AM

    Don't use the query inside any for loops. Instead, query the list of all records and store it in a list or set,  and loop through that list to do the set of logic you wanted to do. No queries inside a for loop is the ground rule for apex programming. Google examples for this approach, there are plenty of examples you can find. Happy coding.

  • [adinserter block='9']
  • Rahul

    Member
    February 10, 2018 at 5:46 AM

    Hi,
    Thanks for reply,  but i am not calling to query in loop .
    Here is my code:

    listCaseTCR = [SELECT id,RV_Consignment_Status__c ,CN_Number__c,Case_Category__c,Issue_Type__c,Sub_Title__c
    FROM Case WHERE Issue_Type__c =:'Tech - CR' AND Sub_Title__c IN:subtitle AND CN_Number__c!='NULL' AND Status!='Closed' AND RV_Is_Parent__c=:FALSE];
    if(!listCaseTCR.isEmpty()){
    //for(Case cr : listCaseTCR)
    total = listCaseTCR.size();
    system.debug('total cases of Tech - CR are :'+total);

    for(i=0;i<total;i++)
    {
    cnumber=listCaseTCR[i].CN_Number__c;
    if(cnumber!=Null && cnumber!='')
    trackconsignment(cnumber);
    }
    }

     

    now problem start from here that when i am calling trackconsignment(cnumber); method then this is using 32 soql queries to closed a case and i have a  lot of cases in list listCaseTCR  so it's firing 101 error.
    how to handle it?

    regards,

    Rahul Kumar

Log In to reply.

Popular Salesforce Blogs

Popular Salesforce Videos

Skip to toolbar