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
Deepika G PDeepika G P 

trigger not getting parent field values in email body

Hi have an parent object project and child object project schedule . When the assigned to is changed to other user email needs to be sent for that i have written trigger below is the bit of that code. Now i want to pull the field values from project ,the trigger is written on project schedule.
When i try to pull the values i am getting null eg:Project:'+ PSOne.Project__r.Name ='null '
Can anyone help me on this please?

Messaging.SingleEmailMessage mail = new Messaging.SingleEmailMessage();
                    User current_user=[SELECT Email FROM User WHERE Id= :UserInfo.getUserId()] ;
                    String emailfromAddr = current_user.Email;
                    String emailtoAddr = PSOne.Assigned_To_User__c;
                    list<string> listmail= new list<string>();
                    listmail.add(emailfromaddr);
                    listmail.add(emailtoaddr);
                    mail.setToAddresses(listmail);
                    mail.setSubject(PSOne.Phase__c+' record is assigned to you  ' + PsOne.Name);  
                    mail.setPlainTextBody('To:'+ emailtoAddr +  '\n has made an change the following Task:'+  
                    '\n Subject:'+ PsOne.Phase__c+ '\n Project:'+ PSOne.Project__r.Name +'\n Due Date:'+PSOne.Project__r.Due_Date__c+'\n                                Priority:'+ PSOne.Project__r.Priority__c+
                    '\n For more details click the link below:{0}' );
                    system.debug('33333333'+ PsOne.Project__r);
                    Messaging.sendEmail(new Messaging.SingleEmailMessage[] {mail});
Best Answer chosen by Deepika G P
sslodhi87sslodhi87

Hi Deepika,

We will not the refrence object fields value in Trigger context variable for that we need to query on Project object and get all the require fields as we will get the only Project Id from Trigger.new or Trigger.newMap.

Please let me know if this help and require any help to get the Project and how how to use etc..

Thanks

All Answers

sslodhi87sslodhi87

Hi Deepika,

We will not the refrence object fields value in Trigger context variable for that we need to query on Project object and get all the require fields as we will get the only Project Id from Trigger.new or Trigger.newMap.

Please let me know if this help and require any help to get the Project and how how to use etc..

Thanks

This was selected as the best answer
Deepika G PDeepika G P
Thank you so much, but I am able to get the value for one but for other fields iam unable to even after fetching
 project__c p=[select id,name,Due_Date__c,Priority__c from project__c where id=:PSOne.Project__c limit 1];

  mail.setPlainTextBody('To:'+ emailtoAddr +  '\n has made an change the following Task:'+  
                    '\n Subject:'+ PsOne.Phase__c+ '\n Project: '+ p.Name +'\n Due Date: '+ p.Due_Date__c +'\n Priority:'+ p.Priority__c+
                    '\n For more details click the link below:+"' + fullTaskURL+'" ');


result:
Subject:Internal Kickoff Meeting
 Project:initial testing
 Due Date:null
 Priority:null

 
sslodhi87sslodhi87
Can you please check the Project record weather these field value are populated or null.
Deepika G PDeepika G P
Thank you thats right!