Tuesday, 6 September 2016

Launch Outlook (or your default email client) From Salesforce Button


Sometimes your wants a functionality where they can launch their default email client (e.g. Outlook etc.) from a Salesforce button. The requirement can be further extended to pre-populate To, CC or BCC email addresses. Here is how to do it in both scenario i.e. static email adresses and dynamic email addresses:

STATIC EMAIL ADDRESSES: When you know whom you want to send email via outlook you can pre-populate To, CC and BCC email address. 

1. Create a custom button and make sure content source is URL


copy and paste the below string. You can modify the addresses, subject and body as per requirement.


mailto:client@salesforce.com?cc="cc@salesforce.com"&bcc="bcc@salesforce.com"&subject="This_is_my_subject"&body="Body_Text"

You can change the to, cc, bcc addressess as needed.


DYANAMIC EMAIL ADDRESSESS:

Lets say you want a button on account detail page which opens outlook will all contact email addresses pre-populated in BCC section of outlook. In such cases we need to create a javascript button. We will also need some apex/javascript logic to get all the contact email addresses. I am using Apex code but you can write your logic in javascript too.

1. Create a global APEX Class which has a method which returns colon separated email addresses of contacts. It should look like this:

global class AccountContactsEmailFetchService {


    webservice static String getContactEmails(String companyName){
       
        String subscriberEmails = '';
       
        //logic to create colon separated string of emails

  
        return subscriberEmails;
    }
}


2. Create a Javascript button (behaviour = execute javascript) and paste the following code:

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

var companyNameParam = '{!Company__c.Name}';

var subscriberEmails = 
sforce.apex.execute("AccountContactsEmailFetchService ","getContactEmails",
{companyName: companyNameParam});

var urlStr = "mailto:client@salesforce.com?cc=cc@salesforce.com&bcc=";

urlStr = urlStr.concat(subscriberEmails);

urlStr = urlStr.concat("&subject=SUBJECT_GOES_HERE&body=Body_Text");

window.location.href = urlStr;


Save this button and add it to layout to see the magic. It will automatically open outlook with emails populate in bcc.

Let me know if you run into any issues. Leave a comment and I will try to respond ASAP