+ Start a Discussion
pierrefrazny.ax358pierrefrazny.ax358 

Trigger on recurring event

I developped a trigger which creates a new record (object A) when a SF event record is inserted. It works well when a single event is created but does not when the user creates events that are recurring. It the event has 3 recurring events, then I get 4 records A.

 

Is there anything special with the ways the recurring events are created in SF? Any idea?

 

Here is my code:

 

 

trigger CreateSchoolActivity on Event (before insert) {
	if(Trigger.IsInsert || Trigger.IsUpdate){
        List<School_Activity__c> schoolactivityList = new List<School_Activity__c>();
        List<Event> EventList = new List<Event>();
        
       	recordtype rt = [Select  r.Id, r.SobjectType, r.Name From RecordType r where sobjecttype ='Event' and Name = 'School Appointment' limit 1];
		Id EventRTid = rt.Id;
    	for (Event e : Trigger.new) {
           if (Trigger.isInsert && e.RecordTypeId == EventRTid) {
        	  School_Activity__c sa; 
              sa = new School_Activity__c(Activity_Subject__c = e.Subject,Status__c=e.Status__c, Start_Date__c =e.StartDateTime, 
              		End_Date__c=e.EndDateTime, Related_Assignment__c=e.WhatId);
              schoolactivityList.add(sa);
              EventList.add(e);
            }
        }
        
        List<String> ids = new List<String>();
        if (schoolactivityList.size() > 0) {
            Database.SaveResult[] result = Database.Insert(schoolactivityList, false);
            for (Database.SaveResult sr : result){
                ids.add(sr.Id);
            }
        }
        if (ids.size() > 0) {
            for (Integer i=0; i<ids.size(); i++) {
                Event e = EventList.get(i); 
                if (ids.get(i) != null) {
                    e.School_Activity_ID__c = ids.get(i);
                }
            }
        }    
    }
}

 

 

Thanks for your help!

Pierre