Join the #BuildWithBuildAgent Challenge! Get recognized, earn exclusive swag, and inspire the ServiceNow Community with what you can build using Build Agent.  Join the Challenge.

Send attachment data to external system using Rest API in JSON format.

Bandaru Sree Le
Kilo Contributor

Hi Team,

I need to send attachment data to external system in JSON format. How can I do it?

Should I also encode data to base64 in this case?

Please help me!

 

Thank You

11 REPLIES 11

Hi Ankur, 

The 3rd party accepts the json request body in the following way,


[ {"col1": "val1","col2": "val2","col3": "val3"}​​​​​​​,
  {​​​​​​​"col1": "val4","col2": "val5","col3": "val6"}​​​​​​​,
 {​​​​​​​ "col1": "val7","col2": "val8","col3": "val9"}​​​​​​​,
{​​​​​​​"col1": "val10","col2": "val11","col3": "val12" }​​​​​​​ ]
 
it accepts row wise.
 
Please help me with the sample code to convert the excel data into JSON format as it is first time I am working.

 

Hi,

refer this sample script on how I used it to generate the HTML structure

on similar lines you parse the excel and create array of json object

each json object will be a row

var html = '<table>';

var att = new GlideRecord('sys_attachment');
att.addQuery('sys_id', '29c39e75073f64102011ff208c1ed04f');
att.query();
if(att.next()){
	var imp = new sn_impex.GlideExcelParser();
	var attachment = new GlideSysAttachment();
	var attachmentStream = attachment.getContentStream(att.sys_id.toString());
	imp.parse(attachmentStream);
	var headers = imp.getColumnHeaders().toString().split(',');
	for(var j in headers){
		html = html + '<th>' + headers[j] + '</th>';
	}

	var rowString = '';

	var model = headers[0];
	var sn = headers[1];
	while(imp.next()) {
		var row = imp.getRow();
		rowString = rowString + '<tr>';
		for(var i in headers){
			rowString = rowString + '<td>' + row[headers[i]] + '</td>';
		}
		rowString = rowString + '</tr>';
	}
	html = html + rowString + '</table>';
	gs.info(html);
}

Regards
Ankur

Regards,
Ankur
Certified Technical Architect  ||  9x ServiceNow MVP  ||  ServiceNow Community Leader