Monday, February 23, 2015

Mass Update Button on Lead List View


In many companies we face challenges where business is not comfortable using data loader and other tools to perform mass update on day to day basis. It can be very tiring to manually mass update the records and perform same job everyday. How about we have a magical button that does the job with one click. Yes, salesforce does provide few standard buttons for mass update. You will find buttons like New lead, Change owner, Change status, Add campaign, Accept etc. on leads list view.


It is also true that these standard buttons have limited functionality to update specific characteristic of the records like “Change owner” can mass update only the owner for the selected records.

Usually we see that coding stuff is handled by the developers, however admins can still try their hands on some basic stuffs like creating a custom button by taking reference of online resources. In this post I will discuss how I was able to take help of online code and built a custom button to do mass update on leads. 

Given below is the scenario:

ABC corporation has leads coming in  from various sources like - emails, trade shows, conferences, events, web forms etc. On day to day basis, tele-business reps work on more than 100 leads a day. This team plays significant role in nourishing cold leads and then hands them over to the Sales team. The folks have come to you discussing their pain point of how they spend hours of time manually updating the leads. Another pain point  is that since multiple teams are involved in nourishing the leads,  defining the ownership is a challenge given that there is only one lead owner field.

There may be a better way to do this, but I followed following approach:

Step 1: To clearly define the ownership let us create a custom lookup field - “Tele Service Rep” on the lead object. Tele business lookup field will show who owns this lead from their team whereas lead owner will  define the sales representative working on the lead from the sales team.

Step 2: Tele business team require a mass update button on the list view that can help them to quickly update and take the ownership of leads. This will help in the accelerated uptake, turnovers and turnarounds.

Note: A list view displays records that meets the current search criteria to which the user has access to. List buttons allow users to select multiple items in the list and perform an action on all of them at once.Buttons display at the top of the details page.

Step 3: Create a New Custom lookup field “ Tele business Rep”


Step 4: Select lookup as data type



Step 5: Choose related object as Lead

Step 6: Enter Label for the lookup field

Step 7: Select Field level security 


Step 8: Add your new field to the lead page layout

Step 9: Your new lookup field is available on your lead object

Step 10: Let us create a custom button for Tele-service team

Go to Setup--> Leads-->Buttons Links and Actions 

Step 11: Create New Button or Link. Select List Button. 

This option creates a new button that you can add to the list view of lead or related list.  



Step 12: Select the behavior, behavior defines what should happen when the user clicks the button. Select the behavior as “Execute the JavaScript”. This option executes your OnClick JavaScript code when the user clicks your button or link. 


Step 13: Paste the code as given below. It is very important to check if there are any syntax error. Click on “Check Syntax”


Step 14: Select your merge fields from the insert field drop down. 


Step 15: Now we need to add the button we created to the lead list view.

Go to Setup --> Lead--> Search Layout--> Lead List view

Step 16: Add the custom button and save.



Step 17: Go to the Lead list view and validate the button's functionality. 


You are done. Enjoy your new custom button that can mass update leads in seconds. 



CODE

{!REQUIRESCRIPT("/soap/ajax/31.0/connection.js")} //adds the proper code for inclusion of AJAX toolkit
{!REQUIRESCRIPT("/soap/ajax/31.0/apex.js")}
var url = parent.location.href; //This is a string for the current page URL
var records = {!GETRECORDIDS($ObjectType.Lead)}; //Picks the Lead records that the user is requesting to update
var updateRecords = []; //array for holding records that this code will ultimately update
if (records.length == 0) { //if the button was clicked but there was no record selected
alert("Please select at least one record to update."); //We want to alert the user to select at least one record to update
} else { //otherwise, there was a record selection

var r = confirm("You are about to change the Tele Service Rep on " + records.length + " records. Continue?"); //Alert the user about the update
if (r == true) { \\If user selects ok to update the records
for (var a=0; a<records.length; a++){ //for all records
var update_Lead = new sforce.SObject("Lead"); //create a new sObject to store the updated lead record details
update_Lead.Id = records[a]; //set the Id of the selected Lead record
update_Tele_Business_Rep__c = '{!$User.Id}'; //Update the Tele business rep lookup field to the current user who clicks the button 
updateRecords.push(update_Lead); //add the updated record to our array
}
result = sforce.connection.update(updateRecords); //push the updated records back to Salesforce
var numSuccess = 0; //The integer value of var on success
var numFails = 0; //The integer value of var on failure
var failMessage = "";
for (var i=0; i<records.length; i++) {
if (result[i].getBoolean("success")) {
numSuccess = numSuccess + 1;
} else {
numFails = numFails + 1;
 if (numFails==1){
 failMessage=result[i].errors.message;
 }
}
}
if (numFails > 0) {
alert("Successes: " + numSuccess + " Failures: " + numFails + "\nFirst Error: " + failMessage);
} else {
alert("Successes: " + numSuccess + " Failures: " + numFails);
}
}
parent.location.href = url;
}

12 comments: