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
ami anuami anu 

How to resolve Trigger with TOO MANY SQL QUERIES:101 error.

Hi everyone,
i'm very much new to salesforce and i have written a trigger which has accomplished the task,but when there are many records its throwing an error: TOO MANY SQL QUERIES:101.

here is my trigger ,pls help me out with this issue

 




Trigger fieldupdate on External_Verification_Opinion__c (After insert,after update)
{  

   list<External_Verification_Opinion__c> evo = [SELECT id,         Final_Opinion__c, RecordTypeId,    Relationship_Form__c FROM External_Verification_Opinion__c where Final_Opinion__c=:'Approved' ];
 for(External_Verification_Opinion__c e : evo) 
  { 
 list<Relationship__c> rf = [SELECT Id,Sent_For_External_Verification__c,Product_Family__c                                 FROM Relationship__c where id=:e.Relationship_Form__c and Sent_For_External_Verification__c=:'yes'];    
for(Relationship__c re : rf)  
 {   
  if((e.RecordTypeId=='012f00000004KhI') && ((re.Product_Family__c=='Saving Accounts') ||(re.Product_Family__c=='credit cards') || (re.Product_Family__c=='Mortgage') ||(re.Product_Family__c=='personal loans'))) 
        {            
         re.Verifier__c=true;     
          update re;     
         }          
if(((e.RecordTypeId=='012f00000004K2e') && (re.Product_Family__c=='Mortgage')) )  
       {          
         re.Technical_Valuator__c=true;       
         update re;           
        }     
if(((e.RecordTypeId=='012f00000004K2j') && (re.Product_Family__c=='Mortgage')) )  
       {          
           re.Solicitor__c=true;    
           update re;            
        }   
  if((e.RecordTypeId=='012f00000004KhJ') && ((re.Product_Family__c=='credit cards') || (re.Product_Family__c=='Mortgage') ||(re.Product_Family__c=='personal loans')))    
     {    
       re.Credit_Agency__c=true;    
       update re;               }        
          } 
  } 
}





Saravanan @CreationSaravanan @Creation

Hi

 

avoid Relationship__c object query inside the for loop.And try to fallow the best practise of the trigger.

Bhawani SharmaBhawani Sharma
You are doing query in for loop. That will quickly hit your query limit. Put this query out side the loop and use Map/List/Set to hold the data. Use in Map/Set/List further.
ami anuami anu
i'm not understanding how i shld query Relationship__c object outside for loop .pls advice
sushant sussushant sus
for(External_Verification_Opinion__c e : evo)
{
ids.add(e.Relationship_Form__c );
recordtypess.add(e.recordtypeid);// i aded this so tht you can use in palce of e.recordtypeid now you can use set.contain(' ');
}

list<Relationship__c> rf = [SELECT Id,Sent_For_External_Verification__c,Product_Family__c FROM Relationship__c where id in: ids and Sent_For_External_Verification__c=:'yes'];
Bhawani SharmaBhawani Sharma
Trigger fieldupdate on External_Verification_Opinion__c (After insert,after update) {
	
	List<External_Verification_Opinion__c> evo = [SELECT id, Final_Opinion__c, RecordTypeId, Relationship_Form__c, (SELECT Id,Sent_For_External_Verification__c,Product_Family__c FROM Relationships__r where Sent_For_External_Verification__c=:'yes') FROM External_Verification_Opinion__c where Final_Opinion__c=:'Approved' ];
	
	List<Relationship__c> Relationships = new List<Relationship__c>();

	for(External_Verification_Opinion__c e : evo) {	
		if(e.Relationships__r != null) {
			for(Relationship__c re : e.Relationships__r) {   
				
				if((e.RecordTypeId=='012f00000004KhI') && ((re.Product_Family__c=='Saving Accounts') ||(re.Product_Family__c=='credit cards') || (re.Product_Family__c=='Mortgage') ||(re.Product_Family__c=='personal loans'))) {
					re.Verifier__c=true;     
					Relationships.add(re);     
				} else if(((e.RecordTypeId=='012f00000004K2e') && (re.Product_Family__c=='Mortgage')) ) {          
					re.Technical_Valuator__c=true;       
					Relationships.add(re);          
				} else if(((e.RecordTypeId=='012f00000004K2j') && (re.Product_Family__c=='Mortgage')) ) {          
					re.Solicitor__c=true;    
					Relationships.add(re);
				} else if((e.RecordTypeId=='012f00000004KhJ') && ((re.Product_Family__c=='credit cards') || (re.Product_Family__c=='Mortgage') ||(re.Product_Family__c=='personal loans'))) {    
					re.Credit_Agency__c=true;    
					Relationships.add(re);               
				}        
			} 
		}
	}
	
	update Relationships;
}

 Your code will be something like this. Probably you need to amke sme adjustment in code, but this is an idea how you need to proceed.