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
vamsi garapativamsi garapati 

how to fetch parent record field from trigger written on child object?Below is the code for an email notification whenever a a new student is joined .But i am getting mail with trainer name as null.

trigger Emailnotification on Student__c (after insert) {

  List<Messaging.SingleEmailMessage> mails = new list<Messaging.SingleEmailMessage>();
  for (Student__c stu : {
      Messaging.SingleEmailMessage mail = new Messaging.SingleEmailMessage();
      List<String> sendTo = new List<String>();
      mail.setSubject('Welcome Aboard');
      String body = 'Dear ' + stu.Name__c + ', \n';
      body += 'Thankyou for Choosing sfdc.';
      body += 'You selected '+ stu.Trainer__r.Name+' as your trainer' ;
Footprints on the MoonFootprints on the Moon
Hi Vamsi,
The way you are referring parent record field is correct, but can you confirm whether Student record is associated with any Trainer record and it is not empty (Assuming this is lookup relationship) ?
Let me know if this helps.
vamsi garapativamsi garapati
Yes the Student record is associated with trainer record