[SalesForce] List View Custom Button to Collect Values of Selected Records

I want to create a JavaScript button to use on list views that when clicked captures a list of the values of a field on all the selected records. GETRECORDIDS returns a set of IDs of selected records. I want to get a set of values that are in a certain field.

For example, if I put the button on a list view of contacts, I would want to get a list of all the email addresses for the selected contacts. Not all of the users have API enabled so I can't query the values after collecting the IDs.

Is there a way for me to get a set of all the email addresses using a JavaScript button?

Best Answer

You can start with something like this:

var parentForm = this;
while(parentForm.nodeName.toLowerCase() != 'form'){
    parentForm = parentForm.parentNode;
}

// we have the form, the table is in 2nd div inside it
var table = parentForm.lastChild.getElementsByTagName('table')[0].getElementsByTagName('tbody')[0];
var rows = table.getElementsByTagName('tr');

//alert(rows.length); // this contains only visible rows (5 by default)!

// first row we can skip, it's a header row really.
var columnindex = 1;
var values = [];

for(var i = 1; i < rows.length; ++i){
    var row = rows[i];
    if(row.firstChild.firstChild.checked){
        var value = row.getElementsByTagName('td')[columnindex].innerHTML;
        values.push(value);
    }
}

if(values.length > 0){
    alert(values.join("\r\n"));
} else {
    alert('nothing was selected');
}

It's essentially screenscraping though and very limited:

  1. If related list has 5+ items but user's preference is to display only 5 - bad luck.
  2. It has hardcoded column index so you must be very careful if you'll ever edit the related list (or expand the code so it checks the header row)
  3. Did I mention it's screenscraping?