trigger TastingEvent on Tasting__c (before insert, before update) {
List<Event> myEvent = new List<Event>();
User u = [Select id, FirstName from User where id =: UserInfo.getUserId() LIMIT 1];
for(Tasting__c taste:Trigger.New) {
String acct = taste.Account__c;
Account myAccount = [SELECT Name, Id FROM Account WHERE Id = :acct];
Event e = new Event();
// this doesn't work
//e.Subject = taste.Account__r.Name;
// but this does
String subject = (myAccount.Name + ' ' + taste.Location__c) ;
e.Subject = subject;
date st = taste.Date_of_Tasting__c;
DateTime dt_StartDate = datetime.newInstance(st.year(), st.month(), st.day(), 3,0,0);
e.StartDateTime = dt_StartDate;
datetime endDate = dt_StartDate;
endDate = endDate.addHours(2);
e.EndDateTime = endDate;
e.OwnerId = u.Id;
myEvent.add(e);
System.debug(taste.Account__r.Name);
}
insert(myEvent);
}
In sf, you can only use the field value that you have queried in SOQL. Trigger.new only store current objects's value, no parent or children objects value.