Last active
July 5, 2024 12:57
-
-
Save iamkalai/c56b8fae9e2cae8b5bbefe132903ec88 to your computer and use it in GitHub Desktop.
ServiceNow Snippets
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 () { | |
var getCatalog = new GlideRecord("sc_cat_item"); | |
getCatalog.addEncodedQuery("active=true^hide_sp=false^sys_class_name=sc_cat_item"); //Update this | |
getCatalog.query(); | |
while (getCatalog.next()) { | |
var addVariableSet = new GlideRecord("io_set_item"); | |
addVariableSet.initialize(); | |
addVariableSet.sc_cat_item = getCatalog.getValue("sys_id"); | |
addVariableSet.variable_set = "86f3c8b51b0d9c508ed631d7dc4bcbcd"; //variable set sys_id | |
addVariableSet.insert(); | |
} | |
})(); |
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 groups = getGroups("nameIN"); | |
var roleToAdd = "f25370019f22120047a2d126c42e7061"; | |
var addGroupRoles = new GlideRecord("sys_group_has_role"); | |
for (var i = 0; i < groups.length; i++) { | |
addGroupRoles.initialize(); | |
addGroupRoles.role = roleToAdd; | |
addGroupRoles.group = groups[i]; | |
addGroupRoles.insert(); | |
} | |
function getGroups(query) { | |
var answer = []; | |
var table = "sys_user_group"; | |
var field = "sys_id"; | |
if (global.JSUtil.notNil(table) && global.JSUtil.notNil(query) && global.JSUtil.notNil(field)) { | |
var gr = new GlideRecord(table); | |
gr.addEncodedQuery(query); | |
gr.query(); | |
while (gr.next()) { | |
answer.push(gr.getValue(field)); | |
} | |
} | |
var arrayUtil = new ArrayUtil(); | |
answer = arrayUtil.unique(answer); | |
return answer; | |
} |
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 gr = new GlideRecord("sys_group_has_role"); | |
gr.addEncodedQuery("role=f25370019f22120047a2d126c42e7061"); | |
gr.query(); | |
while (gr.next()) { | |
var um = new GlideUpdateManager2(); | |
um.saveRecord(gr); | |
} |
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 numberOfDays = 2; | |
var intNumberOfDays = parseInt(numberOfDays); | |
var schedRecord = new GlideRecord('cmn_schedule'); | |
schedRecord.get('name', 'Schedule Name'); | |
var schedule = new GlideSchedule(schedRecord.sys_id); | |
var actualDateTime = new GlideDateTime(); | |
actualDateTime.setDisplayValue(gs.nowDateTime()); | |
var getUsers = new GlideRecord('sys_user'); | |
getUsers.addEncodedQuery('offboarddate!=NULL'); //Change query as needed. | |
getUsers.query(); | |
while (getUsers.next()) { | |
var dateString = getUsers.offboarddate + " " + "00:00:01"; | |
var gdt = new GlideDateTime(dateString); | |
var differenceDays = parseInt((((schedule.duration(gdt, actualDateTime)).getNumericValue()) / 1000) / (3600 * 12)); | |
gs.log('differencedays = ' + differenceDays); | |
if (differenceDays > intNumberOfDays) { | |
gs.log('IN'); | |
} else { | |
gs.log('OUT'); | |
} | |
} |
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
hasDuplicate: function(table, filter, field) { | |
var answer = false; | |
var isDuplicatePresent = new GlideAggregate(table); | |
isDuplicatePresent.addEncodedQuery(filter); | |
isDuplicatePresent.addAggregate('COUNT'); | |
isDuplicatePresent.groupBy(field); | |
isDuplicatePresent.addHaving('COUNT', '>', 1); | |
isDuplicatePresent.query(); | |
if (isDuplicatePresent.hasNext()) { | |
answer = true; | |
} | |
return answer; | |
} |
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 gr = new GlideRecord("question_choice"); | |
gr.addEncodedQuery("question=72f56026dbf5295021fa026dd396197e"); | |
gr.query(); | |
gs.print(gr.getRowCount()); | |
while (gr.next()) { | |
var label = gr.text.toString().toLowerCase(); | |
label = label.replace(/[^A-Z0-9]+/ig, "_"); | |
gr.value = label; | |
gr.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
//Sample script include | |
var DoStuff = Class.create(); | |
DoStuff.prototype = Object.extendsObject(AbstractAjaxProcessor, { | |
initialize: function(request, responseXML, gc) { | |
AbstractAjaxProcessor.prototype.initialize.call(this, request, responseXML, gc); | |
this.stuff = 'something'; | |
}, | |
print: function() { | |
return this.stuff; | |
}, | |
}); | |
//Sample script to test script include works | |
var glideAjax = new GlideAjax('DoStuff'); | |
glideAjax.addParam('sysparm_name','print'); | |
glideAjax.getXMLWait(); | |
alert(glideAjax.getAnswer()); |
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 userGroupsToCopy = getGroups("user=0b04f7ebdb4cb3405cc670bdbf961912"); | |
var userIDToCopy = "2991eccd4790e914af792a62e36d4383"; | |
var addGroups = new GlideRecord("sys_user_grmember"); | |
for (var i = 0; i < userGroupsToCopy.length; i++) { | |
addGroups.initialize(); | |
addGroups.user = userIDToCopy; | |
addGroups.group = userGroupsToCopy[i]; | |
addGroups.insert(); | |
} | |
function getGroups(query) { | |
var answer = []; | |
var table = "sys_user_grmember"; | |
var field = "group"; | |
if (global.JSUtil.notNil(table) && global.JSUtil.notNil(query) && global.JSUtil.notNil(field)) { | |
var gr = new GlideRecord(table); | |
gr.addEncodedQuery(query); | |
gr.query(); | |
while (gr.next()) { | |
answer.push(gr.getValue(field)); | |
} | |
} | |
var arrayUtil = new ArrayUtil(); | |
answer = arrayUtil.unique(answer); | |
return answer; | |
} |
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 userRolesToCopy = getRoles("user=0b04f7ebdb4cb3405cc670bdbf961912^inherited=false"); | |
var userIDToCopy = "2991eccd4790e914af792a62e36d4383"; | |
var addRoles = new GlideRecord("sys_user_has_role"); | |
for (var i = 0; i < userRolesToCopy.length; i++) { | |
addRoles.initialize(); | |
addRoles.user = userIDToCopy; | |
addRoles.role = userRolesToCopy[i]; | |
addRoles.insert(); | |
} | |
function getRoles(query) { | |
var answer = []; | |
var table = "sys_user_has_role"; | |
var field = "role"; | |
if (global.JSUtil.notNil(table) && global.JSUtil.notNil(query) && global.JSUtil.notNil(field)) { | |
var gr = new GlideRecord(table); | |
gr.addEncodedQuery(query); | |
gr.query(); | |
while (gr.next()) { | |
answer.push(gr.getValue(field)); | |
} | |
} | |
var arrayUtil = new ArrayUtil(); | |
answer = arrayUtil.unique(answer); | |
return answer; | |
} |
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
//Script to execute | |
var getRecordToAttach = new GlideRecord("incident"); | |
getRecordToAttach.addEncodedQuery("active=true^number=INC0000002"); | |
getRecordToAttach.query(); | |
if (getRecordToAttach.next()) { | |
var csvData = "Number,Short_Description"; //CSV Header | |
csvData += "\n" + getRecordToAttach.getValue('number') + "," + getRecordToAttach.getValue('short_description'); | |
var gAttachment = new GlideSysAttachment(); | |
var attachmentID = gAttachment.write(getRecordToAttach, "scores" + ".csv", 'application/csv', csvData); | |
var getAttachment = GlideRecord('sys_attachment'); | |
getAttachment.addQuery('sys_id', attachmentID); | |
getAttachment.query(); | |
if (getAttachment.next()) { | |
var binData = gAttachment.getBytes(getAttachment); | |
var encData = GlideStringUtil.base64Encode(binData); | |
var jspr = new JavascriptProbe('SNMid'); | |
jspr.setName('GenerateRecord'); //can be anything | |
jspr.setJavascript("var ddr = new CreateFileFromBase64(); res= ddr.execute();"); | |
jspr.addParameter("verbose", "true"); //if log is required | |
jspr.addParameter("filepath", "path here"); | |
jspr.addParameter("filename", getAttachment.file_name); | |
jspr.addParameter("encodedData", encData); | |
jspr.create(); | |
gs.print('Completed'); | |
} | |
} | |
//Midserver script include CreateFileFromBase64 | |
var CreateFileFromBase64 = Class.create(); | |
CreateFileFromBase64.prototype = { | |
initialize: function() { | |
/* Set up the Packages references */ | |
this.File = Packages.java.io.File; | |
this.FileOutputStream = Packages.java.io.FileOutputStream; | |
this.StringUtil = Packages.com.glide.util.StringUtil; | |
/* Set up the parameters */ | |
this.verbose = probe.getParameter("verbose"); | |
this.filepath = probe.getParameter("filepath"); | |
this.filename = probe.getParameter("filename"); | |
this.encodedData = probe.getParameter('encodedData'); | |
this.debug("verbose -- filepath -- filename : " + this.verbose + ' -- ' + this.filepath + ' -- ' + this.filename); | |
}, | |
createFile: function() { | |
this.debug("Initiating file save function"); | |
var fout = new this.FileOutputStream(new this.File(this.filepath + this.filename)); // create new file | |
fout.write(this.StringUtil.base64DecodeAsBytes(this.encodedData)); // convert base64 to original format and write data to newly created file | |
fout.close(); | |
var result = "File successfully created : " + this.filepath + this.filename; | |
this.debug(result); | |
return result; | |
}, | |
/* Function to debug in mid-server log*/ | |
debug: function(m) { | |
if (this.verbose == "true") { | |
ms.log("CreateFileFromBase64: " + m); | |
} | |
}, | |
/* Execute the Probe*/ | |
execute: function() { | |
var file = this.createFile(); | |
return file; | |
}, | |
}; |
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 gr = new GlideRecord("discovery_credentials"); | |
gr.addQuery("name", "test"); | |
gr.query(); | |
if (gr.next()) { | |
var Encrypter = new GlideEncrypter(); | |
var decrypted = Encrypter.decrypt(gr.password); | |
gs.print(decrypted); | |
} |
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
//Sample Scripts | |
//Parent class | |
var ParentScript = Class.create(); | |
ParentScript.prototype = { | |
initialize: function () { | |
gs.info('In ParentScript Intialize'); | |
}, | |
print: function () { | |
gs.info('In ParentScript Print'); | |
}, | |
}; | |
//Child class | |
var ChildScript = Class.create(); | |
ChildScript.prototype = Object.extendsObject(ParentScript, { | |
initialize: function () { | |
ParentScript.prototype.initialize.call(this); | |
gs.info('In ChildScript Intialize'); | |
}, | |
print: function () { | |
ParentScript.prototype.print.call(this); | |
gs.info('In ChildScript Print'); | |
}, | |
}); | |
//Execute using | |
var child = new ChildScript(); | |
child.print(); |
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 getVariableID(name) { | |
var id = ''; | |
if (typeof g_scratchpad != "undefined") { | |
var fieldPortal = g_form.getField(name); | |
id = fieldPortal.sys_id.toString(); | |
} else { | |
var fieldNormalUI = g_form.getGlideUIElement(name); | |
id = fieldNormalUI.fieldName.toString(); | |
id = id.replace("IO:", ""); | |
} | |
return id; | |
} |
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
//Can be added to script include to return records less than X minutes as OOB you can compare hour, days etc. | |
function getRecordsWithLessThanXMinutes(table, query, field1, field2, mins) { | |
var recordIDs = []; | |
var requiredSeconds = mins * 60; | |
var getRecords = new GlideRecord(table); | |
getRecords.addEncodedQuery(query); | |
getRecords.query(); | |
while (getRecords.next()) { | |
var gdt1 = new GlideDateTime(getRecords[field1].getDisplayValue()); | |
var gdt2 = new GlideDateTime(getRecords[field2].getDisplayValue()); | |
var diffSeconds = gs.dateDiff(gdt1.getDisplayValue(), gdt2.getDisplayValue(), true); | |
if (diffSeconds <= requiredSeconds && diffSeconds >= 0) | |
recordIDs.push(getRecords.getValue('sys_id')); | |
} | |
return recordIDs; | |
} | |
//Function can be called with below code | |
var records = getRecordsWithLessThanXMinutes('incident', 'active=true', 'opened_at', 'sys_created_on', 10); | |
for (var index = 0; index < records.length; index++) { | |
gs.print(records[index]); | |
} |
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
//Can be added to script include to return records more than X minutes as OOB you can compare hour, days etc. | |
function getRecordsWithMoreThanXMinutes(table, query, field1, field2, mins) { | |
var recordIDs = []; | |
var requiredSeconds = mins * 60; | |
var getRecords = new GlideRecord(table); | |
getRecords.addEncodedQuery(query); | |
getRecords.query(); | |
while (getRecords.next()) { | |
var gdt1 = new GlideDateTime(getRecords[field1].getDisplayValue()); | |
var gdt2 = new GlideDateTime(getRecords[field2].getDisplayValue()); | |
var diffSeconds = gs.dateDiff(gdt2.getDisplayValue(), gdt1.getDisplayValue(), true); | |
if (diffSeconds >= requiredSeconds) | |
recordIDs.push(getRecords.getValue('sys_id')); | |
} | |
return recordIDs; | |
} | |
//Function can be called with below code | |
var records = getRecordsWithMoreThanXMinutes('incident', 'active=true', 'opened_at', 'sys_created_on', 10); | |
for (var index = 0; index < records.length; index++) { | |
gs.print(records[index]); | |
} |
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 approver = ""; | |
var comments = ""; | |
var getLatestRejection = new GlideRecord("sysapproval_approver"); | |
getLatestRejection.addEncodedQuery("sysapproval=" + current.getValue("sys_id") + "^state=rejected"); | |
getLatestRejection.orderByDesc('sys_updated_on'); | |
getLatestRejection.setLimit(1); | |
getLatestRejection.query(); | |
if (getLatestRejection.next()) { | |
approver = getLatestRejection.approver.getDisplayValue(); | |
var fullComments = (getLatestRejection.comments.getJournalEntry(1)).split("(Comments)\n"); | |
comments = fullComments[1]; | |
} | |
return approver + "~-~" + comments; |
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 hasCI(ip) { | |
var gr = new GlideRecord("cmdb_ci_hardware"); | |
gr.addEncodedQuery("ip_address=" + ip + "^discovery_source=ServiceNow"); | |
gr.query(); | |
if (gr.hasNext()) { | |
return true; | |
} | |
return false; | |
} | |
var missing = []; | |
var gr = new GlideRecord("dscy_credentials_affinity"); | |
gr.addEncodedQuery("credential_id=6240fc3fdbd9ed1021fa026dd3961907"); | |
gr.query(); | |
while (gr.next()) { | |
if (!hasCI(gr.getValue('ip_address'))) { | |
missing.push(gr.getValue('ip_address')); | |
} | |
} | |
for (var i = 0; i < missing.length; i++) { | |
gs.print(missing[i]); | |
} |
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 getRITM = new GlideRecord("sc_req_item"); | |
getRITM.addEncodedQuery("active=true^cat_item=667c71a30bf00300eba42da0d5673ac4"); | |
getRITM.query(); | |
while (getRITM.next()) { | |
if (getRITM.hasAttachments()) | |
gs.print(getRITM.getValue("number")); | |
} |
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 gdt = new GlideDateTime(); | |
gdt.addSeconds(60); | |
var gr = new GlideRecord("sc_req_item"); | |
gr.addEncodedQuery("number=RITM0010069"); | |
gr.query(); | |
if (gr.next()) { | |
gs.eventQueueScheduled('test.event', gr, '', '', gdt); | |
} |
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 mailRecipients = []; | |
var checkApprovers = new GlideRecord('sysapproval_approver'); | |
checkApprovers.addEncodedQuery('sysapproval=' + current.sys_id.toString()); //Change this query as needed. | |
checkApprovers.query(); | |
while (checkApprovers.next()) { | |
mailRecipients.push(checkApprovers.approver.toString()); | |
} | |
if (current.assigned_to != '') { | |
mailRecipients.push(current.assigned_to.toString()); | |
} | |
if (current.assigned_to == '' && current.assignment_group != '') { | |
mailRecipients.push(current.assignment_group.toString()); | |
} | |
if (current.request.requested_for != '') { | |
mailRecipients.push(current.request.requested_for.toString()); | |
} | |
var arrayUtil = new ArrayUtil(); | |
mailRecipients = arrayUtil.unique(mailRecipients); | |
gs.eventQueue('event_name', current, mailRecipients.toString()); |
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 stopwatch = new GlideStopWatch(); | |
var gr = new GlideRecord('sys_properties'); | |
gr.addEncodedQuery('name=event.limited.queues'); | |
gr.setLimit(1); | |
gr.query(); | |
if (gr.next()) { | |
gs.print(gr.getValue('value')); | |
} | |
gs.print('Timer:' + stopwatch); |
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 gr = new GlideRecord('incident'); | |
gr.addEncodedQuery('active=true^number=INC0000002'); | |
gr.query(); | |
if (gr.next()) { | |
gs.print(gr.short_description.toUpperCase()); | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment