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
amritamrit 

Immmediate Solution for this:SOQL Exception

Hi,

 

I'm finding difficulty in  solving  this issue.When i tried to run atrigger its is showing Too many SOQL queries exception.I know this is because of soql inside for loop..I donot see any queries inside for loop .I cannot find why this error showing if not

 

trigger CarparkUpdate on Opportunity (after insert, after update) {

    List<Id> cId = new List<Id>();
    List<Id> old_cid = new List<Id> ();
    List<Car_Park__c> lstCp,lstCp1;
    Map<Id,Car_Park__c> mapCp = new Map<Id,Car_Park__c> ();
    Map<Id,Car_Park__c> mapCp1 = new Map<Id,Car_Park__c> ();
    
     System.debug('Total Number of SOQL Queries allowed in this apex code context: ' +  Limits.getLimitQueries());

    System.debug('Total Number of records that can be queried  in this apex code context: ' +  Limits.getLimitDmlRows());

    System.debug('Total Number of DML statements allowed in this apex code context: ' +  Limits.getLimitDmlStatements() );

    System.debug('Total Number of script statements allowed in this apex code context: ' +  Limits.getLimitScriptStatements());

     

   if(Trigger.oldMap != null) 
  {
        
        
        for(Opportunity O: Trigger.old){
            old_cid.add(O.Car_Park__c);
            system.debug('OLdcid'+old_cid);
            }
            system.debug('CID111111111111111'+old_cid);
          
            List<Car_Park__c>  Cp1 =[select id from Car_Park__c where id=: old_cid];
            system.debug('1stCp111111111111111'+Cp1.size());
            lstCp1 = [select id,Status__c from Car_Park__c where id IN: old_cid];//Too many SOQL queries Exception
            System.debug('1.Number of Queries used in this apex code so far: ' + Limits.getQueries());
            System.debug('2.Number of rows queried in this apex code so far: ' + Limits.getDmlRows());
            System.debug('3.Number of script statements used so far : ' +  Limits.getDmlStatements());
            System.debug('4.Number of Queries used in this apex code so far: ' + Limits.getQueries());
            System.debug('5.Number of rows queried in this apex code so far: ' + Limits.getDmlRows());
          
              for(Car_Park__c a : lstCp1) {
                mapCP1.put(a.Id,a);
              }
         //System.debug('#### mapCP1: ' + mapCP1.size());
      
        
        
   }  
    
         for(Opportunity O : Trigger.new) {
         cId.add(O.Car_Park__c);
        }

     lstCp = [select Status__c from Car_Park__c where id IN: cId];
     for(Car_Park__c a : lstCp) {
        mapCP.put(a.Id,a);
     }
     for(Opportunity O:trigger.new) {
        if(O.Car_Park__c != null) {            
            //if(O.Stagename == 'Blocking' || O.Stagename == 'Documentation Handoff')
            mapCp.get(O.Car_Park__c).Status__c='Blocked';
        }
    }
    if(Trigger.oldMap != null && mapCp1.size() !=0) {
        for(Opportunity O: Trigger.old){
            if(Trigger.newMap.get(o.Id).Car_Park__c == null)
                mapCp1.get(O.Car_Park__c).Status__c='Available';
            else
                mapCp1.get(O.Car_Park__c).Status__c = mapCp.get(O.Car_Park__c).Status__c;
        }
    }

}

 

 

Thanks

 
 

 Can anyone please help me to solve this issue asap.

 

Thanks

Best Answer chosen by Admin (Salesforce Developers) 
Nick00000Nick00000

Possibly you are hitting the limit due to your test method doing too much, can you post that?

All Answers

Chamil MadusankaChamil Madusanka

Yeah You are correct. There are no any query inside any loop. But there can be possibility to connect with loop where the trigger called. For a example, your trigger is for after update and after insert. Check whether are there any place that you are inserting or updating Oppertunity Object within a loop. If there are, avoid it.

 

Refer : http://wiki.developerforce.com/page/Apex_Code_Best_Practices

 

If a reply to a post answers your question or resolves your problem, please mark it as the solution to the post so that others may benefit.

 

 

amritamrit

Thanks for your reply.We are not updating and insert Opportunity object.

Chamil MadusankaChamil Madusanka

You are not updating and insert Opportunity object OR We are not updating and insert Opportunity object within a loop

 

?????????????

amritamrit

I mean  in this trigger we are not  inserting or updating opportunity object inside loop

 

 

Thanks

Chamil MadusankaChamil Madusanka

Yeah I can see that amrit. I think you didn't get it. I said that Check for all the places where this trigger is firing. And in that places, check for DML (insert, update) inside the loop.

 

If a reply to a post answers your question or resolves your problem, please mark it as the solution to the post so that others may benefit.

amritamrit

Im checking in debug log.there is some workflow which is updating some fields in oppportunity. i tried deactivationg those workflows but still showing exception.Is  that because of this reason.im not. plz correct me . Can you please tell me where is updating or inserting.When im running test class for this trigger its showing this error.But if im tring to execute the other test class it is displaying error in this trigger.

 

Thanks

 

 

Nick00000Nick00000

Possibly you are hitting the limit due to your test method doing too much, can you post that?

This was selected as the best answer
amritamrit

Hi,

 

When i tried to run this below test class,its showing this exception.But this test class is not related to that trigger

 

@isTest
private class Opportunity_Test
{

    static testMethod void myUnitTest() 
    {
        Account a = new Account(name='test');
        insert a;
        Block__c b = new Block__c(name='test block', 
                                  Registration_Flat__c = 0.7,
                                  Appropriation__c = 0.7, 
                                  Vat__c = 0.7, 
                                  Service_Tax__c = 0.7, 
                                  Service_Tax_Maint__c = 0.7, 
                                  VAT_Car_Park__c = 0.7, 
                                  VAT_W_E__c = 0.7, 
                                  ST_W_E__c = 0.7,
                                  ST_Legal__c = 0.7,
                                  Guideline_Value_Flat__c = 100); 
        insert b;
        
        payment_schedule_template__c pts = new payment_schedule_template__c(
        name='On Booking',
        Registration__c = 0.1, 
        Display_Order__c = 1,
        Due_Date__c = Date.today(),
        Maintenance_Charges__c = 0.1,
        Legal_Registration__c = 0.1,
        Tentative_Due_Date__c = 'Today',
        Water_Electricity__c = 0.1,
        Infrastructure_Development__c = 0.1, 
        Installment_Value_in__c = 0.1,
        Stage_Completed__c = False,
        Block__c=b.id);
        insert pts;        

        payment_schedule_template__c pts1 = new payment_schedule_template__c(
        name='test',
        Registration__c = 0.1, 
        Display_Order__c = 1,
        Due_Date__c = Date.today(),
        Maintenance_Charges__c = 0.1,
        Legal_Registration__c = 0.1,
        Tentative_Due_Date__c = 'Today',
        Water_Electricity__c = 0.1,
        Infrastructure_Development__c = 0.1, 
        Installment_Value_in__c = 0.1,
        Stage_Completed__c = True,
        Block__c=b.id);
        insert pts1;   

        payment_schedule_template__c pts2 = new payment_schedule_template__c(
        name='test2',
        Registration__c = 0.1, 
        Display_Order__c = 1,
        Due_Date__c = Date.today(),
        Maintenance_Charges__c = 0.1,
        Legal_Registration__c = 0.1,
        Tentative_Due_Date__c = 'Today',
        Water_Electricity__c = 0.1,
        Infrastructure_Development__c = 0.1, 
        Installment_Value_in__c = 0.1,
        Stage_Completed__c = False,
        Block__c=b.id);
        insert pts2;   
        
        Contact c = new Contact(lastname='test contatct', AccountId = a.Id);
        insert c;
        
        Apartment__c apt = new Apartment__c(name ='test apartment', Block__c = b.id,Built_up_Area__c=1000);        
        insert apt;
        
        Car_Park__c cp = new Car_Park__c(name ='test carpark' , Block__c = b.id, Status__c='Available');
        insert cp;
        
        Opportunity O = new Opportunity(
                            AccountId=a.Id,
                            Name='My Opportunity', 
                            StageName='Site visit',
                            CloseDate=Date.today(),
                            Apartment__c=apt.id,
                            Generate_Payment_Schedule__c=False,
                            Car_Park__c=cp.id);        
        
        insert O;
        
        Receipt__c rec=new Receipt__c(Name='109862812',Opportunity__c=O.id,Realization_Status__c='Not Done');
        insert rec;
                                                                                                                                                                                   
        system.debug('Test Opportunity'+O);

//         if(O.Car_Park__c != null)         
//         if(O.Apartment__c != null) 
            
           O.Stagename = 'Blocking';
           update O;
           apt.Status__c='Blocked';
           update apt;
           cp.Status__c='Blocked';
           update cp;           

        O.Generate_Payment_Schedule__c = True;
        update O;
                
        Payment_Schedule__c P = new Payment_Schedule__c( 
        Due_Date__c = System.Today(),
        Name=pts.Name,
        Installment_Value_in__c=pts.Installment_Value_in__c,
        Tentative_Due_Date__c=pts.Tentative_Due_Date__c,
        Display_Order__c=pts.Display_Order__c,
        Water_Electricity__c = pts.Water_Electricity__c,
        Maintenance_Charges__c = pts.Maintenance_Charges__c,
        Legal_Registration__c = pts.Legal_Registration__c,
        Registration__c = pts.Registration__c,
        Appropriation__c = pts.Block__r.Appropriation__c,
        vatp__c = pts.Block__r.Vat__c,
        stp__c = pts.Block__r.Service_Tax__c,
        stm__c = pts.Block__r.Service_Tax_Maint__c,
        privat__c = pts.Block__r.Vat__c,
        prist__c = pts.Block__r.Service_Tax__c,
        pristm__c = pts.Block__r.Service_Tax_Maint__c,
        VAT_Car_Park__c = pts.Block__r.VAT_Car_Park__c,
        VAT_Water__c = pts.Block__r.VAT_W_E__c,
        ST_Water__c = pts.Block__r.ST_W_E__c,
        ST_Legal__c = pts.Block__r.ST_Legal__c,
        Pri_VAT_Car_Park__c = pts.Block__r.VAT_Car_Park__c,
        Pri_VAT_Water__c = pts.Block__r.VAT_W_E__c,
        Pri_ST_Water__c = pts.Block__r.ST_W_E__c,
        Pri_ST_Legal__c = pts.Block__r.ST_Legal__c,
        Infrastructure_Development__c = pts.Infrastructure_Development__c,
        Status__c='Pending',        
        Opportunity__c = O.id);
        insert p;
        
        Payment_Schedule__c P1 = new Payment_Schedule__c( 
        Due_Date__c = System.Today(),
        Name=pts1.Name,
        Installment_Value_in__c=pts1.Installment_Value_in__c,
        Tentative_Due_Date__c=pts1.Tentative_Due_Date__c,
        Display_Order__c=pts1.Display_Order__c,
        Water_Electricity__c = pts1.Water_Electricity__c,
        Maintenance_Charges__c = pts1.Maintenance_Charges__c,
        Legal_Registration__c = pts1.Legal_Registration__c,
        Registration__c = pts1.Registration__c,
        Appropriation__c = pts1.Block__r.Appropriation__c,
        vatp__c = pts1.Block__r.Vat__c,
        stp__c = pts1.Block__r.Service_Tax__c,
        stm__c = pts1.Block__r.Service_Tax_Maint__c,
        privat__c = pts1.Block__r.Vat__c,
        prist__c = pts1.Block__r.Service_Tax__c,
        pristm__c = pts1.Block__r.Service_Tax_Maint__c,
        VAT_Car_Park__c = pts1.Block__r.VAT_Car_Park__c,
        VAT_Water__c = pts1.Block__r.VAT_W_E__c,
        ST_Water__c = pts1.Block__r.ST_W_E__c,
        ST_Legal__c = pts1.Block__r.ST_Legal__c,
        Pri_VAT_Car_Park__c = pts1.Block__r.VAT_Car_Park__c,
        Pri_VAT_Water__c = pts1.Block__r.VAT_W_E__c,
        Pri_ST_Water__c = pts1.Block__r.ST_W_E__c,
        Pri_ST_Legal__c = pts1.Block__r.ST_Legal__c,
        Infrastructure_Development__c = pts1.Infrastructure_Development__c,
        Status__c='Pending',        
        Opportunity__c = O.id);
        insert p1;

        Payment_Schedule__c P2 = new Payment_Schedule__c( 
        Due_Date__c = System.Today(),
        Name=pts2.Name,
        Installment_Value_in__c=pts2.Installment_Value_in__c,
        Tentative_Due_Date__c=pts2.Tentative_Due_Date__c,
        Display_Order__c=pts2.Display_Order__c,
        Water_Electricity__c = pts2.Water_Electricity__c,
        Maintenance_Charges__c = pts2.Maintenance_Charges__c,
        Legal_Registration__c = pts2.Legal_Registration__c,
        Registration__c = pts2.Registration__c,
        Appropriation__c = pts2.Block__r.Appropriation__c,
        vatp__c = pts2.Block__r.Vat__c,
        stp__c = pts2.Block__r.Service_Tax__c,
        stm__c = pts2.Block__r.Service_Tax_Maint__c,
        privat__c = pts2.Block__r.Vat__c,
        prist__c = pts2.Block__r.Service_Tax__c,
        pristm__c = pts2.Block__r.Service_Tax_Maint__c,
        VAT_Car_Park__c = pts2.Block__r.VAT_Car_Park__c,
        VAT_Water__c = pts2.Block__r.VAT_W_E__c,
        ST_Water__c = pts2.Block__r.ST_W_E__c,
        ST_Legal__c = pts2.Block__r.ST_Legal__c,
        Pri_VAT_Car_Park__c = pts2.Block__r.VAT_Car_Park__c,
        Pri_VAT_Water__c = pts2.Block__r.VAT_W_E__c,
        Pri_ST_Water__c = pts2.Block__r.ST_W_E__c,
        Pri_ST_Legal__c = pts2.Block__r.ST_Legal__c,
        Infrastructure_Development__c = pts2.Infrastructure_Development__c,
        Status__c='Pending',        
        Opportunity__c = O.id);
        insert p2;
           
           O.Stagename = 'Documentation Handoff';
           update O;
           apt.Status__c='Blocked';
           update apt;
           cp.Status__c='Blocked';
           update cp;
           
           O.Stagename = 'Booking Approved';
           update O; 
           apt.Status__c='Booked';
           update apt;
           
           O.Stagename = 'Booking Authorized';
           update O;
           apt.Status__c='Booked';
           update apt;
           
           O.Stagename = 'Sold';
           update O;
           apt.Status__c='Sold';
           update apt;           
                    
           O.Stagename = 'Registered';
           update O;
           apt.Status__c='Sold';
           update apt; 

           O.Stagename = 'Site Visit';
           update O;
           apt.Status__c='Available';
           update apt;
                      
           O.Stagename = 'Re-Assigned';
           update O;
           apt.Status__c='Available';
           update apt;
                        
           O.Stagename = 'Transferred';
           update O;
           apt.Status__c='Available';
           update apt;
                        
           O.Stagename = 'Canceled';
           update O;
           apt.Status__c='Available';
           cp.status__c = 'Available';
             
            String MyString1 = Rec.Name;
            String MyString2 = 'Cancelled';
            IF (MyString1.Contains(MyString2))
            {
                Rec.name = Rec.name;
            }
            else
            {
                Rec.name = Rec.name + ' - Cancelled';
            }
        update apt;
        update cp;
        update rec;                
        
        System.debug('### Opportunity: ' + O);
        a.Name='test account';
        c.LastName='test contact 2';
        c.Application_Form_Filled__c = true;
        c.Contact_Type__c = 'Other';
        update a;
        update c;
        
        B.name = 'test block update';
        b.Registration_Flat__c = 10;
        b.Guideline_Value_Flat__c = 10;
        update b;
        
        pts.name = 'test pst update';
        update pts;
        
        p.name = 'test milestone update';
        update p;
        
//        cp.Status__c='Blocked';
//        apt.Status__c='Blocked';      
//        cp.Status__c='Available';

   }
}

 thanks

Nick00000Nick00000

well of course that test is related to the trigger. Your trigger is after insert and update on opp, and the test method inserts and updates some opportunities.

 

I would recommend splitting it into separate test methods and also look into using Test.startTest() to specify when the test starts - this resets the limits for the actual test.