Skip to content

Instantly share code, notes, and snippets.

Tim Woodruff thisnameissoclever

Block or report user

Report or block thisnameissoclever

Hide content and notifications from this user.

Learn more about blocking users

Contact Support about this user’s behavior.

Learn more about reporting abuse

Report abuse
View GitHub Profile
View Get URI Params - Method 1.js
//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
Created Apr 21, 2019
ServiceNow script to copy a specific attachment from one record to another
View copySpecificAttachment.js
copySpecificAttachment(donorTable, donorID, recipientTable, recipientID, fileName);
function copySpecificAttachment(donorTable, donorID, recipientTable, recipientID, fileName) {
var donorAttSysID;
var newAttRecord;
var linkToNewRecord;
var attDataRecord;
var newDocRecord;
var attRecord = new GlideRecord('sys_attachment');
attRecord.addQuery('table_name', donorTable);
@thisnameissoclever
thisnameissoclever / plainTextSRAPI-handleDataStream.js
Created Apr 21, 2019
Handle data stream for plain-text or non-standard request body content-types in scripted rest API in ServiceNow.
View plainTextSRAPI-handleDataStream.js
(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) {
@thisnameissoclever
thisnameissoclever / calculateAttachmentMD5-fallback.js
Created Apr 21, 2019
Fallback method for calculating MD5 hashes for ServiceNow attachments. Requires HI ticket to access to the calculateMD5CheckSum() method of the GlideChecksum class from ServiceNow.
View calculateAttachmentMD5-fallback.js
var gr = new GlideRecord('sys_attachment'); //the table where attachment metadata is stored
gr.get('0003ea666f015600623008efae3ee4f7'); //sys_id of a record in the sys_attachment table
var md5HashSum = calculateMD5Hash(gr);
function calculateMD5Hash(attachmentGR) {
var attInptStream = GlideSysAttachmentInputStream(attachmentGR.sys_id + '');
var chksum = (new GlideChecksum()).calculateMD5CheckSum(attInptStream);
gs.print(chksum);
}
@thisnameissoclever
thisnameissoclever / calculateAttachmentMD5Hash.js
Last active Apr 21, 2019
Calculate the MD5 hash of an attachment in ServiceNow (Scope & global)
View calculateAttachmentMD5Hash.js
var recordTable = 'incident';
var idOfRecord = '755d52b137b0b30090b68cf6c3990e6f';
var grAttachment = getAttachmentRecord(recordTable, idOfRecord, 'example text doc.txt', true);
if (grAttachment !== false) {
var attachmentStream = new GlideSysAttachment().getContentStream(
grAttachment.getValue('sys_id'));
var gDigest = new GlideDigest();
var base64MD5 = gDigest.getMD5Base64FromInputStream(attachmentStream); //Scope only
gs.info(base64MD5);
}
@thisnameissoclever
thisnameissoclever / global.getAttachmentStringByName.js
Last active Apr 22, 2019
Get the string value of an attachment (which must be a text-formatted file, like .txt, .csv. or .xml) in ServiceNow
View global.getAttachmentStringByName.js
var tableName = 'incident';
var sysIDOfRecord = '755d52b137b0b30090b68cf6c3990e6f';
var fileNameSansExtension = 'example text doc'; //Full file name: example text doc.txt
var grRecordWithAttachment = new GlideRecord(tableName);
grRecordWithAttachment.get(sysIDOfRecord);
var gsa = new GlideSysAttachment();
//ONLY works in global
var textVal = gsa.get(grRecordWithAttachment, fileNameSansExtension);
@thisnameissoclever
thisnameissoclever / scope.getAttachmentContentsAsString.js
Last active Apr 21, 2019
Get a ServiceNow attachment file's contents as a string (Scope)
View scope.getAttachmentContentsAsString.js
var recordTable = 'incident';
var idOfRecord = '755d52b137b0b30090b68cf6c3990e6f';
var grAttachment = getAttachmentRecord(recordTable, idOfRecord, 'example text doc.txt', true);
var gsaTextFile = new GlideSysAttachment();
var strContents = gsaTextFile.getContent(grAttachment); //ONLY worked in non-global scope
gs.info(strContents);
/**
@thisnameissoclever
thisnameissoclever / global.getAttachmentContentsAsString.js
Last active Apr 21, 2019
Get a ServiceNow attachment file's contents as a string (Global)
View global.getAttachmentContentsAsString.js
var tableName = 'incident';
var recordID = '755d52b137b0b30090b68cf6c3990e6f';
gs.print(getAttachmentContentsAsString(tableName, recordID));
function getAttachmentContentsAsString(tableName, recordID) {
//Declare a new instance of GlideSysAttachment.
var gsa = new GlideSysAttachment();
//Get the raw bytes in the file
var bytesInFile = gsa.getBytes(tableName, recordID);
//Convert that jive into a string using Java/Rhino.
View eventWrapper.js
/**
* Does the work on each loop.
* @param {Number} [limit=10] - The number of records to process per loop.
* @param {Number} [currentNumber=0] - The number of records that have been processed so far, by all previous loops.
*/
function eventWrapper(limit, currentNumber) {
var EVENT_NAME = 'EVENT.NAME.HERE'; //todo: Update this to the name of the event you've created.
var TABLE_NAME = 'TABLE_NAME_HERE'; //todo: Update this to the name of the table containing the records you're processing
var QUERY = 'some_query=here'; //todo: Put your query here
@thisnameissoclever
thisnameissoclever / DynamicRefQuals.js
Last active Mar 7, 2019
DynamicRefQuals (Client-callable Script Include)
View DynamicRefQuals.js
var DynamicRefQuals= Class.create();
DynamicRefQuals.prototype = Object.extendsObject(AbstractAjaxProcessor, {
//Client-callable
/*
To quickly and easily create a dynamic reference qualifier script, simply add a custom method to this Script Include.
Here are the guidelines for modifying this Script Include:
1. In the "@author" JSDoc tag of the method your implement, please put your user ID (For example: "twoodruff").
2. See how the getGroupMembersByID and getGroupMembersByName methods are documented, and try to document your method in the same fashion.
3. Unless you've got permission from the author, please do not modify anyone else's methods.
You can’t perform that action at this time.