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 Apex Design Patterns

  • Singleton: Minimizing object instantiation for improved performance and to mitigate impact of governor limits.
  • Strategy: Defining a family of algorithms, enscapsulating each one and making them interchangeable and selectable at runtime.
  • Decorator: Extending the functionality of an sObject in Apex.
  • Facade: Simplifying the execution of classes with complex interfaces (e.g. web service callouts).
  • Composite: Treating a group of objects in a similar manner to a single instance of that object.
  • Bulk State Transition: Efficiently tracking the change of a field value in a trigger and executing functionality based on this change.

Product, Pricebook and PricebookEntry

Product:

  • Product object is a catalog of items or services that your organization sells.
  • A product may contain one or more different sets of prices (PriceBookEntry).
  • Products are represented by Standard Object – Product2.

Pricebook:

  • Pricebook object store a list of products and services that your organization sells.
  • Each organization has one standard price book that defines the standard or generic list price for each product or service.
  • An organization can have multiple custom price books that can be used for specialized purposes, such as a discount price book, price books for different channels or markets, price books for select accounts or opportunities, and so on.
  • For some organizations, the standard price book might be the only price needed, but if you need to set up further price books, you can reference the standard price book when setting up list prices in custom price books.
  • PriceBooks are represented by Standard Object – Pricebook2.

PricebookEntry:

  • PricebookEntry object is a product entry (an association between a Pricebook2 and Product2) in a pricebook.
  • This object allows products to be linked to standard price book or custom price book with a price for a given currency.
  • One price book entry is linked to only one product.
  • One price book entry can only appear in one pricebook.
  • One price book entry can be used in multiple line items. This also further creates flexibility for users to set their own prices.
  • PricebookEntry are represented by Standard Object – Pricebookentry

Data Model: PricebookEntry is the junction object between Product and Pricebook.

1 Star2 Stars3 Stars4 Stars5 Stars (4 votes, average: 5.00 out of 5)
Loading...

Displaying Static Resources Image in Lightning Component

To reference a specific resource in component markup, we can use $Resource.resourceName within an expression. resourceName is the Name of the static resource. In a managed packaged, the resource name must include the package namespace prefix, like $Resource.yourNamespace__resourceName.

Sample Code:

<aura:component>
    <!-- Stand-alone static resources image file-->
    <img src="{!$Resource.resourceName}"/>
    <!-- Stand-alone static resources image file with Namespace-->
    <img src="{!$Resource.yourNamespace__resourceName}"/>
    
    <!-- Asset from an archive static resource image file-->
    <img src="{!$Resource.resourceName + '/assets/images/logo.png'}"/>
    <!-- Asset from an archive static resource image file with Namespace-->
    <img src="{!$Resource.yourNamespace__resourceName + '/assets/images/logo.png'}"/>
</aura:component>