Skip to content

Instantly share code, notes, and snippets.

View thisnameissoclever's full-sized avatar

Tim Woodruff thisnameissoclever

View GitHub Profile
@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 / 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 / LICENSE.txt
Created April 7, 2021 15:19
This license applies to all public gists on https://gist.github.com/thisnameissoclever
MIT License
Copyright (c) 2017 Timothy Woodruff
https://snprotips.com/
This license applies to all public gists on https://gist.github.com/thisnameissoclever
Permission is hereby granted, free of charge, to any person obtaining a copy
of this software and associated documentation files (the "Software"), to deal
in the Software without restriction, including without limitation the rights
to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
data.orgChart = data.orgChart || {};
data.orgChart.tiers = data.orgChart.tiers || [];
data.orgChart.i18n = {
avatar: gs.getMessage("{0} avatar"),
directReport: gs.getMessage("{0} direct report"),
directReports: gs.getMessage("{0} direct reports"),
minimumCharacters: gs.getMessage("Please enter {0} or more characters")
};
var loopDetected = false;
var MSTeamsMessage = Class.create();
MSTeamsMessage.prototype = {
/**
*
* @param {string} [teamsEndpoint=gs.getProperty()] - The MS Teams endpoint for the channel you're trying to post a message to.
* You can also specify this later by calling the .setEndpoint() method.
*/
initialize : function (teamsEndpoint) {
//local init
var exampleObj = {
"name": "Tim",
"age": 31,
"groovy": true,
"dogs": [
{
"name": "Ezri",
"age": 1
},
{
//Note: Read from innermost, outward
getAllUrlParams(
decodeURIComponent(
getAllUrlParams( //Get all URL params. Since SN re-encodes everything it passes into page processors like "nav_to.do" for example, this will have one key-val pair.
(this.location.href ? this.location.href : window.location.href) //The document URL. Should work for SP, and desktop view.
)['uri']
)
);
var yourParamValue = getAllUrlParams(this.location.href)['YOUR_PARAM_NAME'];
@thisnameissoclever
thisnameissoclever / copySpecificAttachment.js
Last active April 23, 2024 23:55
ServiceNow script to copy a specific attachment from one record to another
copySpecificAttachment(donorTable, donorID, recipientTable, recipientID, fileName);
function copySpecificAttachment(donorTable, donorID, recipientTable, recipientID, fileName) {
var donorAttSysID,
grNewAttachment,
linkToNewRecord,
attDataRecord,
newDocRecord,
grAttachment = new GlideRecord('sys_attachment');
grAttachment.addQuery('table_name', donorTable);
@thisnameissoclever
thisnameissoclever / plainTextSRAPI-handleDataStream.js
Created April 21, 2019 20:38
Handle data stream for plain-text or non-standard request body content-types in scripted rest API in ServiceNow.
(function process(/*RESTAPIRequest*/ request, /*RESTAPIResponse*/ response) {
var body = request.body;
var streamBody = body.dataStream;
var stringBody = getBodyText(streamBody);
gs.log('Message body: ' + stringBody);
function getBodyText(streamBody) {