Built something you're proud of? Tell the story. A quick G2 review of App Engine or Build Agent helps other developers see what's possible on ServiceNow. Share your experience.

How to use saveResponseBodyAsAttachment?

Suggy
Giga Sage

How to use saveResponseBodyAsAttachment?

 

I have written the script like this:

 

var tablename = 'incident';

var recordSysId = 'XXXXXXXXXXXXXXXXXXxx';

var filename = 'sampleupload.txt';

var request = new sn_ws.RESTMessageV2();

request.setHttpMethod('get');

request.setEndpoint('https://xxxxxx.service-now.com/api/now/attachment/xxxxxxx/file');

request.saveResponseBodyAsAttachment(tablename, recordSysId, filename);

request.setBasicAuth('xxxxx', 'xxxxxx');

 

 

var response = request.execute();

var httpResponseStatus = response.getStatusCode();

var httpResponseContentType = response.getHeader('Content-Type');

var newAttachmentSysId = response.getResponseAttachmentSysid();

var parsed;

var httpResponseBody;

 

gs.log("http response status_code: " + httpResponseStatus);

gs.log("http response content-type: " + httpResponseContentType);

gs.log(newAttachmentSysId);

 

 

Getting result as below:

*** Script: http response status_code: 404

*** Script: http response content-type: application/json;charset=UTF-8

*** Script: null

 

 

Note - Script reference: https://developer.servicenow.com/blog.do?p=/post/saving-restmessagev2-responses-as-attachments/Savin...

1 ACCEPTED SOLUTION

Suggy
Giga Sage

After giving the role it worked. 

View solution in original post

5 REPLIES 5

dgarad
Tera Sage

Hi @Suggy 

Refer below link it help you.

https://developer.servicenow.com/blog.do?p=/post/saving-restmessagev2-responses-as-attachments/

If my answer finds you well, helpful, and related to the question asked. Please mark it as correct and helpful.

Thanks
dgarad

@dgarad My script reference for from that blog post only, but not working

Not applicable

Hi @Suggy 

 

You can check my reply here-

https://www.servicenow.com/community/developer-forum/how-to-download-attachment-from-url/td-p/282846...

 

Can you elaborate your requirement?

 

Regards,

Amit

Suggy
Giga Sage

After giving the role it worked.