Skip to content

Instantly share code, notes, and snippets.

/**
* Used to validate arguments passed into methods.
*/
var argumentValidation = Class.create();
argumentValidation.prototype = {
initialize: function(logLevel) {
this.logger = new global.GSLog('cf.argumentValidation.log.level');
if (this.notNil(logLevel)) {
logger.setLevel(logLevel);
@ChecksumFailed
ChecksumFailed / simplePipeTest.js
Last active June 14, 2023 17:17
Pipe and Compose in ServiceNow
function f1(a) {
gs.info("Hello " + a);
return gs.now();
}
function f2(a) {
gs.info("The date is: " + a);
return 70;
}
function f3(a) {
gs.info("The temperature is " + a + " degrees");
@ChecksumFailed
ChecksumFailed / DataTransformUtils.js
Last active June 15, 2023 16:27
DataTransformUtilsV2
/*
* NOTE; This is a work in progress and has not been tested in its current form.
* Set of methods to simplify loading data into ServiceNow. Use with Inbound Email, rest, etc...
* A temporary DS/importset will be created for the data load. It will be removed if the cleanup method is called.
* Example usage: DataTransformUtils().getDataSourceByName('Test DataSource').copyDataSource().copyAtt(sourceRecord).loadData().getMapByName('Test Transform Map').importData().cleanUpImport().log();
* this will:
* 1. Retrieve Existing DS by name
* 2. Create a clone of this datasource
* 3. Copy attachments from a source record to copied datasource
* 4. load data from the datasource into the import set table
var cf_ClientCommonsAjax = Class.create();
cf_ClientCommonsAjax.prototype = Object.extendsObject(AbstractAjaxProcessor, {
/**
* Dot walk object until last property reached
* @param {Object} obj obj
* @param {Array} props array of object properties
* @returns the last object value
*/
//copy variables from ritm to change request form
var ritmVar = new GlideRecord('sc_item_option_mtom');
ritmVar.addQuery('request_item', current.getUniqueValue());
ritmVar.addNotNullQuery('sc_item_option.value');
ritmVar.orderBy('sc_item_option.order');
ritmVar.query();
var chgVar = new GlideRecord('question_answer');
chgVar.newRecord();
chgVar.table_name = 'change_request';
(function() {
var comment = "Workflow cancelled by " + gs.getUserDisplayName();
new WorkflowApprovalUtils().cancelAll(current, comment);
new Workflow().restartWorkflow(current);
current.work_notes = comment;
//cancel current flow context and start new
if (!current.cat_item.flow_designer_flow.nil() && !current.flow_context.nil()) {
@ChecksumFailed
ChecksumFailed / DataTransformRecord.js
Last active May 1, 2024 20:21
ServiceNow Data Tranformation helper script
/**
* DataTransformRecord Class
* Manages the lifecycle of data transformation in ServiceNow by automating the creation, manipulation,
* and cleanup of data sources and associated transform maps.
*
* @class
*/
var DataTransformRecord = function() {
var datasource;
var map;
@ChecksumFailed
ChecksumFailed / dotWalkObject_es5.js
Last active November 27, 2024 19:42
Dot walk object until last property reached
/**
* Walks through an object using dot notation or an array of properties. This could be replaced by option chaining in ECMA2021
* @param {object} obj - The object to walk through.
* @param {string | string[]} props - The dot notation string or array of properties to traverse.
* @returns {*} - The value of the property at the end of the traversal, or undefined if not found.
* @throws {Error} - Throws an error if the arguments are invalid.
* @private
*/
function _dotWalkObject(obj, props) {
var propsIsArray = Array.isArray(props);
(function () {
function newGrObject(tbl, obj) {
var grRec = new GlideRecord(tbl);
new GlideRecordUtil().mergeToGR(obj, grRec);
grRec.insert();
}
var oldUiPolicy = 'f6e44e7bdbc005101ad90cd7f4961916';
var newUiPolicy = '05610f08db50c5101ad90cd7f4961937';
var tbl = 'catalog_ui_policy_action';
var getCartItem = function (sys_id) {
var gr = new GlideRecord("sc_cart_item");
gr.get(sys_id);
return gr;
};
var addAttToCartItems = function (items,cat_item) {
var attachment = new GlideSysAttachment();
//set up inputs