Skip to content

Instantly share code, notes, and snippets.

View thisnameissoclever's full-sized avatar

Tim Woodruff thisnameissoclever

View GitHub Profile
@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 / calculateAttachmentMD5Hash.js
Last active July 21, 2023 18:29
Calculate the MD5 hash of an attachment in ServiceNow (Scope & global)
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 January 23, 2022 00:12
Get the string value of an attachment (which must be a text-formatted file, like .txt, .csv. or .xml) in ServiceNow
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 January 23, 2022 00:13
Get a ServiceNow attachment file's contents as a string (Scope)
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 January 23, 2022 00:13
Get a ServiceNow attachment file's contents as a string (Global)
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.
/**
* 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 January 23, 2022 00:13
DynamicRefQuals (Client-callable Script Include)
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.
/***CLASSES***/
class APIClass {
/**
* An APIClass object
* @param name {string} The name of the API class
*/
constructor(name, classDesc = '') {
this._className = name;
this._classDescription = classDesc;
/**
@thisnameissoclever
thisnameissoclever / JournalRedactor.js
Last active April 6, 2024 08:28
Redact or delete a given journal entry in ServiceNow. Usage documentation: http://redactor.snc.guru/
/*
Script Include definition:
Name: JournalRedactor
Client Callable: false
Accessible from: All application scopes
Additional details:
Version: 1.3
Usage documentation: http://redactor.snc.guru/
License: https://gist.github.com/thisnameissoclever/767b8a738b929a0bd943965431061c1e
*/
@thisnameissoclever
thisnameissoclever / Documentation & Article
Last active March 20, 2018 01:24
ServiceNow "Try in Portal" button
https://snprotips.com/blog/2018/3/15/service-catalog-try-in-portal-button