I am very new to Apex (about 2-3 days, about two hours of stuff that actually worked) and trying to create an Event once a custom object record is created.

I plagiarized code to create an Event and it worked! However, it wasn't bulkified so I'm changing it but am getting an error message that puzzles me (because the same line worked on the earlier code).

Working, non-bulkified code:

trigger createCalendarEvent on mBooking__c (after insert) {
for (mBooking__c newBooking: Trigger.New) {
    Event event = new Event(
        StartDateTime = newBooking.mDate__c,
        EndDateTime = newBooking.mDate__c,
        Subject = 'New event created by trigger'
    insert event;

Non-working, bulkified code:

trigger createnewCalendarEvent on mBooking__c (after insert) {
list<Event> addEvent = new List <Event>();
for (Event ev: Trigger.new) {
    ev.add (new Event(
        StartDateTime = mBooking__c.mDate__c,
        EndDateTime = mBooking__c.mDate__c,
        Subject = 'Newer event created by trigger'
insert ev;

Error: Compile Error: Invalid initial expression type for field Event.StartDateTime, expecting: Datetime at line 5 column 25

Why would a bulkified compile not recognize the same/similar line that worked before?

I apologize if this is lengthy or too junior but, since it won't compile, I can't save it anywhere but Word. Thoughts?


You are using a different object inside your for loop. I suggest you do this:

trigger createnewCalendarEvent on mBooking__c (after insert) {
  list<Event> addEvents = new List <Event>();
  for (mBooking__c mb: Trigger.new) {
    addEvents.add(new Event(
        StartDateTime = mb.mDate__c,
        EndDateTime = mb.mDate__c,
        Subject = 'Newer event created by trigger'
    insert addEvents;

Hope it helps!


  • As an addon explanation to the post of @dangt85 not only were you attempting to loop through the wrong object type, but you also were attempting to add the new event to an event record and not the Event List. ev.add (new Event( should have been addEvent.add (new Event( His code as a whole though, should work without a hitch.
    – jonnybro
    Mar 9 '14 at 4:28
  • Thanks! Since I'm plagiarizing and unsure of what I'm writing, I have no idea why it doesn't work. Getting it to work is pure chance without a place like this. Now I get to figure out the event start/end times (which are datetime but not on my timezone).
    – Mike
    Mar 9 '14 at 23:54

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.