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
AK_SmithAK_Smith 

Solve System.LimitException: Too many SOQL queries: 101

During the Deployment i've gon en error
System.LimitException: Too many SOQL queries: 101 
Stack Trace: Class.TriggerHanglerActivity.copyDataToContactProductCategory: line 388, column 1 Class.TriggerHanglerActivity.onAfterUpdate: line 47, column 1 Trigger.ActivityTrigger: line 24, column 1
The Apex Class is:
global class UpdateUserTargets Implements Schedulable
    {
        global void execute(SchedulableContext sc)
        {
            doexecute();
        }

        public void doexecute()
        {
            list<PersonalGoals__c> AllRecords = new list<PersonalGoals__c>();
            AllRecords = [select Id from PersonalGoals__c where Q_check__c = TRUE];
            update AllRecords;
        }
    }

How to solve this error?
 
Rahul KumarRahul Kumar (Salesforce Developers) 
Hi Ak_smith,
  • The "System.LimitException: Too many SOQL queries: 101" error appears when you've hit the Execution Governor Limit, which means you can run up to a total 100 SOQL queries in a single call or context.  All the SOQL queries in triggers fired from one call or context will be counted against the limit of 100.
Please check the below links for further reference.
I hope it will be helpful.

Please mark it as best answer if it resolved the issue.

Best Regards
RahulKumar
Leo10Leo10
Hi,

The "System.LimitException: Too many SOQL queries: 101" error appears when you've hit the Execution Governor Limit, which means you can run up to a total 100 SOQL queries in a single call or context.  All the SOQL queries in triggers fired from one call or context will be counted against the limit of 100.

To fix the issue, you'll need to change your code in such a way that the number of SOQL fired is less than 100. If you need to change the context then you can use @future annotation which will run the code asynchronously.

and you can try this code also
 
global class UpdateUserTargets Implements Schedulable
    {
        global void execute(SchedulableContext sc)
        {
            doexecute();
        }

        public void doexecute()
        {
            list<PersonalGoals__c> AllRecords = new list<PersonalGoals__c>();
            AllRecords = [select Id from PersonalGoals__c where Q_check__c = TRUE limit 100];
            update AllRecords;
        }
    }

Thank you
Suvankar Chakraborty 21Suvankar Chakraborty 21
Governor limit for SOQL query is 100.So,Use ' limit 100' in your code like.

global class UpdateUserTargets Implements Schedulable { global void execute(SchedulableContext sc) { doexecute(); } public void doexecute() { list<PersonalGoals__c> AllRecords = new list<PersonalGoals__c>(); AllRecords = [select Id from PersonalGoals__c where Q_check__c = TRUE limit 100]; update AllRecords; } }
rajat Maheshwari 6rajat Maheshwari 6

Hi AK_Smith,

To get rid out of such issue, you can give a try with created Date filter under where clause.

below is sample snippet : - 

global class UpdateUserTargets Implements Schedulable
    {
        global void execute(SchedulableContext sc)
        {
            doexecute();
        }

        public void doexecute()
        {
            list<PersonalGoals__c> AllRecords = new list<PersonalGoals__c>();
            AllRecords = [select Id from PersonalGoals__c where Q_check__c = TRUE and createdDate >: 2017-01-01T00:00:00Z and CreatedDate:<2017-03-03T00:00:00Z ];
            update AllRecords;
        }
    }

Thanks
Rajat Maheshwari
rajatzmaheshwari@gmail.com
Sachin KumarSachin Kumar
Hi All,

I am geeting the error --  "System.LimitException: Too many SOQL queries: 101"
for below test class.

Can you please suggest me on it.

@isTest
public class RequestQuoteCtrlTest {

    @isTest static void tester(){
        Account acc = new Account(
            Name = 'test account',
            Type = 'Agent',
            FCPA_Status__c = 'Approved',
            RecordTypeId = SObjectType.Account.getRecordTypeInfosByDeveloperName().get('G_P_Dealer_Location').getRecordTypeId()
        );

              insert acc;

        Opportunity mainOpp = new Opportunity(
            Name = 'test opp',
            PriceBook2Id = Test.getStandardPricebookId(),
            StageName = 'Closed Won',
            CloseDate = Date.today(),
            AccountId = acc.Id,
            RecordTypeId = SObjectType.Opportunity.getRecordTypeInfosByDeveloperName().get('APA_PR_Opportunity').getRecordTypeId()
        );

       
  
        insert mainOpp;
     
        Group g = [SELECT Id FROM Group WHERE Name = 'test queue'];

        Queue_Info__c qi = new Queue_Info__c(
            Review_Queue_Id__c = g.Id
        );
        insert qi;

        Product_Hierarchy__c ph = new Product_Hierarchy__c(
            Name = 'test',
            Queue_Info__c = qi.Id
        );
        insert ph;

        Product2 prod = new Product2(
            Name = 'test prod', 
            Product_Hierarchy__c = ph.Id,
            IsActive = true
        );
        insert prod;

        PriceBookEntry pbe = new PriceBookEntry(
            PriceBook2Id = Test.getStandardPricebookId(),
            Product2Id = prod.Id,
            UnitPrice = 100.00,
            IsActive = true
        );
        insert pbe;


        OpportunityLineItem oli = new OpportunityLineItem(
            OpportunityId = mainOpp.Id,
            Product2Id = prod.Id,
            PriceBookEntryId = pbe.Id,
            UnitPrice = 100.00,
            Quantity = 1,
            Description = 'test'
        );
        insert oli;
Test.startTest();
    

            RequestQuoteCtrl ctrl = new RequestQuoteCtrl(new ApexPages.StandardController(mainOpp));
            ctrl.init();

            PageReference p = Page.AddProducts;
            p.getParameters().put('quoteId', ctrl.thisQuote.thisQuote.Id);

            Test.setCurrentPage(p);

            AddProductsCtrl prodCtrl = new AddProductsCtrl(new ApexPages.StandardController(mainOpp));

            ctrl = new RequestQuoteCtrl(new ApexPages.StandardController(mainOpp));
            ctrl.init();
            ctrl.createNewQuote();

        

        System.assertEquals(2, [SELECT Id FROM Quote WHERE OpportunityId = :mainOpp.Id].size());
        System.assertEquals(2, [SELECT Id FROM QuoteLineItem].size());
    }

    @testSetup static void setup(){
        Group g = new Group(
            Name = 'test queue', 
            Type = 'Queue'
        );
        insert g;

        QueueSobject mappingObject = new QueueSobject(QueueId = g.Id, SobjectType = 'Quote_Request__c');
        insert mappingObject;
    }

}


Any help is really appriaciated.
Thank You.