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
Ruben E.Ruben E. 

Too many SOQL queries error on apex class

Hello everybody, I have the class below getting data from a trigger (that seems to be working ok), the problem is, I am getting a "Too many SOQL queries" errors when it executes.  If I comment out "insert opptsToInsert;" then I have no problems, but of course my new opportunity is not created.  I've tryied and regarless of where I put the insert I always get the same error. Any suggestions will be welcome.

Thanks


public class RenewalOpportunities 
{
    public void CreateRenewalOpportunity(Map < Id, Opportunity > oldMap, Map < Id, Opportunity > newMap) 
    {
        System.debug('Renewal starting');    
        List < Opportunity > opptsToInsert = new List < Opportunity > ();
        Opportunity newOppt = new Opportunity();
        //List < Id > OpptyIds = new List < Id > ();
        for (Opportunity o: newMap.Values()) 
        {
            //OpptyIds.add(o.id);
            System.debug('Old ID ' + o.id);
            System.debug('Renewal check ' + o.RenewalCreated__c);

            {
                newOppt.Name = o.Name + ' Renewal Opportunity';
                newOppt.AccountId = o.AccountId;
                newOppt.RecordTypeId = o.RecordTypeId;
                newOppt.CloseDate = Date.Today() + 30;
                newOppt.StageName = 'Renewal Opportunity';
                newOppt.Type = 'Add-On Business';
                newOppt.NextStep = 'Send a invoice for maintenance renewal';
                 newOppt.OwnerId =o.OwnerId;}
                opptsToInsert.add(newOppt);
                System.debug('Account Name ' + newOppt.AccountId);
                System.debug('Opportunity Name ' + newOppt.Name);
                System.debug('Stage Name ' + newOppt.StageName);
                System.debug('Type Name ' + newOppt.Type);
                System.debug('Next Step ' + newOppt.NextStep);
                System.debug('Close Date ' + newOppt.CloseDate);
                System.debug('New Opportunities ' + opptsToInsert);
     

        }   
        
        System.debug('Size ' + opptsToInsert.size());
         if (opptsToInsert.size() > 0){ 
            insert opptsToInsert;}
    }
}
 
GauravGargGauravGarg

Hi Ruben,

There is salesforce governer limit of 100 SOQL Queries in a single thread, due to insert procedure of Renewal Oppty trigger and class logic would be running. 

We need to optimize the Trigger  and Class logic so that Renewal Oppty would not run un-neccesary queries. We have also implemented the same logic for Renewal Opportunities in our business logic. I have quite a good knowledge on Opportunity. 

To help you more on this, I would require access to  your org. 

You can contact me on below details.

Thanks,
Gaurav
Email: gauravgarg.nmims@gmail.com
Skype: gaurav62990

Sandy GaliSandy Gali
Hi Ruben,
                The code you posted here doesnt have any issues. Can you post your trigger code also.

My suspicion is that this class is being called multiple times by the trigger and hence the error. It might be going to an infinite loop.
Please post the trigger code also so that we could get a clear picture.

Regards,
Sandy