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
Nagendra Singh 12Nagendra Singh 12 

Salesforce SOQL Error Too many SOQL 001

Hi All,

I'm running one time update using Execute Anonymous and getting error "Salesforce SOQL Error Too many SOQL 001" for the following code, I tried few options to change the code but couldn't run the update, anyone can help with this code please: (Opportuntiy is master for custom object Sales Team, 1 : M)

List <Opportunity> OpptySalesRep = [Select Id,Sales_Rep__c From Opportunity Where  Sales_Rep__c !=null and (CreatedDate > 2014-11-01T00:00:00Z and CreatedDate <= 2014-11-30T00:00:00Z)];    

    set<Id> opportunityIdSet = new set<Id>();
        for (Opportunity Oppty : OpptySalesRep) {
          
            list<Sales_Team__c> newGSTmember = new list<Sales_Team__c>(); 
           
            opportunityIdSet.add(Oppty.Id); //Needed for following SOQL for GST       

           //Query to get all Sales team memeber for the opportunity                    
           List <Sales_Team__c> gst = [Select Sales_Team_Member__c,Primary__c,Producer__c,Opportunity__c,Id
                                             From Sales_Team__c
                                             Where Opportunity__c  IN: opportunityIdSet and  Producer__c = null and Opportunity__c <> null];
                                        
                                             system.debug('Number of Sales Team Member# ' + gst.size());
            
            //Reset  opportunityIdSet for avoid duplicate iteration 
             opportunityIdSet.remove(Oppty.Id);
                           
            boolean IsnewSalesRep = true;  //to check if Sales Rep already exists in GST            
              
           // Loop through the list and update GST record that matches to Sales Rep
           for (Sales_Team__c OpptyGST : gst){
              system.debug('GST Memebr# : ' + OpptyGST.Sales_Team_Member__c);                        
              
              //Update Sales Rep to Primary
              if (OpptyGST.Sales_Team_Member__c == Oppty.Sales_Rep__c){
                  system.debug('INSIDE If');
                  OpptyGST.Primary__c = true;
                    IsnewSalesRep = false;
                }
              // Update Sales reps to non-primary
                else if (OpptyGST.Sales_Team_Member__c != Oppty.Sales_Rep__c && OpptyGST.Primary__c == true){
                  system.debug('INSIDE else If');
                  OpptyGST.Primary__c = false;
              }
                            
              }
            
            //Add Sales Rep to GST          
             if (IsnewSalesRep == true)
             {
             Sales_Team__c newGST = new Sales_Team__c (); //instantiate the GST object to put values
                          
            newGST.Sales_Team_Member__c = Oppty.Sales_Rep__c;
             newGST.Primary__c = true;
             newGST.Opportunity__c = Oppty.Id;
             newGSTmember.add(newGST);
             insert newGSTmember;
             
              }
           
              update gst;  
                         
           }
kaustav goswamikaustav goswami
This statement ---

//Query to get all Sales team memeber for the opportunity                    
           List <Sales_Team__c> gst = [Select Sales_Team_Member__c,Primary__c,Producer__c,Opportunity__c,Id
                                             From Sales_Team__c
                                             Where Opportunity__c  IN: opportunityIdSet and  Producer__c = null and Opportunity__c <> null];

--- should be outside the for loop.

The query is getting fored each time the loop iterates. Rather query the related child records

Select Sales_Team_Member__c,Primary__c,Producer__c,Opportunity__c,Id
                                             From Sales_Team__c
                                             Where Opportunity__c  IN: OpptySalesRep and  Producer__c = null and Opportunity__c <> null

Then use a map to store the opportunity to the child records.

Thanks,
Kaustav
~Onkar~Onkar
If Sales_Team__C is Child of Opportunity you can use single Query to fetch Opportunity and Related  Sales_Team data.


List <Opportunity> OpptySalesRep = [Select Id,Sales_Rep__c,
                                                          (Select Sales_Team_Member__c,Primary__c,
                                                          Producer__c,Opportunity__c,Id
                                                         From Sales_Team__r) // Relationship Object Name
                                        From Opportunity
                                        Where  Sales_Rep__c !=null
                                        and (
                                            CreatedDate > 2014-11-01T00:00:00Z
                                        and
                                            CreatedDate <= 2014-11-30T00:00:00Z
                                        )
                                    ];    


Please check Relationship name in your Object.
 
Nagendra Singh 12Nagendra Singh 12
Thanks Onkar, how to access child record to update, I tried as "OpptySalesRep.Sales_Team__c.Primary__c" and getting error "Initial term of field expression must be a concrete SObject: List<Opportunity>".
~Onkar~Onkar
List <Opportunity> OpptySalesRep = [Select Id,Sales_Rep__c,
                                                          (Select Sales_Team_Member__c,Primary__c,
                                                          Producer__c,Opportunity__c,Id
                                                         From Sales_Team__r) // Relationship Object Name
                                        From Opportunity
                                        Where  Sales_Rep__c !=null
                                        and (
                                            CreatedDate > 2014-11-01T00:00:00Z
                                        and
                                            CreatedDate <= 2014-11-30T00:00:00Z
                                        )
                                    ];    

===========================
List<Sales_Team__c> TeamList = new List<sales_Team__c>();

 for(Opportunity obj : OpptySalesRep){

  for(Sales_Team__c Team : obj.Sales_Team__r){
      Team.Primary__c = true;
      TeamList.add(Team);
  }

 }

 Database.SaveResult TeamUpdate = Database.update(TeamList);
=========================
Take Reference from above code.