+ Start a Discussion
BCSFDC1974BCSFDC1974 

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>

 

 

 

 

 

Message Edited by BCSFDC1974 on 12-09-2009 09:51 AM
Message Edited by BCSFDC1974 on 12-09-2009 10:13 AM
Best Answer chosen by Admin (Salesforce Developers) 
prageethprageeth

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

prageethprageeth

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.

This was selected as the best answer
BCSFDC1974BCSFDC1974
Problem is this button is on a standard page and I do not want them to have to click another button. I just want it to fire when the first button is clicked.
prageethprageeth
Why can't tou call this method in your existing button.
BCSFDC1974BCSFDC1974

Forgive the brain fart :) Your suggested work beautifully!

 

BCSFDC1974BCSFDC1974

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>

 

 

Message Edited by BCSFDC1974 on 12-11-2009 11:40 AM
Message Edited by BCSFDC1974 on 12-11-2009 11:47 AM
companyDBCcompanyDBC

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.