Skip to content

Instantly share code, notes, and snippets.

View thisnameissoclever's full-sized avatar

Tim Woodruff thisnameissoclever

View GitHub Profile
/**
* 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 / 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.
@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.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 / 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 / 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 / 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) {
@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);
//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'];
var exampleObj = {
"name": "Tim",
"age": 31,
"groovy": true,
"dogs": [
{
"name": "Ezri",
"age": 1
},
{