Pages

Wednesday, June 28, 2017

Send Email from Custom Button Click

Objective

Sometimes, we get a requirement to send email from Record detail page, clicking on Custom Button (naming Send Email).

I was answering this question in Salesforce Stackexchange Send email on Custom button click? and thought that it might help others.

Solution

Here is an approach to handle this requirement.

Create a Javascript Custom Button (Detail Page) button and use the following code:


{!REQUIRESCRIPT("/soap/ajax/31.0/connection.js")} 
{!REQUIRESCRIPT("/soap/ajax/31.0/apex.js")}
var message = new sforce.SingleEmailMessage(); 

message.targetObjectId = "{!Contact.Id}";
message.toAddresses = "{!Contact.Email}";
message.templateId = "00Xq0000000HwSw"; //predefined email template
message.whatId = "{!Case.Id}"; 

var result = sforce.connection.sendEmail([message]); 
if(result[0].success == 'true') 
{ 
    alert("Email sent successfully"); 
} else 
{ 
    alert("Sending failed"); 
} 
alert(result);

Note: Only User, Contact, Lead, or Person Account objects are allowed for targetObjectId

If you need to refer specific attributes then download Partner WSDL and refer complexType SingleEmailMessage



<complexType name="SingleEmailMessage">
<complexContent>
<extension base="tns:Email">
<sequence>
<element name="bccAddresses" minOccurs="0" maxOccurs="25" type="xsd:string" nillable="true"/>
<element name="ccAddresses" minOccurs="0" maxOccurs="25" type="xsd:string" nillable="true"/>
<element name="charset" type="xsd:string" nillable="true"/>
<element name="documentAttachments" minOccurs="0" maxOccurs="unbounded" type="tns:ID"/>
<element name="entityAttachments" minOccurs="0" maxOccurs="unbounded" type="tns:ID"/>
<element name="fileAttachments" minOccurs="0" maxOccurs="unbounded" type="tns:EmailFileAttachment"/>
<element name="htmlBody" type="xsd:string" nillable="true"/>
<element name="inReplyTo" minOccurs="0" type="xsd:string" nillable="true"/>
<element name="optOutPolicy" type="tns:SendEmailOptOutPolicy" nillable="true"/>
<element name="orgWideEmailAddressId" minOccurs="0" maxOccurs="1" type="tns:ID" nillable="true"/>
<element name="plainTextBody" type="xsd:string" nillable="true"/>
<element name="references" minOccurs="0" type="xsd:string" nillable="true"/>
<element name="targetObjectId" type="tns:ID" nillable="true"/>
<element name="templateId" type="tns:ID" nillable="true"/>
<element name="toAddresses" minOccurs="0" maxOccurs="100" type="xsd:string" nillable="true"/>
<element name="treatBodiesAsTemplate" type="xsd:boolean" nillable="true"/>
<element name="treatTargetObjectAsRecipient" type="xsd:boolean" nillable="true"/>
<element name="whatId" type="tns:ID" nillable="true"/>
</sequence>
</extension>
</complexContent>
</complexType>

It's a fun! Share this post to others in case it helps!

6 comments:

  1. Hi, this is Arnold Jr.
    Thanks for posting my question on your blog. It feels amazing. :)

    ReplyDelete
    Replies
    1. Thanks to you for that question. This way we can learn, develop and share information to the community.

      Delete
  2. SPAM firewalls sift through spam, however they sadly are presumably causing up to 25% of your non-spam, authorizations based email promoting efforts to miss the mark concerning the inbox as well. This article, in an inquiry and answer frame, covers the best inquiries that email advertisers need to audit to better comprehend why their generally great email promoting efforts may get blocked.See more

    ReplyDelete
  3. Thanks a ton. Great article. Very useful.

    ReplyDelete
  4. Could you help me on writing code for Custom Object

    ReplyDelete