…thoughts on ServiceNow and digital transformation

Post

Retrieving Files from the MID Server with a ServiceNow Subflow


Powershell Script

#gets the Base64 encoding of a file
#usage: getFileBase64.ps1 \\SERVERNAME\ACHPO\PURCH\ P175897.Pdf
#first parameter is the path, second is the filename
$path=$args[0];
$filename=$args[1];
[convert]::ToBase64String((Get-Content -path "$path$filename" -Encoding byte));

Custom Action – ECC Queue Request

(function execute(inputs, outputs) {
		var arg1 = inputs.path; //the directory of the file
		var arg2 = inputs.file_name; //the filename

		var ecc = new GlideRecord("ecc_queue");
		ecc.initialize();//to create record
		ecc.agent = 'mid.server.' + inputs.mid_server_name;
		ecc.topic = "Command";
		var value = "powershell scripts\\WordMerge\\getFileBase64.ps1 "+arg1+""+arg2;
		ecc.payload = '<?xml version="1.0" encoding="UTF-8"?><parameters><parameter name="name" value="'+value+'"/><parameter name="skip_sensor" value="true"/></parameters>';
		ecc.queue = "output";
		ecc.state = "ready";
		outputs.ecc_sysid = ecc.insert();
})(inputs, outputs);

Script Include in Scoped Application for GlideSysAttachment.writeBase64

We need to use the writeBase64 method of the GlideSysAttachment class, however this only works in a scoped application. Every other artifact has been created in a global app, so we need to either create a scoped app for this script include or put it in an existing scoped application.

var AttachmentScopedUtils = Class.create();
AttachmentScopedUtils .prototype = {
    initialize: function() {
    },

	
	saveAttachmentBase64(glideRecord,fileName,contentType,base64Content){
		var attachment = new GlideSysAttachment();
		return attachment.writeBase64(glideRecord,fileName, contentType, base64Content);
	},
    type: 'AttachmentScopedUtils'
};

Custom Action – Parse ECC Queue Reply

(function execute(inputs, outputs) {

	var xmlDoc = new XMLDocument2();
	xmlDoc.parseXML(inputs.payload);
	var resNode = xmlDoc.getNode("//result");
	
	var name = resNode.getAttribute("command");

	//get the value of the result_code attribute
	var node = xmlDoc.getNode("//results");
	var nodeStr = node.toString()
	var start = nodeStr.indexOf('result_code=') + 13
	outputs.code = nodeStr.substring(start,start+1);

	//get the output 
	outputs.outmessage = xmlDoc.getNodeText("//output");

	//get the gliderecord that the file will be attached to
	var recGR = new GlideRecord(inputs.table_name);
	recGR.get(inputs.record_sysid);

	if(recGR){

		//get the Base64 encoding from the payload
		var encStr = xmlDoc.getNodeText("//stdout");
		//var encStr = gs.getXMLText(payload, "//stdout"); //doesn't work in private scope

		//attach the file to the record
//************* You need to replace the x_xxx_att below with the scope and name of the script include you created above

		outputs.attachment_sysid = new x_xxx_att.AttachmentScopedUtils().saveAttachmentBase64(inputs.file_name,encStr,recGR)


		}
	
})(inputs, outputs);

Subflow