You need to sign in to do that
Don't have an account?

Send email using a button
Hello all,
I have a small question here on how to send an email(with specific template) when a button is clicked.
Could anybody please help me out doing the following:
1. Create a button "Send Email" on opportunity page.
2. when the button is clicked, an email should be sent to the contact related to that opportunity with a specific email template.
Any small pointers how to do this will be appreciated.
Thanks a lot!
Also i wanted the "Additional To" field to be auto populated from one of the custom email fields from Opportunity.
Was able to make this happen by the following code:
location.replace('_ui/core/email/author/EmailAuthor?p2_lkid={!Opportunity.Primary_ContactId__c}&rtype=003&p3_lkid={!Opportunity.Id}&rtype=003&p24={!Opportunity.Broker_s_Email__c}&retURL=/{!Opportunity.Id}&template_id=00XA0000000Dv41')
Thanks,
VK86
All Answers
You can call a VF Page/S-Control on the click of the button and get the requisite contact ID through a query.
You can then re-direct the user to the standard send email page, with the "To" field pre-populated by passing the queried contact ID in the url paramter!
Cool_D
Thanks for ur reply Cool_D.
I am a newb and will be very much happy if you could drop a pseudo code to implement this.
Thanks a lot!
PFB the class and VF page for send email functionality, you can use this to extend it your opps. You might need to make changes to use it for your custom functionality. Hope this helps!
Apex Class
Public class SendEmail
{
public String subject {get; set;}
public String body {get; set;}
private opportunity opp;
// Constructor to populate instance of your object
public SendEmail(ApexPages.StandardController controller) {
this.opp= (opportunity )controller.getRecord();
opp= [SELECT opp fields required
FROM opportunity
WHERE id = :ApexPages.CurrentPage().getParameters().get('id')];
}
public Opportunity getOpportunity(){
return opp;
}
public pageReference send(){
Messaging.SingleEmailMessage email = new Messaging.SingleEmailMessage();
email.setSubject(subject);
email.setToAddresses(address);//Use SOQL to retrieve addresses in the address
email.setPlainTextBody(body);
email.setBccSender(true);
Messaging.SendEmailResult [] r = Messaging.SendEmail(new Messaging.SingleEmailMessage[] {email});
for ( Messaging.sendEmailResult result : r ) {
if ( !r[0].isSuccess () ) {
System.debug ( result );
}
else{
ApexPages.Message msg = new ApexPages.Message(ApexPages.Severity.INFO, 'Email Sent Successfully' );
ApexPages.addMessage(msg);
}
}
return null;
}
}
VF Page
<apex:page StandardController="Opportunity" extensions="SendEmail">
<apex:pageblock title="">
<apex:pageMessages ></apex:pageMessages>
<apex:form >
<apex:outputLabel value="Subject" for="Subject"/>:<br/>
<apex:inputText value="{!subject}" id="Subject" maxlength="80"/>
<br/><br/>
<apex:outputLabel value="Body" for="Body"/>:<br/>
<apex:inputTextarea value="{!body}" id="Body" rows="10" cols="80"/>
<br/><br/><br/>
<apex:commandButton value="Send Email" action="{!send}"/>
</apex:form>
</apex:pageblock>
</apex:page>
Thanks for the reply Wm.
Will try this out. Hopefully i succeed.
Thanks.
Hello All,
I was able to create a buton and when it is clicked, the page redirects to the standard send email page with the fields auto populated.
Here is the code for everyone who need help with similar issues.
Code:
location.replace('_ui/core/email/author/EmailAuthor?p2_lkid={!Opportunity.Primary_ContactId__c}&rtype=003&p3_lkid={!Opportunity.Id}&rtype=003&retURL=/{!Opportunity.Id}&template_id=00XA0000000Dv41')
Thanks,
VK86
Also i wanted the "Additional To" field to be auto populated from one of the custom email fields from Opportunity.
Was able to make this happen by the following code:
location.replace('_ui/core/email/author/EmailAuthor?p2_lkid={!Opportunity.Primary_ContactId__c}&rtype=003&p3_lkid={!Opportunity.Id}&rtype=003&p24={!Opportunity.Broker_s_Email__c}&retURL=/{!Opportunity.Id}&template_id=00XA0000000Dv41')
Thanks,
VK86
Thanks for the code VK86. This is exactly what I was looking for.
One question on the Primary ContactID field. How are you populating this field on your Opportunity?
Just so there's no mis-set expecteations here, the solution marked in this thread is dependent upon observed but unsupported behavior. There is no guarantee that the parameters you are using (or the URL pattern) won't change in a given release as they are not part of any officially supported API.
The supported way to accomplish this task is to create a visualforce page that sits behind a custom button on opportunity that presents the respective reproduced form and sending email from an extension of the Opportunity StandardController in Apex.
True this is more work initially but you need not regression test this approach every release like you would (should) if you stick with the unsupported one which will be more over time..... unless you just "feel lucky" ;-)
Hi TeraMc. Primary ContactID field is populated manually on Opportunity.
Hi Mtbclimber,
I am new to visualforce/apex programming. Just to accomplish this task i used this code.
I strongly agree with you and hopefully i will come up with the standard solution (i.e., using visualforce pages) soon.
Thanks,
VK86
Hi,
How did you get the id of Additional TO field as p24?
My requirement is to pre populate in CC fielf. Hence need to know the id of CC field.
can you please help me?
Regards,
Manasa
that's great..but 'To..' field needs to take user given email id ..and send mail to that mail id.