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
bathybathy 

Recurring event creating an error on apex trigger

Hi all,

I have developed an apex trigger to create a custom object record same like an event when an event is created and used workflow rule on that custom object to send an email alert. because events doesn't support workflow emails.
This trigger is getting an error while creating a recurring event.
I am posting my apex trigger and screen shot of my error message. Please have alook and let me know what could be wrong.
trigger createtask on Event (after insert) {     
List<Task1__c> t1 = new List<Task1__c>();
    for (Event newEvent: Trigger.New)
         {
            
                 t1.add (new Task1__c(
                     Name = 'New PD',
                    Record_Id__c = newEvent.Id,
                    Record_Type__c = NewEvent.RecordTypeId,
                    Start__c = newEvent.StartDateTime,
                    Assigned_to__c = newEvent.OwnerId,
                  
                    Created_by__c = newEvent.CreatedById,
                    Opportunity_Link__c = 'https://superannuationproperty.my.salesforce.com/' + newEvent.WhatId, 
                    Appointment_Successful__c = newEvent.Appointment_Successful__c,
                    Appointment_Unsuccessful__c = newEvent.Appointment_Unsuccessful__c,
                    Related_to__c = newEvent.WhatId,
                    
                    Location__c = newEvent.Location,
                    End__c = newEvent.EndDateTime,
                    Description__c = newEvent.Description,
                  
                    Subject__c = newEvent.Subject,
                    Detail_link__c = 'https://superannuationproperty.my.salesforce.com/' + newEvent.Id ));   
         
         
   insert t1;
 }
 }
User-added image
What I understand is that becuase I haven't selected any opportunity or contact it is giving me error. Is there any way to avoid this for recurring events or any other ideas welcome.

Thanks 
Best Answer chosen by bathy
logontokartiklogontokartik
You just need to insert outside the for loop. Please try this again 
 
trigger createtask on Event (after insert) {     
List<Task1__c> t1 = new List<Task1__c>();
    for (Event newEvent: Trigger.New)
         {
            
                 t1.add (new Task1__c(
                     Name = 'New PD',
                    Record_Id__c = newEvent.Id,
                    Record_Type__c = NewEvent.RecordTypeId,
                    Start__c = newEvent.StartDateTime,
                    Assigned_to__c = newEvent.OwnerId,
                  
                    Created_by__c = newEvent.CreatedById,
                    Opportunity_Link__c = 'https://superannuationproperty.my.salesforce.com/' + newEvent.WhatId, 
                    Appointment_Successful__c = newEvent.Appointment_Successful__c,
                    Appointment_Unsuccessful__c = newEvent.Appointment_Unsuccessful__c,
                    Related_to__c = newEvent.WhatId,
                    
                    Location__c = newEvent.Location,
                    End__c = newEvent.EndDateTime,
                    Description__c = newEvent.Description,
                  
                    Subject__c = newEvent.Subject,
                    Detail_link__c = 'https://superannuationproperty.my.salesforce.com/' + newEvent.Id ));   
         
         

   }
   insert t1; // This is a list you are inserting, it needs to be outside loop
 }

 

All Answers

logontokartiklogontokartik
You just need to insert outside the for loop. Please try this again 
 
trigger createtask on Event (after insert) {     
List<Task1__c> t1 = new List<Task1__c>();
    for (Event newEvent: Trigger.New)
         {
            
                 t1.add (new Task1__c(
                     Name = 'New PD',
                    Record_Id__c = newEvent.Id,
                    Record_Type__c = NewEvent.RecordTypeId,
                    Start__c = newEvent.StartDateTime,
                    Assigned_to__c = newEvent.OwnerId,
                  
                    Created_by__c = newEvent.CreatedById,
                    Opportunity_Link__c = 'https://superannuationproperty.my.salesforce.com/' + newEvent.WhatId, 
                    Appointment_Successful__c = newEvent.Appointment_Successful__c,
                    Appointment_Unsuccessful__c = newEvent.Appointment_Unsuccessful__c,
                    Related_to__c = newEvent.WhatId,
                    
                    Location__c = newEvent.Location,
                    End__c = newEvent.EndDateTime,
                    Description__c = newEvent.Description,
                  
                    Subject__c = newEvent.Subject,
                    Detail_link__c = 'https://superannuationproperty.my.salesforce.com/' + newEvent.Id ));   
         
         

   }
   insert t1; // This is a list you are inserting, it needs to be outside loop
 }

 
This was selected as the best answer
Zuinglio Lopes Ribeiro JúniorZuinglio Lopes Ribeiro Júnior
Hello,

Not sure why you are getting this error but you certainly should put your insert t1 outside of the loop.

Regards

Don't forget to mark your thread as 'SOLVED' with the answer that best helps you.
Shaijan ThomasShaijan Thomas
trigger createtask on Event (after insert) {     
List<Task1__c> t1 = new List<Task1__c>();
    for (Event newEvent: Trigger.New)
         {
            
                 t1.add (new Task1__c(
                     Name = 'New PD',
                    Record_Id__c = newEvent.Id,
                    Record_Type__c = NewEvent.RecordTypeId,
                    Start__c = newEvent.StartDateTime,
                    Assigned_to__c = newEvent.OwnerId,
                  
                    Created_by__c = newEvent.CreatedById,
                    Opportunity_Link__c = 'https://superannuationproperty.my.salesforce.com/' + newEvent.WhatId, 
                    Appointment_Successful__c = newEvent.Appointment_Successful__c,
                    Appointment_Unsuccessful__c = newEvent.Appointment_Unsuccessful__c,
                    Related_to__c = newEvent.WhatId,
                    
                    Location__c = newEvent.Location,
                    End__c = newEvent.EndDateTime,
                    Description__c = newEvent.Description,
                  
                    Subject__c = newEvent.Subject,
                    Detail_link__c = 'https://superannuationproperty.my.salesforce.com/' + newEvent.Id ));   
         
         
   upsert t1;
 }
 }

Can you try this
Shaijan
Shaijan ThomasShaijan Thomas
Did you try this code?
Shaijan
bathybathy
Thanks for your help Guys. i was able to resolve this by putting the insert outside the loop.