You need to sign in to do that
Don't have an account?
Kathryn Bullock
No errors but code to send email does not work
I have a code that I am hoping will send an email to the X3_Party_Installer__r.Email and will create an activity on the record as well. Unfortunately, there are no problems in the code, but I can't get it to work. Please let me know if you have suggestions!!
Controller:
VF Page:
Controller:
public class SendSiteSurveyEmailController { public String Site_Survey {get;set;} Public SendSiteSurveyEmailController(ApexPages.StandardController controller) { Site_Survey = ApexPages.currentPage().getParameters().get('Id'); } Public Pagereference SendSiteSurveyFunction() { list<Site_Survey__c> sitelist = [SELECT X3rd_Party_Installer__c, X3rd_Party_Installer__r.email FROM Site_Survey__c LIMIT 1]; String toaddress= sitelist[0].X3rd_Party_Installer__r.email; try{ Messaging.SingleEmailMessage mail = new Messaging.SingleEmailMessage(); String[] addr = new String[] {toaddress}; mail.setToAddresses(addr); mail.setReplyTo(toaddress); mail.setSenderDisplayName('Name'); mail.setSubject('DarPro Site Survey'); mail.setBccSender(false); mail.setUseSignature(true); mail.setHtmlBody('<b> BODY </b>'); List<Messaging.EmailFileAttachment> fileAttachments = new List<Messaging.Emailfileattachment>(); for (Attachment a : [SELECT Name, Body, BodyLength FROM Attachment WHERE ParentId = :Site_Survey Order By LastModifiedDate DESC Limit 1]){ Messaging.Emailfileattachment efa = new Messaging.Emailfileattachment(); efa.setFileName(a.Name); efa.setBody(a.Body); fileAttachments.add(efa); //mail.setFileAttachments(new Messaging.SingleEmailMessage[] {efa}); } mail.setFileAttachments(fileAttachments); Messaging.sendEmail(new Messaging.SingleEmailMessage[] { mail }); Contact ct = [SELECT id FROM Contact WHERE Email = :toaddress LIMIT 1]; Task newTask = new Task(Description = 'Site Survey Email', Priority = 'Normal', Status = 'Completed', Subject = 'Site Survey', Type = 'Email', WhoId = ct.Id); } catch(Exception e) {} PageReference reference = new PageReference('https://darlingingredients--griffsdbx.cs11.my.salesforce.com/'+Site_Survey); reference.setRedirect(true); return reference; } }
VF Page:
<apex:page standardController="Site_Survey__c" extensions="SendSiteSurveyEmailController"> <apex:form > <script type="text/javascript"> function init() { sendEmail(); } if(window.addEventListener) window.addEventListener('load',init,true) else window.attachEvent('onload',init) </script> <apex:actionFunction name="sendEmail" action="{!sendSiteSurveyFunction}"> </apex:actionFunction> </apex:form> </apex:page>
All Answers