I have a custom object named Trip. I am trying to create an apex trigger that will execute after any trip is created, that will create an event with some of the info from the trip. I am new to apex code so please bear with me. The problem i'm encountering often is that i want to assign the account that was assigned to the trip, to the event. My code is:

trigger CreateEvent on Trip__c(after insert){
   List<Event> eventList = new List<Event>();
   for(Trip__c tripObj : Trigger.new){
      Event event = new Event();
      //event.Account  = tripObj.Account__c;  *problem is here*
      event.Subject = tripObj.Name;
      event.OwnerId = tripObj.CreatedById;
      upsert eventList;

How can I get the account object that was assigned to the trip, to assign it to the event? In my code I know that I'm not assigning the account object, I am assigning a lookup field. Please help, thanks.


2 Answers 2


Try replacing

event.Account  = tripObj.Account__c;


event.whatid  = tripObj.Account__c;

Welcome to Salesforce/Apex development!

If you check out the Salesforce.com documentation, you'll see that the field you're looking for on the Event object is called WhatId:

WhatId: ID of a related Account, Opportunity, Campaign, Case, or custom object. Label is Opportunity/Account ID.

As such, you can replace event.Account with event.WhatId and you should be all set. In general, it's a good step to double-check the name of the field you're trying to use, which you can do in the documentation I linked to, or directly in the Salesforce.com setup pages.

You must log in to answer this question.

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