You need to sign in to do that
Don't have an account?
Sending Email Via Controller
Hey Guys,
I am trying to make a button that sends an email. Will this work? Obviously it currently isn't or I wouldn't be asking :)
public with sharing class ACL_SendQuoteReviewRequest {
private ApexPages.StandardController controller;
public ACL_SendQuoteReviewRequest(ApexPages.StandardController controller) {
this.controller = controller;
Messaging.SingleEmailMessage mail = new Messaging.SingleEmailMessage();
String[] toAddresses = new String[] {'name@domain.com'};
mail.setToAddresses(toAddresses);
mail.setReplyTo('email@domain.com');
mail.setSenderDisplayName('Salesforce Support');
mail.setSubject('Subject');
mail.setBccSender(false);
mail.setUseSignature(false);
mail.setPlainTextBody('Body');
mail.setHtmlBody('<b>HTML Body,/b>');
Messaging.sendEmail(new Messaging.SingleEmailMessage[] { mail });
}
}
And the VF page is quite simple.
<apex:page standardController="SFDC_520_Quote__c" extensions="ACL_SendQuoteReviewRequest"></apex:page>
Hi BCSFDC1974;
Your code has no errors. But I don't think that you can send a mail by calling the mathod in the constructor(Remember DML operations are also not allowed in the constructor.)
Move your code in to a seperate method and then try to call that method in a button action.
All Answers
Hi BCSFDC1974;
Your code has no errors. But I don't think that you can send a mail by calling the mathod in the constructor(Remember DML operations are also not allowed in the constructor.)
Move your code in to a seperate method and then try to call that method in a button action.
Forgive the brain fart :) Your suggested work beautifully!
So it's working fine except when any other user tries to click the button they get a access denied...any ideas? Here's the code....
public with sharing class ACL_SendQuoteReviewRequest {
private ApexPages.StandardController controller;
public string sID { get; set; }
public void sendEmail() {
Messaging.SingleEmailMessage mail = new Messaging.SingleEmailMessage();
String[] toAddresses = new String[] {'email@domain.com'};
mail.setToAddresses(toAddresses);
mail.setReplyTo(UserInfo.getUserName());
mail.setSenderDisplayName(UserInfo.getFirstName() + ' ' + UserInfo.getLastName());
mail.setSubject('Quote Review Requested');
mail.setBccSender(false);
mail.setUseSignature(false);
mail.setPlainTextBody(UserInfo.getFirstName() + ' ' + UserInfo.getLastName() + ' wants the folllowing quote reviewed, https://test.salesforce.com/' + sID);
Messaging.sendEmail(new Messaging.SingleEmailMessage[] { mail }); }
public ACL_SendQuoteReviewRequest(ApexPages.StandardController controller) {
this.controller = controller;
this.sID = ApexPages.currentPage().getParameters().get('Id'); }}
<apex:page standardController="SFDC_520_Quote__c" extensions="ACL_SendQuoteReviewRequest" action="{!sendEmail}"> Request Sent! <br><br> <a href="https://tapp0.salesforce.com/{!sID}">Return to Quote</a></apex:page>
Can you provide the Test Method for this code? I have a very similar code and I am not able to make it work over 75%. Please post the test method!! Thank you.