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 : Trigger.new) {
      
      Messaging.SingleEmailMessage mail = new Messaging.SingleEmailMessage();
      List<String> sendTo = new List<String>();
      sendTo.add(stu.Email__c);
      mail.setToAddresses(sendTo);
      mail.setSenderDisplayName('salesforceAdmin');      
      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' ;
      mail.setHtmlBody(body);
      mails.add(mail);
    }
  
  Messaging.sendEmail(mails);
}
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