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
Victor19Victor19 

Custom Button to send email

Hi,

 

I am trying to create a custom button on the Case object which when clicked sends out an email. I need the email to be sent out to email address populated in a custom field. I will be using 2 Email templates and would like to have an If condition in my code so that it sends out an Email to one of the two custom Email fields (Test Email 1 or Test Email 2) based on the condition.

 

For example: Condition 1: When Type = Employee, and button is clicked Email has to be sent out to the Email address populated in Test Email 1 field with Email Template 1.

                         Condition 2: When Type = Contractor, and button is clicked Email has to be sent out to the Email address populated in Test Email 2 field with Email Template 2.

 

Type is a drop down field.

 

I would really appreciate if someone can give me any suggestions or share some code!

 

Thanks!

Best Answer chosen by Admin (Salesforce Developers) 
jayjaysjayjays

Well, normally you would think just write this in the controller code but how about creating a formula field so that you can reference the email to send to in other code easily.  Alternatively you could write some code in a separate class but it's always best to use declarative coding if you can, hence the formula field.

 

Create a text formula field named say "Email To Send To" and the formula say;

 

CASE( Type ,
"Employee", TestEmail1__c,
"Other", TestEmail2__c,
TestEmail2__c)

 

Then change your code to reference this field instead of TestEmail1__c:

 

<apex:page Standardcontroller="Case" extensions="SendemailController1">

<apex:outputText rendered="false">{!Case.Email_To_Send_To__c}</apex:outputText>
<apex:form >
<apex:commandButton value="Send Email" action="{!sendEmailFunction}"/>
</apex:form>
</apex:page>

 

 

Public class SendemailController1{
private final Case cc;
public SendemailController1(ApexPages.StandardController controller)
{
cc = (Case)controller.getRecord();
}
public void sendEmailFunction(){
Messaging.SingleEmailMessage mail = new Messaging.SingleEmailMessage();
String[] toAddresses = new String[] {cc.Email_To_Send_To__c};
mail.setToAddresses(toAddresses);
mail.setReplyTo('myemail@mail.com');
mail.setSenderDisplayName('My Name');
mail.setSubject('Testing email through apex');
mail.setTemplateId('00XU0000000llZ9');
Messaging.sendEmail(new Messaging.SingleEmailMessage[] { mail });
}
}

All Answers

sambasamba

You will have to create a class, and two webservice methods in this class. Then according to different type call different webservice.

 

This is class format below:

 

global class Test

{

      webService static boolean sendEmail1()

      {

           return true;

      }

      webService static boolean sendEmail2()

      {

           return true;

      }

}

 

In oder to call this webService. you need to add some javascript to your button.

{!REQUIRESCRIPT("/soap/ajax/16.0/connection.js")} 

{!REQUIRESCRIPT("/soap/ajax/16.0/apex.js")} 

 

Hope this helps.

 

 

Thanks,

Samba

jayjaysjayjays
Hi Victor,

Does this have to be when a button is clicked or do you have criteria for when the emails should be sent, for example when the case status is a certain value? If so, you can do this using 2 workflow rules and email alert actions. On the email alert you can select the template and the recipient can be set as an email field which would work in your scenario. It would be good to try this before writing any code.

Thanks,
James
Yoganand GadekarYoganand Gadekar

In the below example you can modify accordingly and use it on case object:

http://cloudforce4u.blogspot.in/2013/07/send-email-in-apex-salesforce.html

 

Mark this as answer and hit kudos if it helps you out.

 

Victor19Victor19

Hi Yoganand,

 

I noticed the example has hard-coded email addresses. How do I pull out the email address stored in a custom field. I don't want to hard code email address as each case can have a specific email address.

 

I would really appreciate if you could help me out.

 

Thanks!

Vic

 

 

Victor19Victor19

Yoganand, 

 

I am getting this error: SendemailController1 Compile Error: Initial expression is of incorrect type, expected: String at line 9 column 47

 

Below is my code:

 

VF:

<apex:page Standardcontroller="Case" extensions="SendemailController1">
<apex:form >
<apex:commandButton value="Send Email" action="{!sendEmailFunction}"/>
</apex:form>
</apex:page>

 

Apex:

Public class SendemailController1{
Case cc;
public SendemailController1(ApexPages.StandardController controller)
{
cc = (Case)controller.getRecord();
}
public void sendEmailFunction(){
Messaging.SingleEmailMessage mail = new Messaging.SingleEmailMessage();
String[] toAddresses = (new String[] {Case.TestEmail1__c});
mail.setToAddresses(toAddresses);
mail.setReplyTo('myemail@mail.com');
mail.setSenderDisplayName('My Name');
mail.setSubject('Testing email through apex');
mail.setTemplateId('00XU0000000llZ9');
Messaging.sendEmail(new Messaging.SingleEmailMessage[] { mail });
}
}

 

Thanks!

jayjaysjayjays

Hi,

 

you need to reference the field in the vf page for it to be available in the controller and also reference cc not Case to get the properties of the record.

 

Thanks,

James.

 

<apex:page Standardcontroller="Case" extensions="SendemailController1">

<apex:outputText rendered="false">{!Case.TestEmail1__c}</apex:outputText>
<apex:form >
<apex:commandButton value="Send Email" action="{!sendEmailFunction}"/>
</apex:form>
</apex:page>

 

 

Public class SendemailController1{
private final Case cc;
public SendemailController1(ApexPages.StandardController controller)
{
cc = (Case)controller.getRecord();
}
public void sendEmailFunction(){
Messaging.SingleEmailMessage mail = new Messaging.SingleEmailMessage();
String[] toAddresses = new String[] {cc.TestEmail1__c};
mail.setToAddresses(toAddresses);
mail.setReplyTo('myemail@mail.com');
mail.setSenderDisplayName('My Name');
mail.setSubject('Testing email through apex');
mail.setTemplateId('00XU0000000llZ9');
Messaging.sendEmail(new Messaging.SingleEmailMessage[] { mail });
}
}

Victor19Victor19

Thanks for the quick response James! I have an other question, I would like to include an IF condition in my code.

 

Basically, I have a Picklist field called Type and when Type = Employee, the email field has to be TestEmail1__c and when Type = Other, the email field has to be TestEmail2__c.

 

How do I incorporate the above functionality in my code.

 

Any help or feedback will greatly help me out.

 

Thanks!

jayjaysjayjays

Well, normally you would think just write this in the controller code but how about creating a formula field so that you can reference the email to send to in other code easily.  Alternatively you could write some code in a separate class but it's always best to use declarative coding if you can, hence the formula field.

 

Create a text formula field named say "Email To Send To" and the formula say;

 

CASE( Type ,
"Employee", TestEmail1__c,
"Other", TestEmail2__c,
TestEmail2__c)

 

Then change your code to reference this field instead of TestEmail1__c:

 

<apex:page Standardcontroller="Case" extensions="SendemailController1">

<apex:outputText rendered="false">{!Case.Email_To_Send_To__c}</apex:outputText>
<apex:form >
<apex:commandButton value="Send Email" action="{!sendEmailFunction}"/>
</apex:form>
</apex:page>

 

 

Public class SendemailController1{
private final Case cc;
public SendemailController1(ApexPages.StandardController controller)
{
cc = (Case)controller.getRecord();
}
public void sendEmailFunction(){
Messaging.SingleEmailMessage mail = new Messaging.SingleEmailMessage();
String[] toAddresses = new String[] {cc.Email_To_Send_To__c};
mail.setToAddresses(toAddresses);
mail.setReplyTo('myemail@mail.com');
mail.setSenderDisplayName('My Name');
mail.setSubject('Testing email through apex');
mail.setTemplateId('00XU0000000llZ9');
Messaging.sendEmail(new Messaging.SingleEmailMessage[] { mail });
}
}

This was selected as the best answer
Victor19Victor19

Thanks James! I am having no issues now!