+ Start a Discussion
bharath kumar 52bharath kumar 52 

prevent insertion of records recursively with triggers

Hi All,

I have written a trigger which is used to insert the status changes on an object. i.e there are multiple updates on an object and i need to do the following :

1.)insert a record into the new object when "Availability time is changed"
2.)insert when the "status" of a object is changed.
For each change i need one instance to be created with appropriate values for time and status change
But the trigger acts roguely. sometimes it creates a record and sometimes it waits for another trigger which will create an object in the table and then fire multiple times. How can i stop this recursive behaviour of the trigger?
Can someone please look into the below code suggest the changes :

trigger UpdateHistoryStatusOnService on WorkOrder (After update) {
 
Service_Status_History__c statusobj = new Service_Status_History__c(); //the object in which a record should be inserted
    List<WorkOrder > liserv = [select id, name , Financial_Status__c, Package_Decline_Reason__c,packagecomments__c,Timechange_comments__c,Timechange_Reason__c, Workorder_Status__c from WorkOrder where id in : Trigger.new];
    list<Service_Status_History__c> hlist= new list<Service_Status_History__c>();

   
    for(WorkOrder s : liserv){
         
        if(s.Financial_Status__c == 'Package Submitted' || s.Financial_Status__c == 'Package Declined' ){
            statusobj.Name = 'Package';
            statusobj.Service__c = s.id;
            if(s.Financial_Status__c == 'Package Declined' ){
                if(s.Package_Decline_Reason__c != null || s.packagecomments__c!= null)
                {
                    statusobj.Reason_Code__c = s.Package_Decline_Reason__c ;
                    statusobj.Comment__c = s.packagecomments__c;
                     statusobj.Previous_Status__c= trigger.oldmap.get(s.id).Financial_Status__c ;
                     statusobj.New_Status__c = s.Financial_Status__c ;
					 //insert the record with presentstatus, previous status, reason code and comments 
                }   
           
            //hlist.add(statusobj);
            
        }
        else if (s.Financial_Status__c =='Package Approved'){
            statusobj.Previous_Status__c= trigger.oldmap.get(s.id).Financial_Status__c ;
            statusobj.New_Status__c = s.Financial_Status__c ;
           //insert when package approved
           
        }
          /*  else if (s.Financial_Status__c =='Package Submitted'){
            statusobj.New_Status__c = s.Financial_Status__c ;
           
           
        }*/
     
      
        
    }
    else {
      }
     
      
      if(s.Timechange_comments__c!= null  && s.Timechange_Reason__c!= null ){
       statusobj.Name = 'Time Availability';
       statusobj.Reason_Code__c = s.Timechange_Reason__c;
       statusobj.Comment__c = s.Timechange_comments__c;
       statusobj.New_Status__c = s.Workorder_Status__c ;
       //hlist2.add(statusobj);
      //  insert hlist2;
	  //Insert when availability time is changed
     } 
   
    
 
}
   
    hlist.add(statusobj);
    insert hlist;
}
 

Thanks,

Bharath

Dilip_VDilip_V
Bharat,

refer to this solution doc.

https://help.salesforce.com/HTViewSolution?id=000133752&language=en_US
Let us know if u have any issues.
Mark it as best answer if it works.

Thanks.
m pandeym pandey
Please can u solve this....
https://developer.salesforce.com/forums/ForumsMain?id=9060G000000Xem6QAC
Amit Chaudhary 8Amit Chaudhary 8
Please check below post to stop the recursive trigger in salesforce
1) http://amitsalesforce.blogspot.in/2015/03/how-to-stop-recursive-trigger-in.html

you can create a class with a static Boolean variable with default value true. In the trigger, before executing your code keep a check that the variable is true or not. Once you check make the variable false.

 Apex Class with Static Variable
public class WorkOrderTriggerHandler
{
     public static Boolean isFirstTime = true;
}

Trigger Code
trigger UpdateHistoryStatusOnService on WorkOrder (After update) {

	if(WorkOrderTriggerHandler.isFirstTime)
	{ 
		WorkOrderTriggerHandler.isFirstTime = false;
		
		Service_Status_History__c statusobj = new Service_Status_History__c(); //the object in which a record should be inserted
			List<WorkOrder > liserv = [select id, name , Financial_Status__c, Package_Decline_Reason__c,packagecomments__c,Timechange_comments__c,Timechange_Reason__c, Workorder_Status__c from WorkOrder where id in : Trigger.new];
			list<Service_Status_History__c> hlist= new list<Service_Status_History__c>();

		   
			for(WorkOrder s : liserv){
				 
				if(s.Financial_Status__c == 'Package Submitted' || s.Financial_Status__c == 'Package Declined' ){
					statusobj.Name = 'Package';
					statusobj.Service__c = s.id;
					if(s.Financial_Status__c == 'Package Declined' ){
						if(s.Package_Decline_Reason__c != null || s.packagecomments__c!= null)
						{
							statusobj.Reason_Code__c = s.Package_Decline_Reason__c ;
							statusobj.Comment__c = s.packagecomments__c;
							 statusobj.Previous_Status__c= trigger.oldmap.get(s.id).Financial_Status__c ;
							 statusobj.New_Status__c = s.Financial_Status__c ;
							 //insert the record with presentstatus, previous status, reason code and comments 
						}   
				   
					//hlist.add(statusobj);
					
				}
				else if (s.Financial_Status__c =='Package Approved'){
					statusobj.Previous_Status__c= trigger.oldmap.get(s.id).Financial_Status__c ;
					statusobj.New_Status__c = s.Financial_Status__c ;
				   //insert when package approved
				   
				}
				  /*  else if (s.Financial_Status__c =='Package Submitted'){
					statusobj.New_Status__c = s.Financial_Status__c ;
				   
				   
				}*/
			 
			  
				
			}
			else {
			  }
			 
			  
			  if(s.Timechange_comments__c!= null  && s.Timechange_Reason__c!= null ){
			   statusobj.Name = 'Time Availability';
			   statusobj.Reason_Code__c = s.Timechange_Reason__c;
			   statusobj.Comment__c = s.Timechange_comments__c;
			   statusobj.New_Status__c = s.Workorder_Status__c ;
			   //hlist2.add(statusobj);
			  //  insert hlist2;
			  //Insert when availability time is changed
			 } 
		   
			
		 
		}
		   
			hlist.add(statusobj);
			insert hlist;
	}	
}

Let us know if this will help you


 
bharath kumar 52bharath kumar 52

@Amit I have followed what you told but it seems that another trigger is causing the trigger to fire again and for the first time when it creates a record it adds data to previous and present status but gives null values to reason code and comment. And the next time it fires it gives the values to reason code and comment there by creating the second record. Also, it wouldn't create a record if i put the "TriggerHandlerclass.StaticBooleanvar" in my trigger. I used the debug logs as well but im not able to figure out why the null values are coming during the first run.(Eventhough i see values stored on the parent object for these fields)

statusobj.Reason_Code__c = s.Package_Decline_Reason__c ; //coming as null during first run
statusobj.Comment__c = s.packagecomments__c;  //coming as null during first run
 statusobj.Previous_Status__c= trigger.oldmap.get(s.id).Financial_Status__c ; // Values are coming 
statusobj.New_Status__c = s.Financial_Status__c ; // Values are coming


Would be glad if you could guide me.


Thanks,

Bharath

Amit Chaudhary 8Amit Chaudhary 8
Please check below post to stop the recursive trigger in salesforce
1) http://amitsalesforce.blogspot.in/2015/03/how-to-stop-recursive-trigger-in.html

You need to update other trigger like that only. Or please post your full code.