You need to sign in to do that
Don't have an account?
Che 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! :)
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); }
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
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.