Friday, January 24, 2014

Salesforce.com: Send an Email to a list of Contacts Using Javascript

List view in Salesforce can help users accomplish activities in a faster way. You can do a Mass Edit / Update, Mass Transfer records from a list view. As of now, There is no out of the box functionality available to send an email to a list of contacts from Salesforce. (Mass mail is a completely different process. You need to create a separate list view and requires an email template)

Below mentioned code is a simple example in which Javascript is used to copy the email addresses of contacts selected from a list view and puts it into the "TO" box on a new email message.

This functionality can be handy when a user wants to send a simple mail to his contacts. You can optionally tag the activity to any object (Account, Opportunity, Case etc.)


For implementation:

Part I
Go to Setup > Customize > Contacts > Buttons, Links & Actions > New Button or Link
Choose Display Type as List Button & click Display Checkboxes (for Multi-Record Selection)
Behavior > "Execute JavaScript"
Content Source > "OnClick JavaScript"
Paste this code & Click Save

{!REQUIRESCRIPT('/soap/ajax/29.0/connection.js')} 
{!REQUIRESCRIPT('/soap/ajax/29.0/apex.js')} 
try{ 
var arrRecordId = {!GETRECORDIDS( $ObjectType.Contact)}; 
var ret = window.location.pathname+window.location.search; 
var pcEmail = arrRecordId[0]; 
if(arrRecordId.length > 0){ 
var strRecIdList = ''; 
for(var i=0; i < arrRecordId.length; i++){ 
strRecIdList += "'" + arrRecordId[i] + "',"; 

strRecIdList = strRecIdList.substring(0, strRecIdList.length-1); 
var contacts = sforce.connection.query("SELECT Email FROM Contact WHERE Id IN(" + strRecIdList + ")"); 
if(contacts.done == 'true' && parseInt(contacts.size)>0){ 
var cEmail = ''; 
for(var j=0; j < contacts.records.length; j++){ 
if(contacts.records[j].Email != null && contacts.records[j].Email != ''){ 
cEmail += contacts.records[j].Email + "; "; 


cEmail = (cEmail.substring(0, cEmail.length-1)).toString(); 
URL = '/_ui/core/email/author/EmailAuthor?p2_lkid='+pcEmail+'&p24='+cEmail+'&retURL='+ret; 
window.location.href= URL; 



catch(e){ 
alert('An Error has Occurred. \r\nError: ' + e); 
}


Part 2

Go to Setup > Customize > Contacts > Search Layouts
Click Edit next to Contacts List View
Add the new custom button and Save.

Now Click on Contacts Tab and then a list view to an send email.

Sunday, February 17, 2013

Salesforce: Single screen entry for different records

Salesforce user interface is simple and intuitive. It is easy to create new records by clicking new button. But imagine if a sales executive has to create an account first, then enter a contact, opportunity, activity details. It will be a time consuming task. A single screen to enter this information will save some time, increases adoption and productivity.

Here is one example to enter Account, Contact, Opportunity and Activity details from one screen. This is just an example hence I have not written any test class. This page can be improved to display as a wizard.

Controller

public class multirecords {

public Account A {get; set;}
Public Contact C {get; set;}
Public Task T {get; set;}
Public Opportunity O {get; set;}

    public multirecords()
    {
        A = new account();
        C = new contact();
        T = new task();
        O = new opportunity();
    }
    
    public PageReference Save()
    {
        insert A;
        
        C.Accountid = A.id;
        insert C;
        
        T.whatid = A.id;
        T.whoid = C.id;
        insert T;
        
        O.Accountid = A.id;
        insert O;
        
        return new PageReference('/'+A.id);
    }

    public PageReference Cancel()
    {
        return new PageReference('/home/home.jsp');
    }
}


Entry Page



Visualforce



Monday, April 23, 2012

Salesforce: Custom Button with Javascript

Salesforce custom button with the javascript is a very exciting feature to implement. We received a requirement from our customer for converting Leads. User should convert Lead record only if the status is changed to "Qualified". Typically when a lead is converted, Salesforce automatically changes the lead status to Qualified. But there are some custom validation rules created for lead record which doesn't allow to complete the conversion process and display error message as validation rules on lead page are not fulfilled.

I created a custom "Convert" button with the help of Javascript and replaced with standard button on lead page layout. The javascript validates whether lead status is qualified or not and doesn't allow user to proceed if it is not qualified.

Code:
var status = "{!Lead.Status}";
if (status == "Qualified")
{
window.location = 'https://ap1.salesforce.com/lead/leadconvert.jsp?retURL=%2F{!Lead.Id}&id={!Lead.Id}';
}
else
{
alert('Please change the Lead Status to Qualified then click on convert');
}


Error Message: