Saturday, June 20, 2015

Mass Delete SF picklist values

In our last release, I need to remove about 260 picklist values. If I do it manually, I need to open 420 pages and click 680 buttons. Loading a SF page is not like to open Google search home page, it is very slow. I might need half day to finish it. But if in future we need to remove thousands picklist values, it might be not a good idea to do it manually. So I spent half day in weekend to automate this. 

My idea is from this blog http://frombelvideres4thfloor.blogspot.se/2011/05/javascript-hack-to-mass-delete-custom.html. But the Javascript in this blog cannot be used in the situation if the system has some records.  If the object has records in the system, and when deleting a picklist value, SF requires user to choose which value should replace the deleted one as the page shown below:

But this blog gives me a good idea to think how to automate this process.  Following is introducing my solution.
Requirements:
Process explanation:
To delete a picklist value, we need Login in to SF, go to the field detail page, click del link and goes to replace value page and click Save. I use Selenium IDE to automate this process. But only use Selenium IDE, it is difficult to only delete the picklist values we want to delete. What's more, in field detail page,  it has several section and each section contains "Del" link as shown below. Only by using Selenium IDE, it is not easy to only click the "Del" link in active picklist values section (in below picture, it is Campaign Type Picklist values).


So I use Javascript to define accurate deleting operation. only delete the choices we want to remove. 

Following is the Selenium IDE process:

Following is the flow chart for above Selenium Test Case:


Javasccript in runScriptAndWait command element:
var links = document.getElementsByTagName("a");
/* picklist values need to be deleted */
var delArray= ["value 0", "value 1", "value 2"];
/* Pick out the Del links. */ 
var delLinks = new Array();
for (
var i = 0; i < links.length; i++) {
   
var  link = links[i];   
   
var  title = link.title;
   if (link.innerHTML == "Del") {
     for(
var  j=0; j<delArray.length; j++){
          if(title.indexOf(delArray[j]) > -1){
               delLinks[delLinks.length] = link;
               break;
          };
      }
     }
  
     if(delLinks.length > 0)
          break;
}
if(delLinks.length > 0){  
/* click the del link for the pick list value that need to be deleted*/ 
  for (
var  i = 0; i < delLinks.length; i++) {
    
var  delLink = delLinks[i];
    window.open(delLink.attributes['href'].value, "_self");
  }
}


Run this tool
  • Install Firefox Selenium IDE and Selenium flow-control 
  • Login SF in Firefox
  • open Selenium IDE, download and open the test case
  • Change the loop time in "while" command. currently, it is 20, so when this test case runs, it deletes at most 20 picklist values. 
  • download and open javascript file in a txt or notepad++, modify the 3rd line, replace the value you need to delete in delArray. After modification, replace the new js in target of runScriptAndWait of Selenium IDE test case
  • Save the test case and click run button of Selenium IDE

Others
  • This tool only deletes the pick list value, won't update any existing records with new value. 
  • if you want to run this on sandbox, change the URL in "open" command
  • We might need to restart this test case several time as Firefox is easy to crash when running this. 
  • Source code of Selenium test case and javascript can be downloaded from here.




No comments:

Post a Comment