Parse json response to create Attachment.

Kishor O
Tera Sage

I am getting the below response body from http get method.

{"id":"718564","ticket_id":"CYDERES-1083083","file_name":"Test attachment for SNOW test.txt","created_at":"2023-11-28T13:19:03.887-0600","mime_type":"text/plain","bytes_data":"b'This is a teste attachment for the SI integration with Service Now.'"}

 

Now I want to parse these data and create attachments in ServiceNow. How can I do this?

1 ACCEPTED SOLUTION

Daniel Borkowi1
Mega Sage

Hi @Kishor O ,

 

you can easily use JSON.parse(<put_your_body_here>).

//request_body from your Rest call

var msg = JSON.parse(request_body);
var attachment = new GlideSysAttachment();

//set up inputs
var rec = new GlideRecord('incident');
rec.get("<your_id_field>", msg.ticket_id);
var fileName = msg.file_name;
var contentType = msg.mime_type;
var content = msg.bytes_data;

var agr = attachment.write(rec, fileName, contentType, content);

gs.info('The attachment sys_id is: ' + agr);

 

Greets
Daniel

Please mark reply as Helpful/Correct, if applicable. Thanks!

View solution in original post

3 REPLIES 3

Daniel Borkowi1
Mega Sage

Hi @Kishor O ,

 

you can easily use JSON.parse(<put_your_body_here>).

//request_body from your Rest call

var msg = JSON.parse(request_body);
var attachment = new GlideSysAttachment();

//set up inputs
var rec = new GlideRecord('incident');
rec.get("<your_id_field>", msg.ticket_id);
var fileName = msg.file_name;
var contentType = msg.mime_type;
var content = msg.bytes_data;

var agr = attachment.write(rec, fileName, contentType, content);

gs.info('The attachment sys_id is: ' + agr);

 

Greets
Daniel

Please mark reply as Helpful/Correct, if applicable. Thanks!

@Daniel Borkowi1 Thank you.It worked. 

@Kishor O you're welcome.