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
Hari G SHari G S 

System.LimitException

 

Hi,

 

While tring to deploy the code to production i am getting this error 

 

System.LimitException: Too many SOQL Queries:101

 

Why this error is occuring?

 

My code is

 

trigger productTrigger on Product2 (after insert, after update) {
    Product2 myProd = Trigger.new[0];    
    Purchased_Products_del__c[] puchasedProd = [select Id, Product_Code__c from Purchased_Products_del__c where Product__c=:myProd.Id];
    if(puchasedProd != null && puchasedProd.size() > 0) {        
        for(Purchased_Products_del__c pProd:puchasedProd) {
            if(pProd.Product_Code__c != myProd.ProductCode) {
                pProd.Product_Code__c = myProd.ProductCode;
                update pProd;
            }
        }
    }
} 

 

Thanks and Regards

Hari G S

Sandeep001Sandeep001

Hello,

 

You are getting an error because your test class is making this trigger executes SOQLquery more than 100 times. It is hitting the governor limit. So you need to create a smaller dataset in your test class to deploy the trigger or you can use Test.startTest(); and Test.stopTest(); in your test class.

 

One more thing to note in your trigger, you must remove any DML statement outside the FOR loop as it might hit governor limit. Put the records to be updated in a List inside the FOR loop and update the list outside it.

 

Cheers,

ashish raiashish rai

Hello,

     Well you have written DML insdie the for loop.So use the below code to over come with that, or i think you are using SOQL query inside the test method also. So please check that and remove that from the loop.Your trigger code should be like this:

 

trigger productTrigger on Product2 (after insert, after update)
{
    Product2 myProd = Trigger.new[0];
    list<Purchased_Products_del__c> pProd1=new list<Purchased_Products_del__c>();
    Purchased_Products_del__c[] puchasedProd = [select Id, Product_Code__c from Purchased_Products_del__c where Product__c=:myProd.Id];
    if(puchasedProd != null && puchasedProd.size() > 0)
    {       
        for(Purchased_Products_del__c pProd:puchasedProd)
        {
            if(pProd.Product_Code__c != myProd.ProductCode)
            {
                pProd.Product_Code__c = myProd.ProductCode;
                pProd1.add(pProd);
            }
        }
        update pProd1;
    }
} 

 

 

Think this will help you.

Hari G SHari G S

 

Hi Sandeep and Ashish , thanks for the help with the modified code.

 

One doubt  I would like ask here, for this trigger to deploy how should I write my Test Class?

 

The Test Case i have written is

 

public with sharing class ProductTestClass {
    static testMethod void myProductTest() {
        Product2 prod = new Product2(name='Test Product1');
        insert prod;
    }
}

 

Is this the right way to write Test Cases?

 

Thanks And Regards

Hari G S

ashish raiashish rai

Hello,

    Yeah you are going on right track but you are considiring an if condition inside your trigger. So you have to make data like that, for this you have to insert Purchased_Products_del__c like this:

 

public with sharing class ProductTestClass

{
    static testMethod void myProductTest()

{
        Product2 prod = new Product2(name='Test Product1');
        insert prod;

      

        Purchased_Products_del__c  pNew=new Purchased_Products_del__c(name='xyz',Product__c=prod.id,other mendetory fields);

        insert pNew;

        update pNew;




    }
}

 

 

Think this will help you.