Mass deleting picklist values in Salesforce with AJAX javascript hack

Posted by Johan on Saturday, April 20, 2013

I have written an updated version of this post Mass deleting picklist values in Salesforce with AJAX javascript hack (2018 version)

Today I was gonna do a small change in our Salesforce instance, namely replacing to 1100+ picklists with brand new values. Last time I did this I used Eclipse and just saved the file (Lead.object for example).

But I realized that only new values are added and old ones are not removed and as usual there are number of requests on success.salesforce.com for this feature but it is not implemented yet.

Anyways after Googling I found this article: http://frombelvideres4thfloor.blogspot.se/2011/05/javascript-hack-to-mass-delete-custom.html which basically solves the problem by opening new windows.

This seemed to put a lot of stress on my poor MacBook so I edited it to use synchronous AJAX requests instead.

/* 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;
  }
}

/* AJAX request 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];
  // Synchronous AJAX style
  xmlhttp = new XMLHttpRequest();
  xmlhttp.open("GET",delLink.attributes['href'].value,false);
  xmlhttp.send();
}

The last 3 lines are what I’ve changed and it will call all delete links on a picklist configuration page. To get it running just create a new bookmark in your browser.
javascript:(function()%7Bvar s = document.createElement(“script”); s.src = “https://superfredag.com/massdelete.js"; void(document.body.appendChild(s));%7D)()

And replace the url with where you put your javascript file, or use mine if you want to.

Then just go to a picklist edit page and click it, it will take a while to execute but you can see that things are happening by opening Developer Tools (I’m using Chromium) and go to the Network tab.

Use it with caution though, it worked for me but you never know when Salesforce changes the layout. Test in your staging environment first ;)

This helped me remove 1100+ picklist values with very little effort.

Big thanks to MARTY Y. CHANG for writing the original post.