Skip to content

Instantly share code, notes, and snippets.

@johncaruso
johncaruso / getFieldSum.js
Last active January 9, 2023 17:51
Get the aggregate SUM of a field using ServiceNow GlideAggregate API.
/**
* Get the aggregate SUM of a field using ServiceNow GlideAggregate API.
*
* @param {string} table Name of table to query
* @param {string} field Name of numeric field to SUM
* @param {string} [query] Optional encoded query string
* @returns {number} Aggregate SUM of field
*/
function getFieldSum(table, field, query) {
var ga = new GlideAggregate(table);
@johncaruso
johncaruso / _cloudSettings
Last active February 16, 2020 17:25
Visual Studio Code Settings Sync Gist
{"lastUpload":"2019-09-27T17:19:50.986Z","extensionVersion":"v3.4.3"}
@johncaruso
johncaruso / arrayToObject.js
Created August 9, 2019 11:21
Convert array of entity objects to object w/entity key, entity object pairs
function arrayToObject(array, keyField) {
return array.reduce(function(obj, item) {
obj[item[keyField]] = item;
return obj;
}, {});
}
@johncaruso
johncaruso / getApisForScript.js
Created November 27, 2018 04:27
Get ServiceNow APIs for Script Editor
/**
* Get ServiceNow APIs for Script Editor
*
* Returns context-specific API for use within Script Editor
*
* @param {GlideRecord} gr - GlideRecord containing script being edited
* @param {*} field - script field name
* @returns {Object} - parsed JSON API
*/
function getApisForScript(gr, field) {
/**
* ServiceNow JavaScript Bugs
*
* Executes a suite of tests to verify proper JavaScript implementation.
* Test failures will output "FAIL: expected <actual> to be <expected>" after test description.
* Or produce compiler exception, e.g.: 'Allows keywords as property names' test.
* Outputs using console.log() if console object is found (i.e., browser or node.js),
* otherwise uses gs.info().
*/
/* globals gs */
@johncaruso
johncaruso / fixTrackInUpdateSets.js
Created October 15, 2018 19:40
Fixes issues when using "Track in Update Sets" UI action on custom scoped app table.
/**
* Fixes issues when using "Track in Update Sets" UI action on custom scoped app table.
*
* Per https://community.servicenow.com/community?id=community_question&sys_id=760fb629db58dbc01dcaf3231f9619bd
* a temporary table with a prefix of rep$x_ may be added to your app scope.
*
* As of Kingston, table columns get created in app scope but the table does not.
*
* @param {string} tableName
*/
@johncaruso
johncaruso / fixCalcField.js
Created October 14, 2018 20:30
Fixes calculated field values where calculated field values were added/changed after records were inserted
/**
* Fixes calculated field values where calculated field values were added/changed after records were inserted
*
* @param {string} tableName
*/
function fixCalcField(tableName) {
var gr = new GlideRecord(tableName);
gr.query();
while (gr.next()) {
@johncaruso
johncaruso / global-gitignore.md
Created October 13, 2018 16:56 — forked from subfuzion/global-gitignore.md
Global gitignore

There are certain files created by particular editors, IDEs, operating systems, etc., that do not belong in a repository. But adding system-specific files to the repo's .gitignore is considered a poor practice. This file should only exclude files and directories that are a part of the package that should not be versioned (such as the node_modules directory) as well as files that are generated (and regenerated) as artifacts of a build process.

All other files should be in your own global gitignore file. Create a file called .gitignore in your home directory and add anything you want to ignore. You then need to tell git where your global gitignore file is.

Mac

git config --global core.excludesfile ~/.gitignore

Windows

git config --global core.excludesfile %USERPROFILE%\.gitignore
@johncaruso
johncaruso / embedded_help_fix.js
Created June 26, 2018 15:31
Remove embedded help content from scoped app
(function () {
function getEmbeddedHelpContent(sysId) {
var gr = new GlideRecord('sys_embedded_help_content');
if (gr.get(sysId)) return gr;
throw 'sys_id not found';
}
function moveEmbeddedHelpContentToGlobal(gr) {
gr.setValue('sys_scope', 'global');
@johncaruso
johncaruso / case-convert.js
Created June 18, 2018 23:21 — forked from rebolyte/case-convert.js
Change a string from camelCase to kebab-case or snake_case and vice versa
'use strict';
let DASH_LOWERCASE_REGEX = /-([a-z])/g;
let UNDERSCORE_LOWERCASE_REGEX = /_([a-z])/g;
let UPPERCASE_REGEX = /([A-Z])/g;
// func argument to .replace receives:
// 1) the matched substring
// 2) nth parenthesized substr match (i.e. if the pattern has any `()` group matches,
// those will be passed as the next args)