Saturday, November 1, 2014

Custom Salesforce Button to Execute JavaScript in List Views

Custom Salesforce Button to Execute JavaScript in List Views


Hi,

I have one requirement that is create a custom button on Leads List View, once the user clicks on that button the selected leads should update with lead status as Unqualified.

For this i created one list view button with Execute JavaScript and place it in Search Layouts (Leads Listview).

Step1:

Go to leads ==> Click on Buttons,Links and Actions ==> Click on New Button or Link.



JavaScript Code to paste in the Window.

//adds the proper code for inclusion of AJAX toolkit
{!REQUIRESCRIPT("/soap/ajax/19.0/connection.js")}
//string for the URL of the current page
var url = parent.location.href; 
//grabs the Lead records that the user is requesting to update
var records = {!GETRECORDIDS($ObjectType.Lead)}; 
//array for holding records that this code will ultimately update
var updateRecords = []; 
 //if the button was clicked but there was no record selected
if (records[0] == null) {
        //alert the user that they didn't make a selection 

alert("Please select at least one record to update."); 
} else { //otherwise, there was a record selection
for (var a=0; a<records.length; a++) { //for all records
//create a new sObject for storing updated record details
var update_Lead = new sforce.SObject("Lead"); 
//set the Id of the selected Lead record
update_Lead.Id = records[a]; 
 //set the value for Status to 'Unqualified'
update_Lead.Status = "Unqualified";
updateRecords.push(update_Lead); 
//add the updated record to our array
}
 //push the updated records back to Salesforce
result = sforce.connection.update(updateRecords);
parent.location.href = url; //refresh the page

}


Step 2 :

Click on Save.

Step 3: 

Go to Leads Object==> Search  Layouts==>Edit Leads List View==>Select the Newly created list view button and save.




Step 4: 



That's it.. Thanks for reading.







4 comments:

Ben Morch said...

Great post! I have a similar situation, but I want to send the list of selected records to a class so that I can do more then just update one field. Is there a way to pass the updateRecords to a class method as a parameter?

Unknown said...

in this only able to update status to unqualified. But I want to make it dynamic what approach should i use?

Unknown said...

want to make it dynamic

radha said...

well explained.Keep updating salesforce Online Course

Post a Comment

 
| ,