function readOnly(count){ }
Starting November 20, the site will be set to read-only. On December 4, 2023,
forum discussions will move to the Trailblazer Community.
+ Start a Discussion
VK86VK86 

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! 

Best Answer chosen by Admin (Salesforce Developers) 
VK86VK86

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

Cool_DevloperCool_Devloper

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 

VK86VK86

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! 

WmWm

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>
                       

VK86VK86

Thanks for the reply Wm.

 

Will try this out. Hopefully i succeed.

 

Thanks. 

VK86VK86

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 

VK86VK86

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 

This was selected as the best answer
TeraMcTeraMc

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?

mtbclimbermtbclimber

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" ;-)

 

 

VK86VK86

Hi TeraMc. Primary ContactID field is populated manually on Opportunity.

 

 

VK86VK86

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 

 

Manasa.RManasa.R

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

lakshman.mattilakshman.matti

that's great..but 'To..'  field  needs to take user given email id ..and send mail to that mail id.