Skip to content

Instantly share code, notes, and snippets.

View thisnameissoclever's full-sized avatar

Tim Woodruff thisnameissoclever

View GitHub Profile
@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 / 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 / EXAMPLE - Querying Incident table from client-side script.js
Last active October 18, 2024 18:30
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 / animated_msg_example_3.js
Last active October 10, 2022 16:44
ServiceNow Expandable form message with some text hidden
showSimpleLoadingMessage('Retrieving data');
var arrListElements = slowFunctionThatReturnsAnArrayOfStrings() || ['Element 1', 'Element 2', 'Element 3'];
var flyoutListHTML = getHTMLListFromArray(
arrListElements,
'disc',
'Element: '
);
g_form.clearMessages();
@thisnameissoclever
thisnameissoclever / animated_msg_example_2.js
Created October 10, 2022 16:39
ServiceNow Animated "Loading" field message during GlideAjax call
var gaSlowGlideAjaxCall = new GlideAjax('{some_glideajax_script_include');
//todo: Set the second argument to the method you want to execute from your GlideAjax Script Include
gaSlowGlideAjaxCall.addParam('sysparm_name', '{some_slow_glideajax_script_method');
//todo: Add whatever other GlideAjax parameters you need to add below, such as:
gaSlowGlideAjaxCall.addParam('record_sys_id', g_form.getUniqueValue());
gaSlowGlideAjaxCall.addParam('some_field_value', g_form.getValue('{field_name}'));
//Show animated form message: "Processing request..."
//This will continue to show and animate until we call stopAnimatedLoadingFieldMessage(),
// which we'll do in the callback function
@thisnameissoclever
thisnameissoclever / animated_msg_example_1.js
Created October 10, 2022 16:31
ServiceNow Animated "Loading" form message during GlideAjax call
var gaSlowGlideAjaxCall = new GlideAjax('{some_glideajax_script_include');
//todo: Set the second argument to the method you want to execute from your GlideAjax Script Include
gaSlowGlideAjaxCall.addParam('sysparm_name', '{some_slow_glideajax_script_method');
//todo: Add whatever other GlideAjax parameters you need to add below, such as:
gaSlowGlideAjaxCall.addParam('record_sys_id', g_form.getUniqueValue());
//Show animated form message: "Processing request..."
//This will continue to show and animate until we call g_form.clearMessages(),
// which we'll do in the callback function
//todo: Update message from "Processing request" to whatever is appropriate to your situation.
@thisnameissoclever
thisnameissoclever / sn_animated_progress_collapsible_message.js
Last active May 29, 2024 12:22
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).
@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 / Expanding infomessage.js
Created May 31, 2022 15:46
Expanding infomessage with functional components in ServiceNow
var message = '';
message += 'This is an expanding info message. It can even run code! Click "Show more" to see!';
message += '<div>';
message += '<p><a href="#" onclick="javascript: jQuery(this.parentNode).next().toggle(200);">Show more</a></p>';
message += '<div style="display: none;">';
message += '<ul style="list-style: none">';
message += '<li>This is the expanded info in the message.</li>';
message += '<li>You can include any details you like here, including info retreived from a script like the sys_id of the current record: ' + g_form.getUniqueValue() + '</li>';
message += '</ul>';