GlideSysAttachment.getContentBase64 isn't working

Mussie
ServiceNow Employee
ServiceNow Employee

Hi Guys,

I am unable to make the below code work. I am trying to send an attachment via REST message by encoding it to Base64 first. The records are getting created in the third party system without the base64 bit. Any ideas?

(function executeRule(current, previous /*null when async*/) {

	try { 

		var attachObj = {};
		var grAttachIncident = new GlideRecord ('sys_attachment');
		grAttachIncident.addQuery ('table_sys_id', current.getUniqueValue());
		grAttachIncident.query();		
		if (grAttachIncident.next()){
			attachObj.u_file_name = grAttachIncident.file_name + '';
			attachObj.u_table_name = grAttachIncident.table_name + '';
			attachObj.u_content_type = grAttachIncident.content_type + '';
			attachObj.u_table_sys_id = current.getUniqueValue() + '';
			attachObj.u_task_number = current.number + '';			
			//encode attachment
			attachObj.u_base64 = new GlideSysAttachment().getContentBase64(grAttachIncident);
			var body = JSON.stringify(attachObj);
			gs.info(body);
			
			var rAttach = new sn_ws.RESTMessageV2('Network Incidents', 'Post_Inc_Attac');
			rAttach.setRequestBody(body);
			var responseAttach = rAttach.execute();
			var httpStatusAttach = responseAttach.getStatusCode();
			gs.print("http response status_code: " + httpStatusAttach);  
	}
	}
	catch(ex) {
	 var message = ex.getMessage();
	}
})(current, previous);

P.S. I am using an instance which is on London.

Thanks,

Mussie

1 ACCEPTED SOLUTION

Thanks Sachin. 

I later realized that 'getContentBase64' doesn't worked in Global, I changed my code as below and it worked:

 

var sa = new GlideSysAttachment();
var binData = sa.getBytes(grAttachIncident);
attachObj.u_base64 = GlideStringUtil.base64Encode(binData);

 

View solution in original post

2 REPLIES 2

Thanks Sachin. 

I later realized that 'getContentBase64' doesn't worked in Global, I changed my code as below and it worked:

 

var sa = new GlideSysAttachment();
var binData = sa.getBytes(grAttachIncident);
attachObj.u_base64 = GlideStringUtil.base64Encode(binData);