Created
September 6, 2019 12:44
-
-
Save jonnyfiveconsulting/8967e475d593b14fb8257db505f4271d to your computer and use it in GitHub Desktop.
ServiceNow kbpocalypse: This script will find all dictionary entries that are using 2020-01-01 as a default date, and tell you the number of impacted records.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
/* | |
This script will find all dictionary entries that are using 2020-01-01 as a default date, and tell you the number of impacted records. | |
To actually make changes, you will want to change the update flag to true; | |
*/ | |
//change this flag to true if you want to update the dictionary entries and affected records | |
var update = false; | |
//Update this value as yyyy-mm-dd | |
var newDefaultDate = "2030-01-01"; | |
var gd = new GlideDate(); | |
gd.setValue(newDefaultDate); | |
gd.getDisplayValue(); | |
var gdt = new GlideDate(); | |
gdt.setValue(newDefaultDate+' 23:59:59'); | |
gdt.getDisplayValue(); | |
if(!update){ | |
gs.print("Since the update flag is false, we are not making upates to these records."); | |
} | |
var dictionaryEntry = new GlideRecord("sys_dictionary"); | |
dictionaryEntry.addEncodedQuery("default_valueLIKE2020-01-01^active=true^internal_type=glide_date^ORinternal_type=glide_date_time"); | |
dictionaryEntry.query(); | |
while(dictionaryEntry.next()){ | |
var table = new GlideRecord(dictionaryEntry.name.toString()); | |
table.addEncodedQuery(dictionaryEntry.element.toString()+"ON2020-01-01@javascript:gs.dateGenerate('2020-01-01','start')@javascript:gs.dateGenerate('2020-01-01','end')"); | |
table.query(); | |
if(table.hasNext()){ | |
gs.print("There are "+table.getRowCount()+ " "+dictionaryEntry.name.toString()+" records with a "+dictionaryEntry.element.toString()+" date of 2020-01-01"); | |
if(update){ | |
var newDate = dictionaryEntry.internal_type == "glide_date" ? gd.getDisplayValue() : gdt.getDisplayValue(); | |
while(table.next()){ | |
table.setValue(dictionaryEntry.element.toString(), newDate); | |
table.update(); | |
} | |
} | |
} else{ | |
gs.print("The "+dictionaryEntry.name.toString()+" table still has this default value, but doesn't have any records."); | |
} | |
if(update){ | |
dictionaryEntry.default_value = dictionaryEntry.default_value.replace(/2020-01-01/gi, newDefaultDate); | |
dictionaryEntry.update(); | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment