+ Start a Discussion
Sandeep M 1Sandeep M 1 

CANNOT_INSERT_UPDATE_ACTIVATE_ENTITY after insert,after update

i want to insert,update two custom fields in an object. i wrote trigger

trigger availableLeavesTrigger on Leave_Request__c (after insert,after UPDATE) {
List<id> lrs=new List<id>();
List<Leave_request__c> lea=new List<Leave_request__c>();
for(Leave_Request__c leave:Trigger.New)
{
  lrs.add(leave.id);
}
List<Leave_request__c> lc=[select id,name,available_casual_leaves__c,available_sick_leaves__c,days_off__c,type_of_leave__C from leave_request__c where id =:lrs ];
for(Leave_Request__c leave:lc)
{
    if(leave.type_of_leave__C=='casual leave')
    {
        leave.available_casual_leaves__c=leave.available_casual_leaves__c-leave.days_off__c;   
        lea.add(leave);  
    }
    else if(leave.type_of_leave__c=='sick leave')
    {
        leave.available_sick_leaves__c=leave.available_sick_leaves__c-leave.days_off__c;
        lea.add(leave);
    }
}
update lea;
}

I just cant able to insert or update the values into the fields. could anyone help me out
Best Answer chosen by Sandeep M 1
Pavan Kumar KajaPavan Kumar Kaja
Try This below code

trigger availableLeavesTrigger on Leave_Request__c (after insert,after UPDATE) {
if(RecursiveCheckController.RecursiveCheck == false){
  List<id> lrs=new List<id>();
  List<Leave_request__c> lea=new List<Leave_request__c>();
  for(Leave_Request__c leave:Trigger.New){
   lrs.add(leave.id);
  }
  List<Leave_request__c> lc=[select id,name,available_casual_leaves__c,available_sick_leaves__c,days_off__c,type_of_leave__C from leave_request__c where id IN=:lrs ];
  for(Leave_Request__c leave:lc){
   if(leave.type_of_leave__C=='casual leave'){
    leave.available_casual_leaves__c=leave.available_casual_leaves__c-leave.days_off__c;  
    lea.add(leave); 
   }
   else if(leave.type_of_leave__c=='sick leave'){
    leave.available_sick_leaves__c=leave.available_sick_leaves__c-leave.days_off__c;
    lea.add(leave);
   }
  }
  if(!lea.isEmpty()){
   RecursiveCheckController.RecursiveCheck = true;
   update lea;
  }
}
}


//////////////////

Class:

public class RecursiveCheckController(){
public static boolean RecursiveCheck = false;
}

All Answers

Ramu_SFDCRamu_SFDC
Add a line before the for loop to check if there are any records in lc.
Pavan Kumar KajaPavan Kumar Kaja
Try This below code

trigger availableLeavesTrigger on Leave_Request__c (after insert,after UPDATE) {
if(RecursiveCheckController.RecursiveCheck == false){
  List<id> lrs=new List<id>();
  List<Leave_request__c> lea=new List<Leave_request__c>();
  for(Leave_Request__c leave:Trigger.New){
   lrs.add(leave.id);
  }
  List<Leave_request__c> lc=[select id,name,available_casual_leaves__c,available_sick_leaves__c,days_off__c,type_of_leave__C from leave_request__c where id IN=:lrs ];
  for(Leave_Request__c leave:lc){
   if(leave.type_of_leave__C=='casual leave'){
    leave.available_casual_leaves__c=leave.available_casual_leaves__c-leave.days_off__c;  
    lea.add(leave); 
   }
   else if(leave.type_of_leave__c=='sick leave'){
    leave.available_sick_leaves__c=leave.available_sick_leaves__c-leave.days_off__c;
    lea.add(leave);
   }
  }
  if(!lea.isEmpty()){
   RecursiveCheckController.RecursiveCheck = true;
   update lea;
  }
}
}


//////////////////

Class:

public class RecursiveCheckController(){
public static boolean RecursiveCheck = false;
}
This was selected as the best answer