/
Automatically update the custom field ID and/or name in filters

Automatically update the custom field ID and/or name in filters

Problem

When you're replacing an old custom field with a new one for different reasons, updating it in various places can be challenging. This task might be so difficult that it stops the change from happening since too much work is needed. This script is designed to help by automatically updating the custom field ID and name in saved filters. It makes things easier by handling these updates alongside the changes you make.

  • Users personal filters

  • Subscriptions

  • Filters used in dashboards

  • Filters used un Agile boards

Solution

Creating a SIL script file

To create a SIL script file,

  1. Log in as a Jira administrator

  2. Navigate to Administration > Add-ons > Power Plugins Configuration > SIL Manager. Right-click the silprograms folder in the left panel of the SIL Manager, and click New.

     

  3. Select the New > File option and type the file name of your new script. Add the .sil extension, save the Name by checking the input box, and enter this SIL™ code into the right panel.

     

    updateFilters.sil

    /******************************************************************************* The following script will update ALL filters in Jira to use a new custom field id or name. This is useful when a new custom field is created to take the place of an old one. This method avoids using database queries to perform the change which can lead to data corruption. *******************************************************************************/ //setting up struct debug { boolean update_id; boolean update_name; boolean test_mode; boolean print_log; } //flags used to test and control behavior of the script debug goldBug; goldBug.update_id = true; goldBug.update_name = false; goldBug.test_mode = true; //<<----- this must be changed in order to persist changes goldBug.print_log = true; /************************CUSTOM FIELD ID - INPUT REQUIRED**********************/ string oldCustomFieldId = "12345"; //enter old custom field id string newCustomFieldId = "67891"; //enter new custom field id /********************CUSTOM FIELD NAME - INPUT REQUIRED************************/ string oldCustomFieldName = "Old Field Name"; //enter old name string newCustomFieldName = "New Field Name"; //enter new name //fields used by CSV log output string CSV = "Times Stamp, Filter Id, Filter Name, Filter Owner, Old Query, New Query, Update Required, Updated\n"; string format = "yyyyMMdd_hhmmss"; //get all filters and loopt through them JFilter []filters = admGetAllFilters(); for(JFilter f in filters) { //build line for CSV output log string temp_row = formatDate(currentDate(), format) + ", " + f.id + ", \"" + f.name + "\", " + f.owner + ", \"" + f.query + "\", "; //get query from filter JFilter updatedFilter = f; string newQuery = f.query; //update custom field id in query if(goldBug.update_id) { newQuery = replace(newQuery, "cf[" + oldCustomFieldId + "]", "cf[" + newCustomFieldId + "]"); } //update custom field name in query if(goldBug.update_name) { newQuery = replace(newQuery, oldCustomFieldName, newCustomFieldName); } //update flags used in CSV log boolean updateRequired = false; boolean success = true; //did a change occur??? if(f.query == newQuery) { //no change to filter was requires since it did not include the modified custom field string HTML = "Filter \"" + f.name + "\" (" + f.id + ") was <strong>not modified</strong> since it did not contain the field that requires updating."; runnerLog(HTML, true); } else { //filter was changed! updatedFilter.query = newQuery; updateRequired = true; string HTML; //container for user output //push update to filter if(goldBug.test_mode != true) { success = admUpdateFilter(updatedFilter); } else { HTML = "<strong> TEST MODE: </strong>"; //mark output with test mode text if running in test mode } //output to display to user if(success) { //whoopie! HTML = "Filter \"" + f.name + "\" (" + f.id + ") has been <strong style='color: green'>successfully</strong> updated."; //:) } else { //awwww crap! HTML = "<strong style='color: red'>ERROR:</strong> Filter \"" + f.name + "\" (" + f.id + ") could not be updated for an unknown reason."; //:( } runnerLog(HTML, true); } //add row to CSV log output CSV += temp_row + "\"" + newQuery + "\", " + updateRequired + ", " + success + "\n"; } //print CSV output log //right click silprograms folder and select 'refresh' to see the new file if(goldBug.print_log) { printInFile("filterUpdateLog_" + formatDate(currentDate(), format) + ".csv", CSV); }
  4. Update the required information on lines 24-28.

  5. Click Check to validate the syntax and Save your file.

  6. Click Run to execute the script.

  7. Check the output in the console.

 

Related content