SaveResponseBodyAsAttachment() method failing with Unable to save response body as attachment: User not authorized .

rahulyamgar
Tera Guru

Hello Friends,

 

I am using the REST API call and saving the response body using the method - "saveResponseBodyAsAttachment(tablename, recordSysId, filename); ". I am setting the MID server to execute the request. Above method execution is failing with "Unable to save response body as attachment: User not authorized " error.

 

Please guide.


Thanks,
Rahul

1 ACCEPTED SOLUTION

This is what we are doing:

1. Getting the ResponseBody.

var response = sm.execute();
var responseBody = response.getBody();

2. Creating an attachment using the below code, where gr is the GlideRecord to which file is attached. 

attachment.write(gr,"SampleJson","json",responseBody); 

 

It worked for me.

 

Thanks,
Rahul

View solution in original post

13 REPLIES 13

Ankur Bawiskar
Tera Patron
Tera Patron

Hi Rahul,

So you are executing REST Call via Mid Server and getting response as some file and want to attach it to some record. Is the user in whose session the script is running having proper user credentials? Can you share the script?

Regards
Ankur

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

Thanks Ankur. I am using MID server to execute the REST call and saving the response body as attachment. Below is the code:

 

try{
var request = new sn_ws.RESTMessageV2();
request.setHttpMethod('get');
var token = "ABC";

//endpoint - ServiceNow REST Attachment API
request.setEndpoint('https://xxxx.com');
request.setRequestHeader("Accept","Application/json");
request.setMIDServer("ABC00");
// var token = this.getToken();
request.setRequestHeader("Authorization",token);

//RESTMessageV2 - saveResponseBodyAsAttachment(String tableName, String recordSysId, String fileName)
request.saveResponseBodyAsAttachment("sys_data_source", "3a9f71f4dbb34050c543178b6896ADF", "Staging.json");

response = request.execute();
gs.info("Response error"+response.getErrorMessage());
httpResponseStatus = response.getStatusCode();

gs.print(" http response status_code: " + httpResponseStatus);
}
catch(ex){
var message = ex.getMessage();
gs.print(message);
}

 

Thanks,
Rahul

Hi Rahul,

So how are you triggering the transform map once attachment is added to data source?

Regards
Ankur

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

Using GlideImportSetLoader( ) APIs.