Sunday, December 6, 2015

Salesforce: Mass Update from List View using JavaScript

Sometimes back, we discussed on how to update Salesforce with JavaScript from a button in a page layout. Next, recently we have issue on how to replicate the same for many records to mass update from list view? Inline Editing in one the option to mass update records from a list view, read this blog for more information on how to use inline editing in list view.

But how about if we always need to mass update with the same data? It can be for only 1 field or for multiple fields, example: update Account type to Customer. Using inline editing may not always be the best option with all the limitations of inline editing.

We can use similar approach of using JavaScript to mass update many records from list view.
1. Create custom button with Java Script
2. Add the button to list view

For this blog, we will use scenario to mass update Account type to Customer.

1. Create custom button
  • Navigate to Setup | Customize | AccountsButtons, Links, and Actions
  • Click New Button or Link
  • Enter Label, select Display Type = List Button and enable Display Checkboxes (for Multi-Record Selection), Behavior = Execute JavaScript, Content Source = OnClick JavaScript
  • Copy and paste following script
 var records = {!GETRECORDIDS($ObjectType.Account)};   
 var updateRecords = [];   
 if (records[0] == null)   
  alert("Please select at least one Account")   
  for (var i=0; i<records.length; i++)   
   var acc = new sforce.SObject("Account"); = records[i];   
   acc.Type = "Customer";   
  result = sforce.connection.update(updateRecords);   
  window.location.href = "/001";   

2. Add the button to list view
  • Navigate to Setup | Customize | Accounts | Search Layouts
  • Click Edit on Accounts List View
  • Select the custom button created and move it to the right on Selected Buttons
  • Save and done

Note: if the record unable to save for some reasons, maybe validation rule, the error message will NOT display and user may not aware. But, admin can debug this with Debug Log.

This operation will consider as "Api" with Application pointing to the custom button id.