+ Start a Discussion
Che SFDCChe SFDC 

Help with populating fields in Event Trigger

Dear all, below is my trigger to send email from Salesforce. This trigger fires when Visit__c field is checked as True. Trigger works fine, but issue is that I`m not able to populate Created By Name, Assigned To name, Opportunity/Account Name (RelatedTo) etc in my html email body. How can I populate these fields in my email body? If I String E.Whatid or Whoid, it gives me ID in template instead of name.

Also, is there a way I can populate Contact name, phone number etc. tagged to this event? I`m new to Apex so any help will be much appreciated! :)
Trigger VisitNotification on Event (after insert){
	
    List<Messaging.SingleEmailMessage> newEmails = new List<Messaging.SingleEmailMessage>();
    for(Event e :Trigger.new){
            String fullTaskURL;
            String Facility;
            Datetime startDate;
            Datetime endDate;
            String Purpose;
            String Salesexec;
    
        if(E.Visit__C){
            Messaging.SingleEmailMessage mail = new Messaging.SingleEmailMessage();
                
            mail.setSubject('Automated Alert: A New Record is Created');
            mail.setSaveAsActivity(false);
            mail.setTargetObjectId('00580000005HreR') ;        

            fullTaskURL = URL.getSalesforceBaseUrl().toExternalForm() + '/' + E.Id;
            Facility = E.Facility__c;
            startDate = E.StartDateTime;
            endDate = E.EndDateTime;
            Purpose = E.Purpose__c;
            Salesexec = E. Salesexec__c;
   
   
            //Generate email template
                String emailBody;
                emailBody = '<html><body>Hi<br> <br> A New Record has been created: <a href="' + fullTaskURL + '">' + E.Id + '</a><br></body></html>' + '<br> <br> Site/Facility: ' + Facility+ '<br> Start Date/Time: ' + StartDate + '<br> End Date/Time: ' +endDate + '<br> Purpose: ' + Purpose + '<br> Sales VP: '+ Salesexec +  '<br> <br> Thank you <br> <br> Salesforce Admin' ; 
                mail.setHtmlBody(emailBody );

            newEmails.add(mail);
        }
    }

    messaging.sendEmail(newEmails);
}

 
Best Answer chosen by Che SFDC
YuchenYuchen
You can try something like this:

String createdByID = E.CreatedByID;
String createdByName = [SELECT Name from User WHERE id=: createdByID limit 1].Name;

Then you can use this createdByName in the email template. 

If you want to get Opportunity Name, it will be like:
String oppID = E.whatId;  (you can check the prefix of the whatid to know it is Opp or Account, Opp will be 006, Account will be 001)
String oppName = [SELECT Name from Opportunity WHERE id=: oppID limit 1].Name;
//similar for Account Name

You can also put the above query in some list/map if you do not want to query inside the for loop.

All Answers

YuchenYuchen
In Trigger.new, you will only have access to one level information, for example, you will have access to the CreatedByID, Opportunity ID, if you want to find out more information you will need to do some query. You can query the User Object to find out the Name of the User for that specific CreatedByID, then you can use the Name in your email template.
Che SFDCChe SFDC
Thanks Yuchen, could you please give an example on how I could do that? I`m sure there is a way to run a query but just not sure how to use it since I`m new.
YuchenYuchen
You can try something like this:

String createdByID = E.CreatedByID;
String createdByName = [SELECT Name from User WHERE id=: createdByID limit 1].Name;

Then you can use this createdByName in the email template. 

If you want to get Opportunity Name, it will be like:
String oppID = E.whatId;  (you can check the prefix of the whatid to know it is Opp or Account, Opp will be 006, Account will be 001)
String oppName = [SELECT Name from Opportunity WHERE id=: oppID limit 1].Name;
//similar for Account Name

You can also put the above query in some list/map if you do not want to query inside the for loop.
This was selected as the best answer
Che SFDCChe SFDC
Yuchen, thank you so much for your help. It works! :)