Skip to content

Instantly share code, notes, and snippets.

View thisnameissoclever's full-sized avatar

Tim Woodruff thisnameissoclever

View GitHub Profile
@thisnameissoclever
thisnameissoclever / $Documentation and how-to.md
Last active April 1, 2025 15:19
Easily calculate the distance between any two Location records in the cmn_location table in ServiceNow, WITHOUT using an API call. See the full article at https://distance.snc.guru/

How to implement and use DistanceCalculator

  1. Ensure that you are in the scope you intend to put this script in. If you're uncertain, I recommend selecting the Global application scope.
  2. Create a new Script Include in the sys_script_include table.
  3. Set the Name field to DistanceCalculator.
  4. Set Accessible from to "All application scopes".
  5. Set the description field to a meaningful description of the function of the Script Include (such as from the comments above 1the main public function in the script).
  6. Copy the script from the DistanceCalculator.js file in this Gist (below), and paste it into the Script field.
  7. Submit the record.
@thisnameissoclever
thisnameissoclever / haversine.js
Last active March 24, 2025 21:11
Calculating the distance between two location records in ServiceNow.
/**
*
* @param {String} originLocationID - The sys_id of the Location record for the origin point.
* This should be the sys_id of a valid record in the cmn_location table.
*
* @param destinationLocationID - The sys_id of the Location record for the destination point.
* This should be the sys_id of a valid record in the cmn_location table.
*
* @param {('km'|'miles'|'mile'|'mi'|'yards'|'yd'|'inches'|'in'|'feet'|'ft'|'meters'|'m'|'centimeters'|'cm'|'millimeters'|'mm'|'nautical miles'|'nm')} [distanceUnits='km']
* - The units of the distance. Default is 'km'.
@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 ">")