- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
03-24-2020 09:31 AM
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
Solved! Go to Solution.
- Labels:
-
MID Server
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
04-22-2020 08:25 AM
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
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
03-24-2020 09:46 AM
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
Ankur
✨ Certified Technical Architect || ✨ 9x ServiceNow MVP || ✨ ServiceNow Community Leader
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
03-24-2020 09:55 AM
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
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
03-25-2020 01:30 AM
Hi Rahul,
So how are you triggering the transform map once attachment is added to data source?
Regards
Ankur
Ankur
✨ Certified Technical Architect || ✨ 9x ServiceNow MVP || ✨ ServiceNow Community Leader
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
03-25-2020 05:11 AM
Using GlideImportSetLoader( ) APIs.