Take the 2-minute tour ×
Salesforce Stack Exchange is a question and answer site for Salesforce administrators, implementation experts, developers and anybody in-between. It's 100% free, no registration required.

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?

share|improve this question
add comment

1 Answer 1

up vote 6 down vote accepted

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'
    ));
  }
  if(!addEvents.isEmpty())
    insert addEvents;
}

Hope it helps!

Cheers

share|improve this answer
    
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 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 at 23:54
add comment

Your Answer

 
discard

By posting your answer, you agree to the privacy policy and terms of service.

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