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
peterpptpeterppt 

Using code (BeforeInsert trigger) to charge a normal SF event into to a recurring event

 

We use third part calendaring software which does not handle public events or recurring events in the the way we want.

We have succesfully implemented ( with help from this forum) a trigger that fires on the beforeinsert event and changes the OwnerID of the record.  This sorts our public calendar problems very well.

 

We want to do something similar for recurring events.  The problem is that the software creates events which are not marked as recurring and to my knowledge there is no way to change a normal event to a resurring event.  So our plan is to intercept the event data before the table is updated and add the minimum fields required to make the event recurring - creating a simple as possible recurring pattern.  The key is that when the user goies into edit the record, he has the opportunity to edit the recurrence pattern.

 

When a normal recurring event is created where start date of recurring pattern = end date,  SF seems to create two records - one as a normal event, and the other with the same data but with the recurring data fields completed.  These records are linked with a value in the field RecurrenceActivityID field.  However using the Excel Connector I  can create a recurring event by just using the record with all the recurring data.  And when I go to edit this record, I can change the recurrence pattern and, when the changes are saved events for the whole pattern will be created. Perfect.

 

The problem.... is when I try and replicate this with trigger code the record save fails, either with a specific error message ( e.g. missing field - these errors I  have corrected ) or - worse - just with a fail message.

 

To date my Apex coding expeirence is light so I'm not sure where to go. Is the approach I'm proposing valid ?   I'm a little uneasy that SF seems to  generate two records - I'd try and mimiick this but I can see this making my simple code rather more complex - it then becomes two insert operations and, more problematical, I have to grab the RecurrenceAcivityID value that SF has generated.

 

The current trigger code is below

trigger Peter1 on Event (before insert) {
   if (trigger.isBefore && trigger.isInsert) {
      
      for (Event e : trigger.new) {
          if ( e.public_name__c == 'Hennock' ) {
              e.OwnerId = '023b000000098Dx';
         }
          if ( e.public_name__c == 'Bovey Church Rooms' ) {
              e.OwnerId = '023b000000098Ds';
         }
          if ( e.public_name__c == 'PPT Bovey' ) {
              e.OwnerId = '023b000000098Dn';
         }
        if (e.create_recurring_event__c == True)  {
    
          e.RecurrenceTimeZoneSidKey = 'Europe/London'; 
          e.RecurrenceType = 'RecursWeekly';
          e.IsRecurrence = True; 
          e.RecurrenceDayOfWeekMask = 62; 
          e.RecurrenceStartDateTime = e.StartDateTime;
      
          datetime t = e.StartDateTime;
          e.RecurrenceEndDateOnly = Date.newInstance(t.year(),t.month(),t.day());  
          e.RecurrenceInterval = 1 ;  
          e.RecurrenceDayOfMonth = Null;
          e.RecurrenceInstance = Null;
          e.ActivityDate =  Date.newInstance(t.year(),t.month(),t.day());  
          e.ActivityDateTime = e.StartDateTime;
       }
  
      }
   }
   // All done!
 }

The only other thing I can add  relates to error messages I have had relating to the RecurringInterval field.  These have said this should be Null, but setting this field  = Null, = 0 ( it is a integer field ) or leaving the whole assignment statment out all cause errors.  In the excel connector the update works with the cell left blank.  But I've not ventured into the VBA code to see what value is passed to SF - perhaps I should !

 

So any help anyone can give here will be much appreciated.

 

Thanks

 

Peter