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
mcrosbymcrosby 

Creating a Trigger that creates an Event

I am attempting to write a custom application for my organization that allows for a Time Off approval process.  I am trying to create a trigger that will create an Event record for a user if their Time Off request has been approved.

I developed the trigger in my sandbox, and everything seems to work properly.  However, I'm trying to work my way through deploying it to production and I'm running into a problem.

When I run the compileAndTest Ant script, I'm getting the following error:

System.DmlException: Update failed. First exception on row 0 with id a0FT0000000Arq8MAC; first error: CANNOT_INSERT_UPDATE_ACTIVATE_ENTITY, createTimeOffEvents: execution of AfterUpdate

caused by: System.DmlException: Insert failed. First exception on row 0; first error: INSUFFICIENT_ACCESS_ON_CROSS_REFERENCE_ENTITY, insufficient access rights on cross-reference id

Trigger.createTimeOffEvents: line 108, column 13

I believe the reason for this error is because I am attempting to set the OwnerId of the Event to a selected user id that is specified in the custom Time Off object.

Is there a way to allow triggers to set the OwnerId of a new record?
edel1985edel1985

hey

im trying to get time off to create an event when time off has been approved.

would it be possible to get the code for the trigger you created just so i can see how this works.

im new to using apex and need all the help i can get

mcrosbymcrosby

I've abandoned this for now, but I think the following was working at one point.  I had created a custom Time Off object with the fields specified in the following code.  The logic below seemed to work for what we needed at the time.  Good luck in getting yours developed.

Code:
trigger createTimeOffEvents on Time_Off__c (after update) {

    for (Integer i = 0; i < Trigger.old.size(); i++) {
        Time_Off__c oldRec = Trigger.old[i];
        Time_Off__c newRec = Trigger.new[i];
        
        if(oldRec.Status__c == 'Submitted' && newRec.Status__c == 'Approved')
        {
            System.debug('Creating event record');

            Boolean isAllDay = false;
            if(newRec.Start_Time__c == null || newRec.Start_Time__c == '')
            {
                System.debug('No start time specified, assuming all day event');
                isAllDay = true;
            }
            System.debug('isAllDay=' + isAllDay);
            //create an event record
            Event ev = new Event();
            ev.OwnerId = newRec.Requested_for__c;
            ev.Description = newRec.Description__c;
            ev.ShowAs = 'Busy';
            ev.Subject = newRec.Type__c;
            ev.IsReminderSet = false;
            ev.IsPrivate = false;
            Date startDate = newRec.Start_Date__c;

            if(isAllDay)
            {
                ev.IsAllDayEvent = true;
                ev.ActivityDate = startDate;
                Date endDate = newRec.End_Date__c;
                if(endDate != null)
                {
                    ev.IsRecurrence = true;
                    //ev.RecurrenceStartDateTime = Datetime.newInstance(startDate.year(), startDate.month(), startDate.day(), 0, 0, 0);
                    ev.RecurrenceStartDateTime = Datetime.newInstanceGmt(startDate.year(), startDate.month(), startDate.day(), 0, 0, 0);
                    ev.RecurrenceEndDateOnly = endDate;
                    Boolean excludeWeekends = newRec.Exclude_Weekends__c;
                    if(excludeWeekends)
                    {
                        ev.RecurrenceType = 'RecursEveryWeekday';
                        ev.RecurrenceDayOfWeekMask = 62;
                    }
                    else
                    {
                        ev.RecurrenceType = 'RecursDaily';
                        ev.RecurrenceInterval = 1;
                    }
                }
            }
            else
            {
                String startTime = newRec.Start_Time__c;
                Boolean isStartTimeAM = startTime.endsWith('AM');
                String[] startTimeArray = startTime.split(' ', 2);
                String[] startTimeHourMinuteArray = startTimeArray[0].split(':', 2);
                String startTimeHour = startTimeHourMinuteArray[0];
                String startTimeMinute = startTimeHourMinuteArray[1];
                Integer startHourInt = Integer.valueOf(startTimeHour);
                if(!isStartTimeAM)
                {
                    if(startHourInt < 12)
                    {
                        startHourInt = startHourInt + 12;
                    }
                }
                Integer startMinuteInt = Integer.valueOf(startTimeMinute);

                Date endDate = newRec.End_Date__c;
                String endTime = newRec.End_Time__c;
                Boolean isEndTimeAM = endTime.endsWith('AM');
                String[] endTimeArray = endTime.split(' ', 2);
                String[] endTimeHourMinuteArray = endTimeArray[0].split(':', 2);

                String endTimeHour = endTimeHourMinuteArray[0];
                String endTimeMinute = endTimeHourMinuteArray[1];
                Integer endHourInt = Integer.valueOf(endTimeHour);
                if(!isEndTimeAM)
                {
                    if(endHourInt < 12)
                    {
                        endHourInt = endHourInt + 12;
                    }
                }
                Integer endMinuteInt = Integer.valueOf(endTimeMinute);

                //to get the duration, we need to subtract the start time from
                //from the end time and convert to minutes

                //first, get the number of hours (in minutes)
                Integer duration = (endHourInt - startHourInt) * 60;
                if(startMinuteInt == 30 && endMinuteInt == 0)
                {
                    //subtract 30 minutes from the time
                    duration = duration - 30;
                }
                else if(startMinuteInt == 0 && endMinuteInt == 30)
                {
                    //add 30 minutes to the time
                    duration = duration + 30;
                }
                ev.DurationInMinutes = duration;
                ev.ActivityDateTime = Datetime.newInstance(startDate.year(), startDate.month(), startDate.day(), startHourInt, startMinuteInt, 0);
            }
            System.debug('Attempting to insert...');
  try
  {
             insert ev;
  }
  catch(Exception e)
  {
   System.debug(e.getMessage());
  }

        }
    }
}

NOTE:  After looking at the code, there are a few things that would make better sense efficiency wise.  This was one of my first attempts at creating a trigger.  For example, it would be best to do a batch insert of events rather than looping through and creating one event record at a time.  However, the above code might help you get a little further along in what you need to do.
 



Message Edited by mcrosby on 07-18-2008 09:50 AM