2

I created a custom object which when saved creates a calendar Event using an Apex trigger. Am able to populate all of the desired fields in the Event except the Related To field. I want the Related To Object to inherent the custom object name and the Related To field the customer object record name.

Thanks!

> trigger calup on Schedule_Onsite_Technician__c (after insert, after
> update) {   List<Event> NewFSEvent = new List<Event>();
> 
>   for (Schedule_Onsite_Technician__c  eve : Trigger.new) {
>     Event e = new Event();
>     e.StartDateTime = eve.Onsite_Arrival_date_Time__c;
>     e.EndDateTime = eve.Return_date__c;
>     e.Location = eve.Location_Address__c;
>     e.Description = eve.Equipment__c;
>     e.OwnerId = eve.Assigned_Tech__c;
>     //e.WhatId = eve.RecordID;
>     //e.What.Type = 'ScheduleOnsiteTechnician';
>     e.ShowAs = 'OutofOffice';
>     e.Subject = eve.Field_Service_Activities__c;
>     e.WhoId = eve.Contact__c;
> 
>     NewFSEvent.add(e);   }
> 
>   insert NewFSEvent; }
1
  • eve.recordid should be eve.id. You don't need to set what.type.
    – sfdcfox
    May 11, 2014 at 2:39

1 Answer 1

2

Set the WhatId on the event record to your custom object's ID. Here's an example:

trigger createEventForCustomObject on CustomObject__c (after insert) {
    Event[] events = new Event[0];
    for(CustomObject__c record: Trigger.new) {
        events.add(new Event(WhatId=record.Id, Subject='Hello', ActivityDateTime=DateTime.now()));
    }
    insert events;
}
1
  • Thanks. Should have included my code as I am still having a problem.
    – Steven J
    May 11, 2014 at 0:37

You must log in to answer this question.

Not the answer you're looking for? Browse other questions tagged .