Wednesday, May 18, 2011

JavaScript Hack to Mass Delete Custom Picklist Values

As an extension of last night's efforts, I've created what appears to be a working JavaScript hack to delete all picklist values from a custom picklist. This simplifies maintenance of picklist values by allowing an admin to delete all the values and then simply re-add the ones that are desired.

The following code was tested in Chrome 11 on Windows 7.

javascript:

/* Gather all of the a elements on the page. */

var links = document.getElementsByTagName("a");

/* Pick out the Del links. */

var delLinks = new Array();
for (var i = 0; i < links.length; i++) {
  var link = links[i];
  
  if (link.innerHTML == "Del") {
    /*alert("Del link found!");*/
    /*alert(link.attributes['href'].value);*/
    delLinks[delLinks.length] = link;
  }
}

/* Open each Del link to delete the associated
   picklist value.
   
   This code can be augmented as desired
   to only delete certain values.
   
   However, for custom picklists it's probably
   easier to just delete all of the values
   and then re-add the desired values. */

for (var i = 0; i < delLinks.length; i++) {
  var delLink = delLinks[i];
  window.open(
      delLink.attributes['href'].value);
}

As with all hacks, please to use with caution, at your own risk.