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
rockForcerockForce 

trigger hits the governor limits

Hello

 

I have created a trigger which hits the governor limits. Is there any way to sort this.

 

Here is my trigger code:

trigger sampleTrigger on sample__c (before insert, before update) {

	 
    List<Demo__c> demoCodeList = 
        [SELECT Id, Name FROM Dmeo__c WHERE status_code__c IN ('A', 'B', 'C', 'D', 'E')];
        

	Map<Id,Demo__c> demoCodeMap = new Map<Id, Demo__c>(demoCodeList);
    
    
    for (sample__c sam : trigger.new) {
    	
        if(demoCodeMap.containsKey(sam.demo_code_id__c)) {

            Integer statCount = [Select count() from sample__c 
                where demo_code_id__c IN :demoCodeList
                and year__c = 0];
                

            test__c tt = [select total_sample__c from test__c];
            
            if( statCount >= tt.total_test__c){
                sam.addError('Demo Error');     
            }
            
        }
    }
}

 Thanks in advance.

Venkat_rdVenkat_rd

You have two SOQL statements inside for loop

 

 Integer statCount = [Select count() from sample__c 
                where demo_code_id__c IN :demoCodeList
                and year__c = 0];
                

            test__c tt = [select total_sample__c from test__c];

move them out of for loop and you should do fine. you can not have more than 100 SOQL queries in context.