Skip to content

Instantly share code, notes, and snippets.

View thisnameissoclever's full-sized avatar

Tim Woodruff thisnameissoclever

View GitHub Profile
@thisnameissoclever
thisnameissoclever / EXAMPLE - Querying Incident table from client-side script.js
Last active April 24, 2024 15:24
ServiceNow EfficientGlideRecord: A MUCH more efficient and performant client-side GlideRecord queries, as efficient as GlideAjax (or more!)
//Client-side example usage
doThing();
function doThing() {
new EfficientGlideRecord('incident')
.setLimit(10)
.addNotNullQuery('assignment_group')
.addField('number')
.addField('short_description')
.addField('assignment_group', true) //Get display value as well
@thisnameissoclever
thisnameissoclever / copySpecificAttachment.js
Last active April 23, 2024 23:55
ServiceNow script to copy a specific attachment from one record to another
copySpecificAttachment(donorTable, donorID, recipientTable, recipientID, fileName);
function copySpecificAttachment(donorTable, donorID, recipientTable, recipientID, fileName) {
var donorAttSysID,
grNewAttachment,
linkToNewRecord,
attDataRecord,
newDocRecord,
grAttachment = new GlideRecord('sys_attachment');
grAttachment.addQuery('table_name', donorTable);
@thisnameissoclever
thisnameissoclever / JournalRedactor.js
Last active April 6, 2024 08:28
Redact or delete a given journal entry in ServiceNow. Usage documentation: http://redactor.snc.guru/
/*
Script Include definition:
Name: JournalRedactor
Client Callable: false
Accessible from: All application scopes
Additional details:
Version: 1.3
Usage documentation: http://redactor.snc.guru/
License: https://gist.github.com/thisnameissoclever/767b8a738b929a0bd943965431061c1e
*/
@thisnameissoclever
thisnameissoclever / getJournalEntries.js
Last active April 6, 2024 08:28
Get ServiceNow Journal Entries, optionally parse and convert HTML control-characters and line-breaks, and return an array of the last N journal entries.
/**
* Get the journal entries from a given record, and optionally parse and convert line breaks and
* HTML and wokkas (< and >) to HTML (<br />\n and HTML-ized character codes).
* @param {GlideRecord} current - A GlideRecord object positioned to the record you want to get the
* journal entries from.
* @param {String} journalFieldName - The journal field name (e.g. "work_notes", "comments",
* "comments_and_work_notes", etc.).
* @param {Boolean} [convertLineBreaksToHTML=false] - Set this to true, to convert line-breaks
* (\r\n) to HTML (<br />\n).
* @param {Boolean} [convertWokkasToHTML=false] - Set this to true, to convert wokkas ("<" and ">")
@thisnameissoclever
thisnameissoclever / LocalProperty.js
Last active March 28, 2024 09:46
ServiceNow Local System Properties
/**
* Local (instance-specific) System Property Script Include
* More info available at https://localprop.snc.guru
*
* A ServiceNow JavaScript ES5 class, LocalProperty, for getting and setting instance-specific
* system properties.
* See method documentation for more information.
* @param {string} [instanceNameOverride=] - Override the name of the instance that's currently
* executing this code.
* For example, if you have a system property such as "my_prod_instance.some_property" which
@thisnameissoclever
thisnameissoclever / getMethodsAndProperties.js
Last active November 22, 2023 09:48
ServiceNow: Get Methods and Properties of an object
var methodsAndProperties = [];
var testObj = new GlideFilter('a=b', 'rule'); //TODO: replace this with whatever object you want to test
getMethodsAndProperties(methodsAndProperties, testObj);
gs.print('\n' + methodsAndProperties.join('\n'));
/**
* Populates an extant array in-place, of methods and properties of a given object.
* @param methodsAndProperties {array} - the array to populate/modify in-place.
@thisnameissoclever
thisnameissoclever / calculateAttachmentMD5Hash.js
Last active July 21, 2023 18:29
Calculate the MD5 hash of an attachment in ServiceNow (Scope & global)
var recordTable = 'incident';
var idOfRecord = '755d52b137b0b30090b68cf6c3990e6f';
var grAttachment = getAttachmentRecord(recordTable, idOfRecord, 'example text doc.txt', true);
if (grAttachment !== false) {
var attachmentStream = new GlideSysAttachment().getContentStream(
grAttachment.getValue('sys_id')
);
var gDigest = new GlideDigest();
var base64MD5 = gDigest.getMD5Base64FromInputStream(attachmentStream); //Scope only
gs.info(base64MD5);
@thisnameissoclever
thisnameissoclever / CS-Comment ACL Script When Advanced False.js
Last active April 28, 2023 17:08
Prevent ACL Script Execution when Advanced is False
function onChange(control, oldValue, newValue, isLoading, isTemplate) {
if (isLoading) {
return;
}
var scriptHasUncommentedLines, flyoutText;
var isAdvanced = (newValue === 'true');
var origScriptVal = (g_form.getValue('script').trim());
var doesScriptExist = (!!origScriptVal);
@thisnameissoclever
thisnameissoclever / sn_animated_progress_collapsible_message.js
Last active November 15, 2022 18:57
ServiceNow Animated Progress Message with Collapsible Details
/**
* Show an animated loading message such as "Loading...", where the dots will be
* animated with the interval specified in msInterval; first showing "Loading.", then
* "Loading..", then "Loading...", up to the number of dots indicated in maxDots.
* Once maxDots is reached, the message will be reset to "Loading." and the animation
* will repeat until stopAnimatedLoadingMessage() is called.
*
* @param {String} fieldName - The name of the field on which to show the loading message.
* @param {String} messageText - The loading message to be shown, which will be followed
* by animated dots (or whatever character is specified in dotChar, if specified).