+ Start a Discussion
satyamsatyam 

Too Many SOQL Problem-Urgent Help

Hi Everyone,

We have a requirement where we need to compare current Client support record with all the old client support record and that needs to be done before saving the record.

We need to compare near about 12 fields for every record which we are selecting during mass operation (can select 100 record for mass operation),but we are not having any field which can used as a key without saving the record.

We tried using map and list but we are getting exceptions ' Too many code statements: 200001' and as well as its making the process too time taking.

Please help to achieve this requirement .

 

Below is the method where we are getting issue:

 

public static Map<Client_support__c,List<String>> checkDuplicatePriceSupportforMassChangeone(List<Client_support__c> PSList){
                   
    List<String> duplicatesPS= new List<String>();    
    List<Client_support__c> OldPS=new List<Client_support__c>();
    oldPS =[select id,status__c,distributor_PRofile__c,Material_Selling__c,Material_Buying__c where status__c='Approved' or status__c='Awaiting distributor Approval' or status__c='Awaiting Nexeo Approval'];
    List <Client_support__c>l1 = new List<Client_support__c>();
    Map<Client_support__c,List<duplicate>> mapPS = new Map<Client_support__c,List<duplicate>>();
    List <String> l2=new List<String>();   
  for(Client_support__c ps:PSList){

    for(Client_support__c ps1:oldPS){

    if(ps.distributor_PRofile__c!=null){
        if(ps.distributor_PRofile__c == ps1.distributor_PRofile__c){
        l1.add(ps1);
    }
    }

    if(ps.Material_Selling__c!=null){
        for(Client_support__c li : L1){
       
            if(ps.Material_Selling__c == ps1.Material_Selling__c){
            //l1.add(li);
            l2.add(li.aggrement_number__c);
            }

        }
    }

   
    }
   
    mapPS.put(ps1 ,l2);

 }   

 

Thanks in advance:))))
                   
   

Flint LockwoodFlint Lockwood

Hi, 

 

How are you calling this class (i.e. off a button or a trigger)? Also, what does your other original class look like (the one that gave you the SOQL error)? You might want to try a future method here. But even with that you might want to limit the records that call this operation. 

 

 

 

 

satyamsatyam
Hi,

Thanks for Reply!!

We are calling this method from another class method and passing a list of record to this method .

so my problem is that i want to run this below method for all the record passed through list .

So now i think you will understand my issue.

Thanks
Naidu PothiniNaidu Pothini
public static Map<Client_support__c,List<String>> checkDuplicatePriceSupportforMassChangeone(List<Client_support__c> PSList){
                   
    List<String> duplicatesPS= new List<String>();    
    List<Client_support__c> OldPS=new List<Client_support__c>();
    oldPS =[select id,status__c,distributor_PRofile__c,Material_Selling__c,Material_Buying__c where status__c='Approved' or status__c='Awaiting distributor Approval' or status__c='Awaiting Nexeo Approval']; // did you copy the exact code... it seems like you are missing FROM in the query. can you post the updated code
    List <Client_support__c>l1 = new List<Client_support__c>();
    Map<Client_support__c,List<duplicate>> mapPS = new Map<Client_support__c,List<duplicate>>();
    List <String> l2=new List<String>();   
  for(Client_support__c ps:PSList){

    for(Client_support__c ps1:oldPS){

    if(ps.distributor_PRofile__c!=null){
        if(ps.distributor_PRofile__c == ps1.distributor_PRofile__c){
        l1.add(ps1);
    }
    }

    if(ps.Material_Selling__c!=null){
        for(Client_support__c li : L1){
       
            if(ps.Material_Selling__c == ps1.Material_Selling__c){
            //l1.add(li);
            l2.add(li.aggrement_number__c);
            }

        }
    }

   
    }
   
    mapPS.put(ps1 ,l2);

 }   

 

satyamsatyam

Hi ,

 

Below is the code:

 

public static Map<Client_support__c,List<String>> checkDuplicateClientSupportforMassChangeone(List<Client_support__c> PSList){
                   
    List<String> duplicatesPS= new List<String>();    
    List<Client_support__c> OldPS=new List<Client_support__c>();
    oldPS =[select id,status__c,distributor_PRofile__c,Material_Selling__c,Material_Buying__c from Client_support__c  where status__c='Approved' or status__c='Awaiting distributor Approval' or status__c='Awaiting  Approval'];
    List <Client_support__c>l1 = new List<Client_support__c>();
    Map<Client_support__c,List<duplicate>> mapPS = new Map<Client_support__c,List<duplicate>>();
    List <String> l2=new List<String>();   
  for(Client_support__c ps:PSList){

    for(Client_support__c ps1:oldPS){

    if(ps.distributor_PRofile__c!=null){
        if(ps.distributor_PRofile__c == ps1.distributor_PRofile__c){
        l1.add(ps1);
    }
    }

    if(ps.Material_Selling__c!=null){
        for(Client_support__c li : L1){
       
            if(ps.Material_Selling__c == ps1.Material_Selling__c){
            //l1.add(li);
            l2.add(li.aggrement_number__c);
            }

        }
    }

   
    }
   
    mapPS.put(ps1 ,l2);

 }   

 

This is the whole code but now i am getting Too many code statements: 200001

 

Red line code is giving error i think its because of for loop inside loop but not know how to do this in single for loop.

 

Thanks for Help :)))

 

 

Naidu PothiniNaidu Pothini
public static Map<Client_support__c,List<String>> checkDuplicatePriceSupportforMassChangeone(List<Client_support__c> PSList)
{
    List<String> duplicatesPS= new List<String>();    
    List<Client_support__c> OldPS=new List<Client_support__c>();

    oldPS =[SELECT Id, Status__c, Distributor_PRofile__c, Material_Selling__c, Material_Buying__c 
            FROM Client_support__c 
            WHERE Status__c = 'Approved'
            OR Status__c = 'Awaiting distributor Approval'
            OR Status__c = 'Awaiting Nexeo Approval'];

    List <Client_support__c>l1 = new List<Client_support__c>();
    Map<Client_support__c,List<duplicate>> mapPS = new Map<Client_support__c,List<duplicate>>();
    List <String> l2 = new List<String>();

    for(Client_support__c ps : PSList)
    {
        for(Client_support__c ps1 : oldPS)
        {
            if(ps.distributor_PRofile__c != null && ps.Distributor_Profile__c == ps1.Distributor_Profile__c)
            {
                l1.add(ps1);
            }

            if(ps.Material_Selling__c!=null && ps.Material_Selling__c == ps1.Material_Selling__c)
            {
                for(Client_support__c li : L1)
                {
                    l2.add(li.aggrement_number__c);
                }
            }

            mapPS.put(ps1 ,l2);
        }
    } 
}  

 try this let me know if it throws any error// Can you explain what exactly you are trying in the highlighted code?