You need to sign in to do that
Don't have an account?
![vamsi garapati vamsi garapati](https://dfc-org-production.my.site.com/img/userprofile/default_profile_45_v2.png)
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);
}
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);
}
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.