-
-
Save nancystodd/0e06645c80cb997b9e7b8cb193e2f194 to your computer and use it in GitHub Desktop.
ServiceNow code snippits
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
//Stick this in a background script and use to search scripts anywhere in the system. | |
var term = "search,terms"; //use comma to separate terms | |
var useAndQuery = true; //search for term 1 and term 2. If false, it will look for term 1 or term 2. | |
var debug = false; //set this true if you want to troubleshoot the script. | |
var delimiter = ","; //if you want to split terms on something other than a comma | |
var globalScriptSearch = { | |
search: null, | |
delimiter: ",", | |
out: ["\n"], | |
debug: false, | |
useAndQuery: true, | |
/* Main method that calls all the sub-methods.*/ | |
search: function(term, useAndQuery, debug, delimiter) { | |
if (typeof debug != "undefined") this.debug = debug; | |
if (typeof delimiter != "undefined") this.delimiter = delimiter; | |
if (typeof useAndQuery != "undefined") this.useAndQuery = useAndQuery; | |
if (typeof term != "string") { | |
gs.print("GlobalScriptSearch - ERROR: must pass a valid search term to search"); | |
return false; | |
} | |
this.search = term; | |
this._searchLocations(); | |
this._searchFieldTypes(); | |
this._searchWorkflow(); | |
gs.print(this.out.join("\n")); | |
return true; | |
}, | |
_searchLocations: function() { | |
var locations = { | |
"question": "default_value", | |
"sys_trigger": "job_context", | |
"sys_dictionary": "calculation", | |
"sys_dictionary_override": "calculation", | |
"sys_ui_macro": "xml", | |
"sys_ui_page": "html", | |
"sys_impex_entry": "default_value", | |
"content_block_programmatic": "programmatic_content", | |
} | |
for (var location in locations) { | |
this._searchLocation(location, locations[location]); | |
} | |
}, | |
_searchFieldTypes: function() { | |
var fieldList = new GlideRecord("sys_dictionary"); | |
var fieldTypes = ["script_plain", "script", "email_script", "condition_string", "conditions"]; | |
var aq = null; | |
for (var ic = 0; ic < fieldTypes.length; ic++) { | |
if (aq == null) | |
aq = fieldList.addQuery("internal_type", fieldTypes[ic]); | |
else | |
aq.addOrCondition("internal_type", fieldTypes[ic]); | |
} | |
this._searchFieldsNamedScript(aq); | |
//exclude all field types that don't come from String | |
var nonString = new GlideRecord("sys_glide_object"); | |
nonString.addQuery("scalar_type", "!=", "string"); | |
nonString.query(); | |
while (nonString.next()) { | |
fieldList.addQuery("internal_type", "!=", nonString.name + ""); | |
} | |
fieldList.addQuery("internal_type", "!=", "boolean"); | |
fieldList.query(); | |
while (fieldList.next()) { | |
if (fieldList.name.indexOf("var__") >= 0) | |
continue; | |
this._searchLocation(fieldList.name, fieldList.element); | |
} | |
}, | |
_searchFieldsNamedScript: function(addQuery) { | |
addQuery.addOrCondition("element", "ENDSWITH", "script"); | |
addQuery.addOrCondition("element", "STARTSWITH", "script"); | |
}, | |
_searchWorkflow: function() { | |
var allDocs = []; | |
var tableName = 'sys_variable_value'; | |
var fieldName = 'value'; | |
var rec = new GlideRecord(tableName); | |
rec.addQuery('document', 'wf_activity'); | |
var qc = rec.addQuery('variable.element', 'CONTAINS', 'script'); | |
qc.addOrCondition('variable.internal_type', 'CONTAINS', 'script'); | |
var terms = this.search.split(this.delimiter); | |
for (var ib = 0; ib < terms.length; ib++) { | |
if (this.useAndQuery) { | |
rec.addQuery(fieldName, 'CONTAINS', terms[ib]); | |
} else { | |
if (ib == 0) | |
var aq = rec.addQuery(fieldName, "CONTAINS", terms[ib]); | |
else | |
aq.addOrCondition(fieldName, "CONTAINS", terms[ib]); | |
} | |
} | |
this._addMatches(tableName, fieldName, rec); | |
}, | |
_searchLocation: function(table, field) { | |
var fieldName = field + ""; | |
var tableName = table + ""; | |
if (this.debug) | |
gs.print("_searchLocation: " + tableName + " " + fieldName); | |
var target = new GlideRecord(tableName); | |
if (!target.isValid()) { | |
gs.print("GlobalScriptSearch - ERRROR: " + tableName + " was an invalid table name (field: " + fieldName + ")."); | |
return; | |
} | |
var terms = this.search.split(this.delimiter); | |
if (this.debug) | |
gs.print("_searchLocation: terms.length " + terms.length); | |
if (this.useAndQuery) { | |
for (var ia = 0; ia < terms.length; ia++) { | |
target.addQuery(fieldName, "CONTAINS", terms[ia]); | |
} | |
} else { | |
var aq; | |
for (var ia = 0; ia < terms.length; ia++) { | |
if (ia == 0) | |
var aq = target.addQuery(fieldName, "CONTAINS", terms[ia]); | |
else | |
aq.addOrCondition(fieldName, "CONTAINS", terms[ia]); | |
} | |
} | |
this._addMatches(tableName, fieldName, target); | |
}, | |
_addMatches: function(tableName, fieldName, match) { | |
try { | |
match.query(); | |
if (match.getRowCount() < 1) return; | |
var matchList = []; | |
this.out.push("\n\n* Searching - " + tableName + "." + fieldName + " ***"); | |
while(match.next()) { | |
matchList.push(match.sys_id+""); | |
this.out.push(match.getClassDisplayValue() + " - " + match.getDisplayValue() + ": /" + tableName + ".do?sys_id=" + match.sys_id); | |
} | |
this.out.push("\n[All matches] /" + tableName + ".do?sysparm_query=sys_idIN" + matchList.join(",")); | |
} catch(e) { | |
gs.print("GlobalScriptSearch - ERRROR: failure while trying to insert match " + e); | |
} | |
}, | |
type: 'GlobalScriptSearch' | |
} | |
globalScriptSearch.search(term, useAndQuery, debug, delimiter); |
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
//hide tab (section) called variables. | |
g_form.setSectionDisplay('variables', false); |
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
function onLoad() { | |
//Type appropriate comment here, and begin script below | |
var isIMUser = g_user.hasRole('incident_manager'); | |
if (!isIMUser){ | |
var list = $$('div[tab_caption="Service Impacts"]')[0]; | |
if(list.hasClassName('embedded')){ | |
list.hide(); | |
} | |
//g_form.hideRelatedList(u_service_impact) | |
} | |
} |
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
doInsertWithUsers(); | |
function doInsertWithUsers() { | |
var oldid = current.sys_id.toString(); | |
var saveMe = current; | |
var id = current.setNewGuid(); | |
var userids = new GlideRecord('u_add_access_users'); | |
userids.addQuery('u_access_request',oldid); | |
userids.query(); | |
while (userids.next()) { | |
userids.u_access_request = id; | |
userids.u_lan_id = ""; | |
userids.insert(); | |
} | |
current.insert(); | |
action.setRedirectURL(saveMe); | |
} |
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
gs.print("Old Subcategory values"); | |
var count = new GlideRecord('incident'); | |
count.addQuery('sys_created_on', "<", "2016-04-01 08:00:00"); | |
count.addNullQuery('u_bmc_event_id'); | |
count.query(); | |
while (count.next()) { | |
//gs.print("Ticket Number = " + count.number + " ,Reported By = " + count.u_reported_by + " ,caller_id = " + count.caller_id + " ,assigned to = " + count.assigned_to); | |
count.u_sub_cat_temporary_field = count.subcategory; | |
count.update(); | |
} |
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
function onSubmit() { | |
var subsub = g_form.getValue('u_sub_subcategory'); | |
var comments2 = g_form.getValue('comments'); | |
var worknotes = g_form.getValue('work_notes'); | |
//Type appropriate comment here, and begin script below | |
if(comments2 == '' && worknotes == ''){ | |
g_form.flash("comments", "#FFFACD", 0); | |
g_form.flash("work_notes", "#FFFACD", 0); | |
alert("You need to add comments or work notes if you want to update the record"); | |
return false; | |
} | |
} |
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
var list = $$('div[tab_caption="Affected CIs"]')[0]; | |
if(list.hasClassName('embedded')){ | |
list.show(); | |
} |
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
function onBefore(current, previous) { | |
//This function will be automatically called when this rule is processed. | |
//Get the Buisness Service SubComponent | |
var ServiceSubComp = current.u_service_sub_component; | |
//Get the Business Service | |
var PrimaryService = current.u_business_service; | |
//Get the Date and Time the Ticket was created | |
var OpenedAt = current.opened_at; | |
//Get the Core Hours. | |
var PrimaryCoreSchedule = PrimaryService.u_core_business_hours.name; | |
var SubCoreSchedule = ServiceSubComp.u_core_business_hours.name; | |
if(SubCoreSchedule == ""){ | |
gs.info("NO Sub Core Schedule"); | |
var bSubSched = false; | |
}else{ | |
//Get the Schedule | |
var SubSched = new GlideSchedule(ServiceSubComp.u_core_business_hours); | |
var bSubSched = true; | |
} | |
if(PrimaryCoreSchedule == ""){ | |
gs.info("NO Primary Core Schedule"); | |
var bPrimarySched = false; | |
}else{ | |
//Get the Schedule | |
var PrimarySched = new GlideSchedule(PrimaryService.u_core_business_hours); | |
var bPrimarySched = true; | |
} | |
if(bSubSched == true){ | |
//Get the Schedule for the PRimary Service | |
var sched = new GlideSchedule(ServiceSubComp.u_core_business_hours); | |
//Create a GlideDateTime record with the Created date and time. | |
var d = new GlideDateTime(OpenedAt); | |
//Check if created date and time is within the core schedule | |
if (sched.isInSchedule(d)){ | |
gs.info("Is within core hours"); | |
//Set the Core Hours flag on the ticket. | |
current.setValue("u_core_hours", true); | |
current.update(); | |
}else{ | |
//Ticket not created within the Core Hours. | |
gs.info("Is NOT within core hours"); | |
} | |
}else if(bPrimarySched == true && bSubSched == false){ | |
//Get the Schedule for the PRimary Service | |
var sched = new GlideSchedule(PrimaryService.u_core_business_hours); | |
//Create a GlideDateTime record with the Created date and time. | |
var d = new GlideDateTime(OpenedAt); | |
//Check if created date and time is within the core schedule | |
if (sched.isInSchedule(d)){ | |
gs.info("Is within core hours"); | |
//Set the Core Hours flag on the ticket. | |
current.setValue("u_core_hours", true); | |
current.update(); | |
}else{ | |
//Ticket not created within the Core Hours. | |
gs.info("Is NOT within core hours"); | |
} | |
}else{ | |
gs.info("NO Core Schedules for Business Service or Service Sub Component"); | |
current.setValue("u_core_hours", false); | |
current.update(); | |
} | |
} |
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
var usergroup = new GlideAggregate('sys_user_grmember'); | |
usergroup.groupBy('user'); | |
usergroup.query(); | |
var user_list = new GlideRecord('sys_user'); | |
user_list.addNullQuery('last_login_time'); //Only users who never logged in | |
user_list.addQuery('sys_id', 'NOT IN', usergroup); //Users who are not in a group | |
user_list.addActiveQuery(); | |
user_list.query(); | |
var incident_query="" | |
var change_query="" | |
var in_incident_count=0; | |
var in_change_count=0; | |
//Loop through the list of users (from the previous query: not member of a group and never logged in) | |
while(user_list.next()){ | |
var found_in_incident = false; | |
var found_in_change = false; | |
//Query if user is found in any of the incident user fields | |
var incident_list = new GlideRecord('incident'); | |
incident_query="u_reported_by=" + user_list.sys_id + "^ORresolved_by=" + user_list.sys_id + "^ORcaller_id=" + user_list.sys_id + "^ORu_affected_user=" + user_list.sys_id + "^ORassigned_to=" + user_list.sys_id + "^ORopened_by=" + user_list.sys_id; | |
incident_list.addEncodedQuery(incident_query); | |
incident_list.query(); | |
if(incident_list.getRowCount() > 0){ | |
found_in_incident = true; | |
in_incident_count++; | |
} | |
//Query if user is found in any of the change_request user fields | |
var change_list = new GlideRecord('change_request'); | |
change_query="requested_by=" + user_list.sys_id + "^ORu_change_initiator=" + user_list.sys_id + "^ORu_single_owner=" + user_list.sys_id + "^ORu_sponsor=" + user_list.sys_id + "^ORassigned_to=" + user_list.sys_id + "^ORopened_by=" + user_list.sys_id; | |
change_list.addEncodedQuery(change_query); | |
change_list.query(); | |
if(change_list.getRowCount() > 0){ | |
found_in_change = true; | |
in_change_count++ | |
} | |
//If user has vener been used in incident and in change | |
if(!found_in_incident && !found_in_change) | |
gs.print(user_list.sys_id + " " + user_list.name); | |
//ADD ALL YOUR DEACTIVATION/ADMIN ACTIONS HERE | |
} | |
gs.print("\n"+ in_incident_count + " users found in incidents fields \n" + in_change_count + " users found in change fields"); |
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
gs.print("Starting reported by cleanup script"); | |
var count = new GlideRecord('incident'); | |
count.addQuery('sys_created_on', "<", "2016-04-01 08:00:00"); | |
count.addNullQuery('u_bmc_event_id'); | |
count.query(); | |
while (count.next()) { | |
gs.print("Ticket Number = " + count.number + " ,Reported By = " + count.u_reported_by + " ,caller_id = " + count.caller_id + " ,assigned to = " + count.assigned_to); | |
count.assigned_to = count.caller_id; | |
count.caller_id = count.u_reported_by; | |
count.update(); | |
} |
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
var GeneratePCRdoc = Class.create(); | |
GeneratePCRdoc.prototype = { | |
initialize: function(grGlideRecord, bDebug) { | |
this.m_sDoc = ""; | |
this.m_sDebug = ""; //Debug string | |
//if Debug is set on object initialisation | |
(bDebug && typeof bDebug != 'undefined') ? this.m_bDebug = true : this.m_bDebug = false; | |
if (grGlideRecord && typeof grGlideRecord != 'undefined') | |
this.m_grGlideRecord = grGlideRecord; | |
else { | |
if (this.m_bDebug) | |
this.m_sDebug += "No GlideRecord object supplied"; | |
return false; | |
} | |
if (this.m_bDebug) { | |
this.m_sDebug += "GenerateWord object initialize"; | |
} | |
}, | |
setDebug: function(bDebug) { | |
(bDebug && typeof bDebug != 'undefined') ? this.m_bDebug = true : this.m_bDebug = false; | |
}, | |
_addParagraph: function(sText) { | |
return '<w:p><w:r><w:t>' + sText + '</w:t></w:r></w:p>'; | |
}, | |
_getHeaderData: function() { | |
/*{0}= Number, | |
{1}= u_pcr_tile, | |
{2}= u_date_pcr_requested | |
{3}= u_date_pcr_delivered | |
{4}= Description, | |
*/ | |
var aHeaderData = [this.m_grGlideRecord.number, | |
this.m_grGlideRecord.u_pcr_tile, | |
this.m_grGlideRecord.u_date_pcr_requested.getDisplayValue(), | |
this.m_grGlideRecord.u_date_pcr_delivered.getDisplayValue(), | |
this.xmlEncode(this.m_grGlideRecord.description) | |
]; | |
return aHeaderData; | |
}, | |
_getSummaryData: function() { | |
/* Summary of assessment data | |
0 = investigation | |
1 = business impact | |
2 = root cause | |
3 = recommendations | |
4 = Summeriser (person who clicked the button) | |
5 = date / time | |
*/ | |
var aInvestigation = this.m_grGlideRecord.u_investigation.getDisplayValue(); | |
var aBusiness_Impact = "Business Impact: " + this.m_grGlideRecord.u_business_impact.getDisplayValue(); | |
var aRoot_Cause = "Root Cause: " + this.m_grGlideRecord.u_pcr_root_cause.getDisplayValue(); | |
var aRecommendations = "Recommendations: " + this.m_grGlideRecord.u_pcr_recommendations.getDisplayValue(); | |
var aSummaryData = [aInvestigation, aBusiness_Impact, aRoot_Cause, aRecommendations, | |
gs.getUser().getFullName(), | |
gs.nowDateTime() | |
]; | |
return aSummaryData; | |
}, | |
generateAssessment: function(sFileName) { | |
var aDetail = []; //empty array to parse into the header/footer | |
var sHeaderTable = gs.getMessage('PCR_Header_Table', this._getHeaderData()); // PCR header details | |
var sSummaryHeader = gs.getMessage('PCR_Assessment_Summary', this._getSummaryData()); // summary block | |
//Add the title (in the document header) | |
aDetail.push("DVLA PCR Assessment"); //{0} | |
//Add the header table | |
aDetail.push(sHeaderTable); //{1} | |
//Add the summary of assessments | |
aDetail.push(sSummaryHeader); //{2} | |
//Push current year into document | |
aDetail.push(new GlideDateTime().getYear()); //{4} | |
this.m_sDoc = gs.getMessage('DVLA_WordML_Header_Footer', aDetail); // The XML document header and foo//ter | |
this._writeFile(sFileName); | |
}, | |
getFolder: function() { | |
var grFolder = new GlideRecord("problem"); | |
// map the phase numbers with the text used in the folders as there isn't a 1-1 match | |
var phase = this.m_grGlideRecord.u_pcr_attachments; | |
grFolder.addQuery("number"); | |
//grFolder.addQuery("u_change.number", ""+this.m_grGlideRecord.number); | |
grFolder.query(); | |
while (grFolder.next()) { | |
// if we have a matching sub folder then return it | |
return grFolder.sys_id; | |
} | |
// otherwise just return the change record so we don't lose the attachment | |
return this.m_grGlideRecord.sys_id; | |
}, | |
getProblem: function() { | |
var grproblem = new GlideRecord("problem"); | |
// map the phase numbers with the text used in the folders as there isn't a 1-1 match | |
grproblem.addQuery("number"); | |
while (grproblem.next()) { | |
// if we have a matching sub folder then return it | |
return grproblem.sys_id; | |
} | |
// otherwise just return the change record so we don't lose the attachment | |
return this.m_grGlideRecord.sys_id; | |
}, | |
xmlEncode: function(sInput) { | |
sInput = trim(sInput.toString()); | |
var replace_with = '&'; | |
sInput = sInput.replace(/&/g, replace_with); | |
replace_with = '<'; | |
sInput = sInput.replace(/</g, replace_with); | |
replace_with = '>'; | |
inpsInputut = sInput.replace(/>/g, replace_with); | |
replace_with = '''; | |
sInput = sInput.replace(/'/g, replace_with); | |
replace_with = '"'; | |
sInput = sInput.replace(/"/g, replace_with); | |
return sInput; | |
}, | |
_writeFile: function(sFileName) { | |
//TODO put file into correct folder for phase | |
var attachment = new Attachment(); | |
var sMimeType = "text/xml"; | |
var oFolder = this.getProblem(); | |
var attachmentRec = attachment.write('problem', oFolder, sFileName, sMimeType, this.m_sDoc); | |
gs.log("file should be written" + sFileName + " sysid=" + this.m_grGlideRecord.sys_id + " attachmentrec value = " + attachmentRec); | |
}, | |
showDebug: function() { | |
gs.print(this.m_sDebug); //,"GeneratePCRdoc Script include"); | |
}, | |
type: 'GeneratePCRdoc' | |
}; |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment