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
Sudhir DudejaSudhir Dudeja 

On Clicking of Mass update button in list view a backend trigger fire which could not able to update the events records, please take a look at full description [Help]( CANNOT_INSERT_UPDATE_ACTIVATE_ENTITY,VALID_ID_FIELD, invalid record id: [])

public with sharing class MassUpdateInstructor {
           public SFDC_Class__c sess{get;set;}
           public Set<Id> sessIDS;
           public String selectedSessID;
           public List<SFDC_Class__c> sfdcRecords;
           public boolean pgb{get;set;}
	public MassUpdateInstructor() {
    	  sess=new SFDC_Class__c();
            sessIDS= new Set<Id>();
            sfdcRecords=new List<SFDC_Class__c>();
            selectedSessID = ApexPages.CurrentPage().getParameters().get('id');
            pgb=true;
            //System.debug('value is'+pgb);
            system.debug(selectedSessID);
            processIDS(selectedSessID);
	}

            public void processIDS(String sessionIDS){

                if(sessionIDS != null && sessionIDS !='')
                     {
                         List<String> strList = sessionIDS.split(',');
                         for (String s: strList)
                         {
                             sessIDS.add(s);
                         }
                        system.debug(sessIDS);
                     }
            }

            public PageReference fetchRecord(){

                //System.debug('value is'+pgb);
                sfdcRecords=[Select Contact__c from SFDC_Class__c where id=:sessIDS];
                System.debug(sfdcRecords);
                for(SFDC_Class__c sfdc:sfdcRecords){
                    sfdc.Contact__c=sess.Contact__c;
                }
                try {
                    update sfdcRecords;
                    pgb=false;
                    ApexPages.Message myMsg = new ApexPages.Message(ApexPages.Severity.INFO, 'Sessions Instructor are Updated');
                    ApexPages.addMessage(myMsg);
                    //System.debug('value is'+pgb);
                } catch(Exception e) {
                    ApexPages.Message myMsg = new ApexPages.Message(ApexPages.Severity.Error, 'There is an error: '+' '+e.getMessage());
                    ApexPages.addMessage(myMsg);
                    System.debug(e.getMessage());
                }
                return null;
            }
}


The above code is for my massupdate button and this button is in the list view now whenever i tried to update multiple records , a trigger in update event get fired and that trigger have simple query to update event but it give me such strange error , see below trigger query.
 
eventList=[Select StartDateTime FROM Event limit 2];
          system.debug(eventList);
          update eventList;


The error is given below, Please help to sort out this problem 

Note: When i try to update a single record(using limit 1) I would not get any error but for multiple records i am getting an error


Update failed. First exception on row 0 with id a08c00000080wefAAA; first error: CANNOT_INSERT_UPDATE_ACTIVATE_ENTITY, CreateEvents: execution of BeforeUpdate caused by: System.DmlException: Update failed. First exception on row 0 with id 00Uc0000003cwSPEAY; first error: INVALID_ID_FIELD, invalid record id: [] Class.MassUpdateInstructor.callTrigger: line 55, column 1 Trigger.CreateEvents: line 238, column 1: []
Sudhir DudejaSudhir Dudeja
There is an error: Update failed. First exception on row 0 with id a08c00000080wefAAA; first error: CANNOT_INSERT_UPDATE_ACTIVATE_ENTITY, CreateEvents: execution of AfterUpdate caused by: System.DmlException: Update failed. First exception on row 0 with id 00Uc0000003XEeMEAW; first error: INVALID_ID_FIELD, invalid record id: [] Trigger.CreateEvents: line 242, column 1: []


This is the error
RAM AnisettiRAM Anisetti
Hi Sudhir,

provide me trigger for event code...
 
Sudhir DudejaSudhir Dudeja

Thank you for your reply RAM, below is the code for trigger

 

/**
 * [SFDC_Class__c This trigger used to create a event in calendar when a new session record get created and the values of start and end dat will get copied to the old start and old end date]
 * @param  insert [Work on record creation]
 * @param  Update [Work on record update]
 * @return        [Null]
 * @author [Sudhir Dudeja]
 */
trigger CreateEvents on SFDC_Class__c (after Insert,before Update) {

       List<Event> eventslist = new List<Event>(); // to hold th events
       List<SFDC_Class__c> sessDay = new List<SFDC_Class__c>();
       List<Event> eventList = new List<Event>();
       List<SFDC_Class__c> sessionList=new List<SFDC_Class__c>(); //to hold session object
       List<Event> eventtoUpdate = new List<Event>(); //list to hold the event records to update
       String fullRecordURL;
       Public Datetime dd;
       Public Datetime dd1;
       List<SFDC_Class__c> sessionupdateList=new List<SFDC_Class__c>();

        if(trigger.isInsert){

           for(SFDC_Class__c session : Trigger.new)
           {

                  SFDC_Class__c sfdc=new SFDC_Class__c(id=Session.id);   //Creating a new session object
                  if(session.Start_Date_New__c!=null){   //if date value in start date new is not null
                    sfdc.Start_Date__c=session.Start_Date_New__c.date(); //then copy the data in the old start date
                    dd=session.Start_Date_New__c;  //create a dummy vairable which hold the new start date value
                  }
                  if(Session.End_Date_New__c!=null) //Check new end date is not null
                  {
                    sfdc.End_Date__c=session.End_Date_New__c.date(); //copy the value of new end date to old end date
                    dd1=session.End_Date_New__c;//create a dummy vairable which hold the new end date value
                  }
                    system.debug(sfdc);
                    sessionList.add(sfdc);  //Add session object to the list

                  if(session.Create_Event__c==true){ //if checkbox is checked , create event
                  Event ee=new Event(); //create a new event
                  ee.OwnerId=label.Contain_ID_of_the_data_CRM; //copy the logged in user id to event
                  ee.ActivityDateTime=session.Start_Date_New__c;
                  ee.Description='Session Event';
                  ee.EndDateTime=session.End_Date_New__c;
                  ee.IsReminderSet=True;
                  ee.IsAllDayEvent=False; //Evnt is not all day
                  ee.Instructor__c=session.Dummy_Instructor_Field__c;
                  system.debug(session.Dummy_Instructor_Field__c);
                  ee.Session_Name__c=session.Name;
                  ee.Session_Status__c='Scheduled';
                  ee.Subject=session.Name;
                  ee.Location=session.Facility__c;
                  ee.Session_ID__c=session.id;  //A variable in event to hold the session ID in the event.
                  ee.SessionDayURL__c = URL.getSalesforceBaseUrl().toExternalForm() + '/' + session.Id;  //URL of session record
                  system.debug(session.Courses_Names__c);
                  //This event field all the details in each seprate lines.
                  ee.Description='Instructor Name :'+ ' ' +session.Dummy_Instructor_Field__c+ '' + '\t\n' +''+ 'Session Name :'+ ' ' +session.Name+ '' + '\t\n' +''+ 'Course Name :'+ ' ' +session.Courses_Names__c+ '' + '\t\n' +''+ 'Location :' + ' ' +session.Facility__c+ ''+ '\t\n' +''+ 'Is Catering Required ? :'+ ' ' +session.Is_Catering_Required__c+'' + '\t\n' +'\t\n' +''+ 'Session URL :' + ' ' +URL.getSalesforceBaseUrl().toExternalForm() + '/' + session.Id+'' + '\t\n' +''+ 'Additional Requests :'+ '\t\n' +session.Additional_Request__c;
                  eventslist.add(ee); //Add events to the list @declare variable at line 10
                  system.debug(ee.Description);
                }
           }

           if(!eventslist.isempty()) //If evenlist is not empty
           {
             insert eventslist; //insert all events
           }
             if(!sessionList.isempty() && dd!=null && dd1!=null) //if session list is not empty and contains start date and end date then update session object
           {
             update sessionList; //update the session object
           }
        }
      if(trigger.isUpdate){  //trigger work upon the update event
       if(preventTriggerRecursion.updatedOnce1()) //To check whether the trigger is not get stuck in recursion if we are updating the same object
        {

          eventList=[Select StartDateTime FROM Event ];
          system.debug(eventList);
          update eventList;
        }
      }
}
RAM AnisettiRAM Anisetti
Hi Sudhir,

As my understand,if possible can you modify ur trigger like below...or if possible tell me the exact functionality of the trigger

 
trigger CreateEvents on SFDC_Class__c (after Insert,after Update) {

 List<Event> eventslist = new List<Event>(); 

if(trigger.isInsert && trigger.isAfter){
for(SFDC_Class__c s:trigger.new){

   if(s.Create_Event__c==true && s.Start_Date_New__c!=null && s.End_Date_New__c!=null){
   
      Event ee=new Event();
	              ee.OwnerId=label.Contain_ID_of_the_data_CRM; //copy the logged in user id to event
                  ee.ActivityDateTime=s.Start_Date_New__c;
                  ee.whatid=s.id; //need to mention whatid here
                  ee.EndDateTime=s.End_Date_New__c;
                  ee.IsReminderSet=True;
                  ee.Instructor__c=s.Dummy_Instructor_Field__c;
                  ee.IsAllDayEvent=False; //Evnt is not all day
                  system.debug(s.Dummy_Instructor_Field__c);
                  ee.s_Name__c=s.Name;
                  ee.s_Status__c='Scheduled';
                  ee.Subject=s.Name;
                  ee.Location=s.Facility__c;
                  ee.s_ID__c=s.id;  //A variable in event to hold the s ID in the event.
                  ee.sDayURL__c = URL.getSalesforceBaseUrl().toExternalForm() + '/' + s.Id;  //URL of s record
                  ee.Description='Instructor Name :'+ ' ' +s.Dummy_Instructor_Field__c+ '' + '\t\n' +''+ 's Name :'+ ' ' +s.Name+ '' + '\t\n' +''+ 'Course Name :'+ ' ' +s.Courses_Names__c+ '' + '\t\n' +''+ 'Location :' + ' ' +s.Facility__c+ ''+ '\t\n' +''+ 'Is Catering Required ? :'+ ' ' +s.Is_Catering_Required__c+'' + '\t\n' +'\t\n' +''+ 's URL :' + ' ' +URL.getSalesforceBaseUrl().toExternalForm() + '/' + s.Id+'' + '\t\n' +''+ 'Additional Requests :'+ '\t\n' +s.Additional_Request__c;
                  eventslist.add(ee); 
	  
	  
   
   }

}

if(eventslist.size()>0){
  insert eventslist;

}


if(trigger.isUpdate && trigger.isAfter){


//perform logic here if you need create or update event when update SFDC_Class__c record.....
   
   
}




}

 
Sudhir DudejaSudhir Dudeja

Hello Ram,

Thanks for your reply.

 

My trigger update the events and it was working fine but recently I added a mass update button on list view which is used to update a particular field of a object so whenever I try to mass update the records from the list view my trigger gets fired and in that trigger on update context i am getting an error so I start troubleshooting the error and commented all the code in the trigger update context place simple soql query there which is returning me two records of event and then after that there is update list command.

So its like 

 

mass update button fires ----> trigger and in trigger update context have the below code(the below code is the complete code) but it giving me error (on multple records)as shown above

 

if(trigger.isUpdate && trigger.isAfter){

eventList=[Select StartDateTime FROM Event limit 2];
          system.debug(eventList);
          update eventList;

}



Code in trigger update works fine if I select one records from list and try to update it but when i select multiple records from the list then i get the error.

 

Sudhir DudejaSudhir Dudeja
I tried your code i am still getting the same error for multiple records and no error when I am selecting a single record from the list view
RAM AnisettiRAM Anisetti
Let me know what is the purpose of the below code....
 
if(trigger.isUpdate && trigger.isAfter){

eventList=[Select StartDateTime FROM Event limit 2];
system.debug(eventList);
update eventList;

}

 
RAM AnisettiRAM Anisetti
if possible give the code for following class..

preventTriggerRecursion.updatedOnce1()
Sudhir DudejaSudhir Dudeja

Hello Ram,


Below is the actual code of my trigger 

 

/**
 * [SFDC_Class__c This trigger used to create a event in calendar when a new session record get created and the values of start and end dat will get copied to the old start and old end date]
 * @param  insert [Work on record creation]
 * @param  Update [Work on record update]
 * @return        [Null]
 * @author [Sudhir Dudeja]
 */
trigger CreateEvents on SFDC_Class__c (after insert,after Update,before update,before insert) {

       List<Event> eventslist = new List<Event>(); // to hold th events
       List<SFDC_Class__c> sessDay = new List<SFDC_Class__c>();
       List<Event> eventList = new List<Event>();
       List<SFDC_Class__c> sessionList=new List<SFDC_Class__c>(); //to hold session object
       List<Event> eventtoUpdate = new List<Event>(); //list to hold the event records to update
       public List<SFDC_Class__c> sfdcs=new List<SFDC_Class__c>(); //to hold session object
       String fullRecordURL;
       Public Datetime dd;
       Public Datetime dd1;
       List<SFDC_Class__c> sessionupdateList=new List<SFDC_Class__c>();


        if(trigger.isInsert && trigger.isBefore){
          for(SFDC_Class__c sDay : trigger.new){
            if(sDay.Create_Event__c==true && sDay.Start_Date_New__c==null) {
              sDay.Start_Date_New__c.addError('Start Date is manadtory to create event');
            }
          }
        }

        if(trigger.isInsert && trigger.isAfter){

           for(SFDC_Class__c session : Trigger.new)
           {

                  SFDC_Class__c sfdc=new SFDC_Class__c(id=Session.id);   //Creating a new session object
                  if(session.Start_Date_New__c!=null){   //if date value in start date new is not null
                    sfdc.Start_Date__c=session.Start_Date_New__c.date(); //then copy the data in the old start date
                    dd=session.Start_Date_New__c;  //create a dummy vairable which hold the new start date value
                  }
                  if(Session.End_Date_New__c!=null) //Check new end date is not null
                  {
                    sfdc.End_Date__c=session.End_Date_New__c.date(); //copy the value of new end date to old end date
                    dd1=session.End_Date_New__c;//create a dummy vairable which hold the new end date value
                  }
                    system.debug(sfdc);
                    sessionList.add(sfdc);  //Add session object to the list

                    if(session.Create_Event__c==true && session.Start_Date_New__c==null) {
                      session.Start_Date_New__c.addError('Start Date is manadtory to create event');
                    }


                  if(session.Create_Event__c==true && session.Start_Date_New__c!=null && session.End_Date_New__c!=null){ //if checkbox is checked , create event
                  Event ee=new Event(); //create a new event
                  ee.OwnerId=label.Contain_ID_of_the_data_CRM; //copy the logged in user id to event
                  ee.ActivityDateTime=session.Start_Date_New__c;
                  ee.Description='Session Event';
                  ee.EndDateTime=session.End_Date_New__c;
                  ee.IsReminderSet=True;
                  ee.IsAllDayEvent=False; //Evnt is not all day
                  ee.Instructor__c=session.Dummy_Instructor_Field__c;
                  system.debug(session.Dummy_Instructor_Field__c);
                  ee.Session_Name__c=session.Name;
                  ee.Session_Status__c='Scheduled';
                  ee.Subject=session.Name;
                  ee.Location=session.Facility__c;
                  ee.Session_ID__c=session.id;  //A variable in event to hold the session ID in the event.
                  ee.SessionDayURL__c = URL.getSalesforceBaseUrl().toExternalForm() + '/' + session.Id;  //URL of session record
                  system.debug(session.Courses_Names__c);
                  //This event field all the details in each seprate lines.
                  ee.Description='Instructor Name :'+ ' ' +session.Dummy_Instructor_Field__c+ '' + '\t\n' +''+ 'Session Name :'+ ' ' +session.Name+ '' + '\t\n' +''+ 'Course Name :'+ ' ' +session.Courses_Names__c+ '' + '\t\n' +''+ 'Location :' + ' ' +session.Facility__c+ ''+ '\t\n' +''+ 'Is Catering Required ? :'+ ' ' +session.Is_Catering_Required__c+'' + '\t\n' +'\t\n' +''+ 'Session URL :' + ' ' +URL.getSalesforceBaseUrl().toExternalForm() + '/' + session.Id+'' + '\t\n' +''+ 'Additional Requests :'+ '\t\n' +session.Additional_Request__c;
                  eventslist.add(ee); //Add events to the list @declare variable at line 10
                  system.debug(ee.Description);
                }
           }

           if(!eventslist.isempty()) //If evenlist is not empty
           {
             insert eventslist; //insert all events
           }
             if(!sessionList.isempty() && dd!=null && dd1!=null) //if session list is not empty and contains start date and end date then update session object
           {
             update sessionList; //update the session object
           }
        }

        if(trigger.isUpdate && trigger.isBefore){
          for(SFDC_Class__c sDay : trigger.new){
            if(sDay.Create_Event__c==true && sDay.Start_Date_New__c==null) {
              sDay.Start_Date_New__c.addError('Start Date is manadtory to create event');
            }
          }
        }

      if(trigger.isUpdate && trigger.isAfter){  //trigger work upon the update event
       if(preventTriggerRecursion.updatedOnce1()) //To check whether the trigger is not get stuck in recursion if we are updating the same object
        {
          Map<Id,Id> sessionIdMap = new Map<Id,Id>(); //Create a map to hold session ID and event ID
          Set<Id> SessionIdSet = new Set<Id>(); //Set to hold ID

          for(SFDC_Class__c sess : Trigger.new)
          {
            SessionIdSet.add(sess.id);  //Set to hold all the record ID whome we have to update
          }

          system.debug('SessionIdSet->' +SessionIdSet);

          //Query to event to fetch those events which are associate with session.
          eventList = [Select Id,ActivityDateTime,EndDateTime,Session_ID__c FROM Event where Session_ID__c = : SessionIdSet];

          system.debug('eventList-->'+ eventList);
          System.debug(eventList.size());

          //if event list is not null and size is greater than zero
          if(eventList != Null || eventList.size()>0){
          for(Event eve : eventList)
          {
          if(eve.Session_ID__c != Null){  //if session ID in event not null
          sessionIdMap.put(eve.Session_ID__c,eve.Id); //then add the session ID and event id to the map
          }
          }
          system.debug('sessionIdMap-->'+sessionIdMap);
          }

          //Loop to all records which we are updating
          for(SFDC_Class__c sDay : trigger.new){


            //if checkbox is checked mark true in event.
            //Trigger.oldMap.get(sDay.id).Client__c==sDay.Client__c this conidition is added to remove the error which getting on mass update button on session other than this was not necessary to put
            if(sDay.Create_Event__c==true){
                Event eve = new Event(); //Create a new event
                eve.ID = sessionIdMap.get(sDay.ID); //fetching event record or event from map
                system.debug('*****'+eve.ID);
                eve.ActivityDateTime = sDay.Start_Date_New__c;  //change the time in event
                eve.EndDateTime = sDay.End_Date_New__c;//change the end date in event
                eve.Instructor__c=sDay.Dummy_Instructor_Field__c; //Change the instructor
                system.debug(sDay.Course_Name__c);
                eve.Description='Instructor Name :'+ ' ' +sDay.Dummy_Instructor_Field__c+ '' + '\t\n' +''+ 'Session Name :'+ ' ' +sDay.Name+ '' + '\t\n' +''+ 'Course Name :'+ ' ' +sDay.Courses_Names__c+'' + '\t\n' +''+ 'Location :' + ' ' +sDay.Facility__c+''+ '\t\n' +''+ 'Is Catering Required ? :'+ ' ' +sDay.Is_Catering_Required__c+'' + '\t\n' + '\t\n' +''+ 'Session URL :' + ' ' +URL.getSalesforceBaseUrl().toExternalForm() + '/' + sDay.Id+'' + '\t\n' +''+ 'Additional Requests :'+ '\t\n' +sDay.Additional_Request__c;
                eve.Location=sDay.Facility__c;
               if(eve.ID!=null){ //if event id is not null
                  system.debug('*****'+eve);
                  eventtoUpdate.add(eve); //add event to the list
              }
                  else{
                    Event ee=new Event(); //create a new event
                    ee.OwnerId=label.Contain_ID_of_the_data_CRM; //copy the logged in user id to event
                    ee.ActivityDateTime=sDay.Start_Date_New__c;
                    ee.Description='Session Event';
                    ee.EndDateTime=sDay.End_Date_New__c;
                    ee.IsReminderSet=True;
                    ee.Location=sDay.Facility__c;
                    ee.IsAllDayEvent=False; //Evnt is not all day
                    ee.Instructor__c=sDay.Dummy_Instructor_Field__c;
                    ee.Session_Name__c=sDay.Name;
                    ee.Session_Status__c='scheduled';
                    ee.Subject=sDay.Name;
                    ee.Description='Instructor Name :'+ ' ' +sDay.Dummy_Instructor_Field__c+ '' + '\t\n' +''+ 'Session Name :'+ ' ' +sDay.Name+ '' + '\t\n' +''+ 'Course Name :'+ ' ' +sDay.Courses_Names__c+'' + '\t\n' +''+ 'Location :' + ' ' +sDay.Facility__c+''+ '\t\n' +''+ 'Is Catering Required ? :'+ ' ' +sDay.Is_Catering_Required__c+'' + '\t\n' + '\t\n' +''+ 'Session URL :' + ' ' +URL.getSalesforceBaseUrl().toExternalForm() + '/' + sDay.Id+'' + '\t\n' +''+ 'Additional Requests :'+ '\t\n' +sDay.Additional_Request__c;
                    ee.Session_ID__c=sDay.id;  //A variable in event to hold the session ID in the event.
                    ee.SessionDayURL__c = URL.getSalesforceBaseUrl().toExternalForm() + '/' + sDay.Id;  //URL of session record
                    eventslist.add(ee); //Add events to the list @declare variable at line 10
                    system.debug(ee.SessionDayURL__c);
                    system.debug(sDay.Dummy_Instructor_Field__c);
                  }
            }
            
           }

              sfdcs=[Select Start_Date_New__c,End_Date_New__c,Start_Date__c,End_Date__c from SFDC_Class__c where id IN :SessionIdSet];

              for(SFDC_Class__c sfdc : sfdcs){
                    if(sfdc.Start_Date_New__c!=null && sfdc.Start_Date__c!=sfdc.Start_Date_New__c ){ //if value are changed
                    sfdc.Start_Date__c=sfdc.Start_Date_New__c.date(); //then copy the new value to old one
                    }
                    if(sfdc.End_Date_New__c!=null && sfdc.End_Date__c!=sfdc.End_Date_New__c ){
                    sfdc.End_Date__c=sfdc.End_Date_New__c.date();
                    }
                    if(sfdc.Start_Date_New__c!=null || sfdc.End_Date_New__c!=null){
                    sessionupdateList.add(sfdc);
                    }
            }

          //if session list is greater than 0 then update list
           if(sessionupdateList.size()>0)
           {
            update sessionupdateList;
           }

           if(!eventslist.isempty()) //If evenlist is not empty
           {
             insert eventslist; //insert all events
           }

            //if event list is greater than 0 then update the list
           if(eventtoUpdate !=null && eventtoUpdate.size()>0)
           {
            system.debug(eventtoUpdate);
            system.debug(eventtoUpdate.size());
            system.debug('in update');
            update eventtoUpdate;
           }
        }
      }
}


Now When i am massupdating my records from list view button this trigger get run and i am getting the below error

Error:
There is an error: Update failed. First exception on row 0 with id a08c00000080wefAAA; first error: CANNOT_INSERT_UPDATE_ACTIVATE_ENTITY, CreateEvents: execution of AfterUpdate caused by: System.DmlException: Update failed. First exception on row 0 with id 00Uc0000003cwSPEAY; first error: INVALID_ID_FIELD, invalid record id: [] Trigger.CreateEvents: line 208, column 1: []


For troubleshooting purpose i remove all the code in trigger on update context and just add some line which basically test whther i am able to update the event on not so  for testing purpose i added the below line after removing all the codes in update context.

 

if(trigger.isUpdate && trigger.isAfter){  //trigger work upon the update event
       if(preventTriggerRecursion.updatedOnce1()) //To check whether the trigger is not get stuck in recursion if we are updating the same object
        {

eventList=[Select StartDateTime FROM Event limit 2];
          system.debug(eventList);
          update eventList;


}

Note 1 : When i select multiple records from list view I get an error but when i select one record from lis view it works fine.

Note 2:  My trigger works fine when i am updating multiple records through data loader.

Below is the code to prevent the recursive trigger

 

public class preventTriggerRecursion
{
    private static boolean updatedRec1 = true;

    public static boolean updatedOnce1()
    {
        if(updatedRec1)
        {
            updatedRec1 = false;
            return true;
        }
        else
        {
            return updatedRec1;
        }
    }


    private static boolean updatedRec2 = true;

    public static boolean updatedOnce2()
    {
        if(updatedRec2)
        {
            updatedRec2 = false;
            return true;
        }
        else
        {
            return updatedRec2;
        }
    }

}
 

I actually could not able to out figure why i am getting this strange error, I can't just able to update the event 

 

RAM AnisettiRAM Anisetti
try this...
 
/**
 * [SFDC_Class__c This trigger used to create a event in calendar when a new session record get created and the values of start and end dat will get copied to the old start and old end date]
 * @param  insert [Work on record creation]
 * @param  Update [Work on record update]
 * @return        [Null]
 * @author [Sudhir Dudeja]
 */
trigger CreateEvents on SFDC_Class__c (after insert,after Update,before update,before insert) {

       List<Event> eventslist = new List<Event>(); // to hold th events
       List<SFDC_Class__c> sessDay = new List<SFDC_Class__c>();
       List<Event> eventList = new List<Event>();
       List<SFDC_Class__c> sessionList=new List<SFDC_Class__c>(); //to hold session object
       List<Event> eventtoUpdate = new List<Event>(); //list to hold the event records to update
       public List<SFDC_Class__c> sfdcs=new List<SFDC_Class__c>(); //to hold session object
       String fullRecordURL;
       Public Datetime dd;
       Public Datetime dd1;
       List<SFDC_Class__c> sessionupdateList=new List<SFDC_Class__c>();


        if(trigger.isInsert && trigger.isBefore){
          for(SFDC_Class__c sDay : trigger.new){
            if(sDay.Create_Event__c==true && sDay.Start_Date_New__c==null) {
              sDay.Start_Date_New__c.addError('Start Date is manadtory to create event');
            }
          }
        }

        if(trigger.isInsert && trigger.isAfter){

           for(SFDC_Class__c session : Trigger.new)
           {

                  SFDC_Class__c sfdc=new SFDC_Class__c(id=Session.id);   //Creating a new session object
                  if(session.Start_Date_New__c!=null){   //if date value in start date new is not null
                    sfdc.Start_Date__c=session.Start_Date_New__c.date(); //then copy the data in the old start date
                    dd=session.Start_Date_New__c;  //create a dummy vairable which hold the new start date value
                  }
                  if(Session.End_Date_New__c!=null) //Check new end date is not null
                  {
                    sfdc.End_Date__c=session.End_Date_New__c.date(); //copy the value of new end date to old end date
                    dd1=session.End_Date_New__c;//create a dummy vairable which hold the new end date value
                  }
                    system.debug(sfdc);
                    sessionList.add(sfdc);  //Add session object to the list

                    if(session.Create_Event__c==true && session.Start_Date_New__c==null) {
                      session.Start_Date_New__c.addError('Start Date is manadtory to create event');
                    }


                  if(session.Create_Event__c==true && session.Start_Date_New__c!=null && session.End_Date_New__c!=null){ //if checkbox is checked , create event
                  Event ee=new Event(); //create a new event
                  ee.OwnerId=label.Contain_ID_of_the_data_CRM; //copy the logged in user id to event
                  ee.ActivityDateTime=session.Start_Date_New__c;
                  ee.Description='Session Event';
                  ee.EndDateTime=session.End_Date_New__c;
                  ee.IsReminderSet=True;
                  ee.IsAllDayEvent=False; //Evnt is not all day
                  ee.Instructor__c=session.Dummy_Instructor_Field__c;
                  system.debug(session.Dummy_Instructor_Field__c);
                  ee.Session_Name__c=session.Name;
                  ee.Session_Status__c='Scheduled';
                  ee.Subject=session.Name;
                  ee.Location=session.Facility__c;
                  ee.Session_ID__c=session.id;  //A variable in event to hold the session ID in the event.
                  ee.SessionDayURL__c = URL.getSalesforceBaseUrl().toExternalForm() + '/' + session.Id;  //URL of session record
                  system.debug(session.Courses_Names__c);
                  //This event field all the details in each seprate lines.
                  ee.Description='Instructor Name :'+ ' ' +session.Dummy_Instructor_Field__c+ '' + '\t\n' +''+ 'Session Name :'+ ' ' +session.Name+ '' + '\t\n' +''+ 'Course Name :'+ ' ' +session.Courses_Names__c+ '' + '\t\n' +''+ 'Location :' + ' ' +session.Facility__c+ ''+ '\t\n' +''+ 'Is Catering Required ? :'+ ' ' +session.Is_Catering_Required__c+'' + '\t\n' +'\t\n' +''+ 'Session URL :' + ' ' +URL.getSalesforceBaseUrl().toExternalForm() + '/' + session.Id+'' + '\t\n' +''+ 'Additional Requests :'+ '\t\n' +session.Additional_Request__c;
                  eventslist.add(ee); //Add events to the list @declare variable at line 10
                  system.debug(ee.Description);
                }
           }

           if(!eventslist.isempty()) //If evenlist is not empty
           {
             insert eventslist; //insert all events
           }
             if(!sessionList.isempty() && dd!=null && dd1!=null) //if session list is not empty and contains start date and end date then update session object
           {
             update sessionList; //update the session object
           }
        }

        if(trigger.isUpdate && trigger.isBefore){
          for(SFDC_Class__c sDay : trigger.new){
            if(sDay.Create_Event__c==true && sDay.Start_Date_New__c==null) {
              sDay.Start_Date_New__c.addError('Start Date is manadtory to create event');
            }
          }
        }

      if(trigger.isUpdate && trigger.isAfter){ 
	  //trigger work upon the update event
       if(preventTriggerRecursion.updatedOnce1()) //To check whether the trigger is not get stuck in recursion if we are updating the same object
        {
          Map<Id,Id> sessionIdMap = new Map<Id,Id>(); //Create a map to hold session ID and event ID
          Set<Id> SessionIdSet = new Set<Id>(); //Set to hold ID

          for(SFDC_Class__c sess : Trigger.new)
          {
            SessionIdSet.add(sess.id);  //Set to hold all the record ID whome we have to update
          }

          system.debug('SessionIdSet->' +SessionIdSet);

          //Query to event to fetch those events which are associate with session.
		  
		  
          eventList = [Select Id,ActivityDateTime,EndDateTime,Session_ID__c FROM Event where Session_ID__c IN : SessionIdSet];
          
          system.debug('eventList-->'+ eventList);
          System.debug(eventList.size());

          //if event list is not null and size is greater than zero
          if(eventList != Null || eventList.size()>0){
          for(Event eve : eventList)
          {
          if(eve.Session_ID__c != Null){  //if session ID in event not null
          sessionIdMap.put(eve.Session_ID__c,eve.Id); //then add the session ID and event id to the map
          }
          }
          system.debug('sessionIdMap-->'+sessionIdMap);
          
		  
		  
		  
		  }

          //Loop to all records which we are updating
          for(SFDC_Class__c sDay : trigger.new){


            //if checkbox is checked mark true in event.
            //Trigger.oldMap.get(sDay.id).Client__c==sDay.Client__c this conidition is added to remove the error which getting on mass update button on session other than this was not necessary to put
            if(sDay.Create_Event__c==true){
                Event eve = new Event(); //Create a new event
                eve.id = sessionIdMap.get(sDay.id); //fetching event record or event from map
                system.debug('*****'+eve.ID);
                eve.ActivityDateTime = sDay.Start_Date_New__c;  //change the time in event
                eve.EndDateTime = sDay.End_Date_New__c;//change the end date in event
                eve.Instructor__c=sDay.Dummy_Instructor_Field__c; //Change the instructor
                system.debug(sDay.Course_Name__c);
                eve.Description='Instructor Name :'+ ' ' +sDay.Dummy_Instructor_Field__c+ '' + '\t\n' +''+ 'Session Name :'+ ' ' +sDay.Name+ '' + '\t\n' +''+ 'Course Name :'+ ' ' +sDay.Courses_Names__c+'' + '\t\n' +''+ 'Location :' + ' ' +sDay.Facility__c+''+ '\t\n' +''+ 'Is Catering Required ? :'+ ' ' +sDay.Is_Catering_Required__c+'' + '\t\n' + '\t\n' +''+ 'Session URL :' + ' ' +URL.getSalesforceBaseUrl().toExternalForm() + '/' + sDay.Id+'' + '\t\n' +''+ 'Additional Requests :'+ '\t\n' +sDay.Additional_Request__c;
                eve.Location=sDay.Facility__c;
               if(eve.ID!=null){ //if event id is not null
                  system.debug('*****'+eve);
                  eventtoUpdate.add(eve); //add event to the list
              }
                  else{
                    Event ee=new Event(); //create a new event
                    ee.OwnerId=label.Contain_ID_of_the_data_CRM; //copy the logged in user id to event
                    ee.ActivityDateTime=sDay.Start_Date_New__c;
                    ee.Description='Session Event';
                    ee.EndDateTime=sDay.End_Date_New__c;
                    ee.IsReminderSet=True;
                    ee.Location=sDay.Facility__c;
                    ee.IsAllDayEvent=False; //Evnt is not all day
                    ee.Instructor__c=sDay.Dummy_Instructor_Field__c;
                    ee.Session_Name__c=sDay.Name;
                    ee.Session_Status__c='scheduled';
                    ee.Subject=sDay.Name;
                    ee.Description='Instructor Name :'+ ' ' +sDay.Dummy_Instructor_Field__c+ '' + '\t\n' +''+ 'Session Name :'+ ' ' +sDay.Name+ '' + '\t\n' +''+ 'Course Name :'+ ' ' +sDay.Courses_Names__c+'' + '\t\n' +''+ 'Location :' + ' ' +sDay.Facility__c+''+ '\t\n' +''+ 'Is Catering Required ? :'+ ' ' +sDay.Is_Catering_Required__c+'' + '\t\n' + '\t\n' +''+ 'Session URL :' + ' ' +URL.getSalesforceBaseUrl().toExternalForm() + '/' + sDay.Id+'' + '\t\n' +''+ 'Additional Requests :'+ '\t\n' +sDay.Additional_Request__c;
                    ee.Session_ID__c=sDay.id;  //A variable in event to hold the session ID in the event.
                    ee.SessionDayURL__c = URL.getSalesforceBaseUrl().toExternalForm() + '/' + sDay.Id;  //URL of session record
                    eventslist.add(ee); //Add events to the list @declare variable at line 10
                    system.debug(ee.SessionDayURL__c);
                    system.debug(sDay.Dummy_Instructor_Field__c);
                  }
            }
            
           }

              sfdcs=[Select Start_Date_New__c,End_Date_New__c,Start_Date__c,End_Date__c from SFDC_Class__c where id IN :SessionIdSet];

              for(SFDC_Class__c sfdc : sfdcs){
                    if(sfdc.Start_Date_New__c!=null && sfdc.Start_Date__c!=sfdc.Start_Date_New__c ){ //if value are changed
                    sfdc.Start_Date__c=sfdc.Start_Date_New__c.date(); //then copy the new value to old one
                    }
                    if(sfdc.End_Date_New__c!=null && sfdc.End_Date__c!=sfdc.End_Date_New__c ){
                    sfdc.End_Date__c=sfdc.End_Date_New__c.date();
                    }
                    if(sfdc.Start_Date_New__c!=null || sfdc.End_Date_New__c!=null){
                    sessionupdateList.add(sfdc);
                    }
            }

          //if session list is greater than 0 then update list
           if(sessionupdateList.size()>0)
           {
            update sessionupdateList;
           }

           if(!eventslist.isempty()) //If evenlist is not empty
           {
             insert eventslist; //insert all events
           }

            //if event list is greater than 0 then update the list
           if(eventtoUpdate !=null && eventtoUpdate.size()>0)
           {
            system.debug(eventtoUpdate);
            system.debug(eventtoUpdate.size());
            system.debug('in update');
            update eventtoUpdate;
           }
		   
		   
		   
		   
        }
      }
}