Skip to main content
Hi,

My first try of this didn't work, and I did a work around, but I'm really trying to understand why my first try didn't work.  Basically, I'm trying to pull the Account Name from one of' it's child objects.  So, I thought the syntatx should be something like sObject.Account__r.Name

 

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);

}

 
2 answers
  1. Jan 30, 2015, 3:23 AM
    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.
0/9000