Tag Archives: Custom Button

Send Email From a Custom Button in Salesforce

Sample Code:

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

var mail = new sforce.SingleEmailMessage();

mail.targetObjectId = "{!Contact.Id}";
mail.toAddresses = "{!Contact.Email}";
mail.templateId = "00X28000000Utjy";
mail.whatId = "{!Case.Id}";

var result = sforce.connection.sendEmail([mail]);

if (result[0].success == 'true') {
    alert("Email sent successfully.");
} else {
    alert("Email sending failed.");
}

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.

<?xml version="1.0" encoding="UTF-8"?>
<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="templateName" minOccurs="0" type="xsd:string" 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>

Salesforce Check Permission Set in Custom Button

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

var result = sforce.connection.query("SELECT Id FROM PermissionSetAssignment WHERE PermissionSet.Name = 'Sample_Permission_Set' AND AssigneeId = '{!$User.Id}'");
var psAssignment = result.getArray("records");

if (psAssignment.length === 1) {
    //Write your logic
} else {
    alert('You don't have access to this feature.');
}

Custom Clone Button in Salesforce

Salesforce provides Clone functionality for some standard objects(Standard Clone button), However some standard objects do not have this button. For this purpose of cloning we will need to create custom button that will perform the functionality of cloning.

This cloning functionality can be achieved by writing a javascript for this custom button.

As an example lets create a custom button “Clone” on Account object that will clone the record.

Simply override your custom button “Clone” with the following javascript and you will have your custom Clone button that functions exactly like standard clone button

{!REQUIRESCRIPT("/soap/ajax/22.0/connection.js")} 
window.parent.location.href="/{!Account.Id}/e?&clone=1&retURL=/{!Account.Id}";

Note: retUrl specifies the location where you want to be on press of back button.