function readOnly(count){ }
Starting November 20, the site will be set to read-only. On December 4, 2023,
forum discussions will move to the Trailblazer Community.
+ Start a Discussion
LuciferLucifer 

Invalid foreign key relationship: Task.related at

I have an issue with the email template. I wanted to put the record name(Auto number field) in to the tepmlate. This is an email triggering on task. SO I'm able to get the task fields but wanted to get the name of the record on which the task is creating.

 

 

Essentially im looking for an output in this fashion:      

Task Name = ARQ 000001   Hello Lucifer  a subject Pre-Meeting1 with Priority Not Started and duedate 2012-11-27 00:00:00 is been created.Please click here to open task

 

 

mail.setHTMLBody    ('Task Name = ' +tsk.related.Annual_Request__r.Name +'Hello  ' +theUser.Name+' '+ 'a subject '+tsk.Subject+' '+'with Priority '+tsk.status +' '+ 'and duedate ' + tsk.Activitydate +' '+'is been created.'+'<a href="https://cs3.salesforce.com/'+tsk.Id+'">Please click here to open task<a/>');

 

 

Please help me with this..

Best Answer chosen by Admin (Salesforce Developers) 
kirkevonphillykirkevonphilly

The related records who (whoid) and what (whatid) are fairly limited in what you can retrieve through the relationship due to their polymorphic nature.

 

Prior to Winter '13 you'd have to do a separate query on the related object to pull in additional fields like your Annual_Request__r.name.  Then along came Winter '13 which allowed you to do something like this

 

SELECT
TYPEOF What
WHEN yourObject THEN Annual_Request__r.name
ELSE Id
END
FROM Task

 So depending on how your "tsk" comes to be, you may be able to use that new feature or you'll have to create an additional query, possibly pull the result into a map <id,yourObject>, and get the Annual_Request__r.name value from it that way.