Skip to content

Instantly share code, notes, and snippets.

View thisnameissoclever's full-sized avatar

Tim Woodruff thisnameissoclever

View GitHub Profile
@thisnameissoclever
thisnameissoclever / BenchmarkRunner.js
Last active October 13, 2022 17:04
ServiceNow Benchmark Runner Script Include
var BenchmarkRunner = Class.create();
BenchmarkRunner.prototype = {
initialize : function () {
this.timers = {};
},
/**
* Execute two functions with the specified arguments, and get info about which was faster
* (and by how much).
*
@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 / calculateAttachmentMD5-fallback.js
Last active August 24, 2022 16:10
Fallback method for calculating MD5 hashes for ServiceNow attachments. Requires HI ticket to access to the calculateMD5CheckSum() method of the GlideChecksum class from ServiceNow.
var grAttachment = new GlideRecord('sys_attachment'); //the table where attachment metadata is stored
grAttachment.get('0003ea666f015600623008efae3ee4f7'); //sys_id of a record in the sys_attachment table
var md5HashSum = calculateMD5Hash(grAttachment);
function calculateMD5Hash(attachmentGR) {
var attachmentInputStream = GlideSysAttachmentInputStream(attachmentGR.sys_id + '');
var chksum = (new GlideChecksum()).calculateMD5CheckSum(attachmentInputStream);
gs.print(chksum);
}
@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>';
@thisnameissoclever
thisnameissoclever / Display form in dialog.js
Created May 31, 2022 15:44
Display form in dialog in ServiceNow and pre-populate some fields
function showMyForm(){
var recordID = g_form.getUniqueValue() || "-1";
var recordTableName = g_form.getTableName();
var dialog = new GlideDialogForm('Update Record', recordTableName);
//Set to "-1" to show new record form instead
dialog.setSysID(recordID);
//If false, will show related lists as well.
dialog.addParm('sysparm_form_only', 'true');
//todo: Form view?
@thisnameissoclever
thisnameissoclever / Populate default value for image field.js
Created May 31, 2022 15:41
Populate default value for image field ServiceNow
GlideSysAttachment.copy("ZZ_YYtable_name", "default_image_sys_id_here", "ZZ_YYtable_name", current.getValue('sys_id'));
@thisnameissoclever
thisnameissoclever / add object to object.js
Last active January 23, 2022 00:14
Add one object/array to another, avoiding "pass-by-reference" pitfalls in JavaScript.
/**
* Adds one object to another, nesting the child object into the parent.
* this is to get around javascript's immutable handling of objects.
* @param name {String} - The name of the property of the parent object, in which to nest the child object. <br />For example, if the name parameter is set to "pickles" then "parent.pickles" will return the child object.
* @param child {Object} - The object that should be nested within the parent object.
* @param [parent={}] {Object} - The parent object in which to nest the child object. If the parent object is not specified, then the child object is simple nested into an otherwise empty object, which is then returned.
* @returns {Object} - A new object consisting of the parent (or an otherwise empty object) with the child object nested within it.
* @example
* //sets myNewObject to a copy of originalObject, which now also contains the original (yet un-linked) version of itself as a child, under the property name "original"
* var myNewObject = addObjToObj("orig
@thisnameissoclever
thisnameissoclever / ServiceNow Client Script-Get URI Param.js
Created May 13, 2017 06:06
This ServiceNow Client Script returns an object consisting of a property for each URI parameter, and its' value.
/**
* Get the values of each URI param as properties of an object (name-value pairs).
* @param url
* @returns {{}}
* @example - In the URL www.google.com/page?urip=pickles&otheruriparam=bananas, you could use getAllUrlParams().urip to get "pickles".
*/
function getAllUrlParams(url) {
// get query string from url (optional) or window
var queryString = url ? url.split('?')[1] : window.location.search.slice(1);