+ Start a Discussion
shrey.tyagi88@tcs.comshrey.tyagi88@tcs.com 

Take SOQL out of FOR loop in Apex Trigger---Please Help!!!

Can anyone please help me take the SOQL outside the for loop?

 

The code is given below:

 

    if(Trigger.isAfter && Trigger.isInsert){
      List<Timeline_Entries__c> TimeLineList = new List<Timeline_Entries__c> ();
      List<Moratorium__c> MoratoriumList = new List<Moratorium__c>();
      for(Case cs:Trigger.new){
        MoratoriumList=[Select Name,Cert_No__c,Start_Date__c,Stop_Date__c from Moratorium__c where Cert_No__c=:cs.Cert_Number__c]  ;
          system.debug('MoratoriumListkasize'+MoratoriumList.size());
          for (integer x=0;x<MoratoriumList.size();x++){
            TimeLineList.add(new Timeline_Entries__c(Type__c='Test',Case__c=cs.id,Call_In_Out__c='In',Activity__c='TestingMoratoriums',Curtailment_Calc__c='Start',Activity_Date__c=MoratoriumList[x].Start_Date__c));
            TimeLineList.add(new Timeline_Entries__c(Type__c='Test',Case__c=cs.id,Call_In_Out__c='In',Activity__c='TestingMoratoriums',Curtailment_Calc__c='Stop',Activity_Date__c=MoratoriumList[x].Stop_Date__c));
          }
      }
    if(TimeLineList.size()>0){
     insert TimeLineList;
    }  
  }

Dhaval PanchalDhaval Panchal
if(Trigger.isAfter && Trigger.isInsert){
	List<Timeline_Entries__c> TimeLineList = new List<Timeline_Entries__c> ();
	List<Moratorium__c> MoratoriumList = new List<Moratorium__c>();
	Set<String> setCertNo = new Set<String>() //I hope Cert_Number__c is a string type, change setCertNo type if it is of other type
	for(Case cs:Trigger.new){
		setCertNo.add(cs.Cert_Number__c);
	}
	if(setCertNo.size()>0){
		MoratoriumList=[Select Name,Cert_No__c,Start_Date__c,Stop_Date__c from Moratorium__c where Cert_No__c IN:setCertNo]  ;
		if(MoratoriumList.size()>0){
			for(Moratorium__c mort:MoratoriumList){
				TimeLineList.add(new Timeline_Entries__c(Type__c='Test',Case__c=cs.id,Call_In_Out__c='In',Activity__c='TestingMoratoriums',Curtailment_Calc__c='Start',Activity_Date__c=mort.Start_Date__c));
				TimeLineList.add(new Timeline_Entries__c(Type__c='Test',Case__c=cs.id,Call_In_Out__c='In',Activity__c='TestingMoratoriums',Curtailment_Calc__c='Stop',Activity_Date__c=mort.Stop_Date__c));
			}
		}
	}
	if(TimeLineList.size()>0){
		insert TimeLineList;
	}  
}

 

Naveen NelavelliNaveen Nelavelli

if(Trigger.isAfter && Trigger.isInsert){
      List<Timeline_Entries__c> TimeLineList = new List<Timeline_Entries__c> ();
      List<Moratorium__c> MoratoriumList = new List<Moratorium__c>();

     List<Moratorium__c> MList = new List<Moratorium__c>();
      
        MList=[Select Name,Cert_No__c,Start_Date__c,Stop_Date__c from Moratorium__c ]  ;

for(Case cs:Trigger.new){

 

 for(Moratorium__c mrl:MList){

if(mrl.Cert_No__c=:cs.Cert_Number__c){

    MoratoriumList.add(mrl);

 

}
          system.debug('MoratoriumListkasize'+MoratoriumList.size());
          for (integer x=0;x<MoratoriumList.size();x++){
            TimeLineList.add(new Timeline_Entries__c(Type__c='Test',Case__c=cs.id,Call_In_Out__c='In',Activity__c='TestingMoratoriums',Curtailment_Calc__c='Start',Activity_Date__c=MoratoriumList[x].Start_Date__c));
            TimeLineList.add(new Timeline_Entries__c(Type__c='Test',Case__c=cs.id,Call_In_Out__c='In',Activity__c='TestingMoratoriums',Curtailment_Calc__c='Stop',Activity_Date__c=MoratoriumList[x].Stop_Date__c));
          }
      }
    if(TimeLineList.size()>0){
     insert TimeLineList;
    } 
  }

Dhaval PanchalDhaval Panchal
Hi Naveen,

In this case you are trying to fetch all records from Moratorium__c object which may cause performance issue if Moratorium__c has too many records.