Need to read a file from the local machine and upload the file on the servicenow record
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
01-17-2022 04:03 AM
Hello All,
I want to read the file from the local machine and upload the file into the servicenow record.
To achieve this, I have followed steps 1. Read the file and convert it to a base64 encoded string. 2. Decode the string and write it to the record. I have created a Scripted API with the following code:
(function process( /*RESTAPIRequest*/ request, /*RESTAPIResponse*/ response) {
var tableName = request.queryParams.tableName;
var number = request.queryParams.number;
var fileName = request.queryParams.fileName;
var fileMimeType = request.headers.mimetype;
var fileData = request.body.dataStream;
var rec = new GlideRecord(tableName);
rec.addQuery('number', number);
rec.query();
if (rec.next()) {var sa = new GlideSysAttachment();
var base64string = GlideStringUtil.base64Encode(fileData);
//var base64string = gs.base64Encode(fileData);
var decodedString = GlideStringUtil.base64Decode(base64string);
//var decodedString = gs.base64Decode(base64string);
sa.write(rec, fileName, fileMimeType, decodedString);
var responseBody = {};
responseBody.number = number;
responseBody.status = "Success";
response.setBody(responseBody);
} else {
var responseBodyFailure = {};
responseBodyFailure.status = "Success";
response.setBody(responseBodyFailure);
}
})(request, response);
However, I am facing two issues with this code:
1. By executing this code in my PDI the file is getting uploaded successfully however the files seems to be blank when opened.
2. I have moved the same code into my customer environment, however it gives me an error "Cannot find function base64Encode in object function GlideStringUtil()". Therefore I used gs.base64Encode(fileData) and gs.base64Decode(base64string); in the above code. This creates the same issue mentioned in 1st point i.e. the file is getting uploaded successfully however the files seems to be blank when opened.
Please suggest to me if I am missing anything.
Thank you,
Shweta
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
01-18-2022 03:09 AM
I tried calling the API through Rest API Explorer as well as through PostMan.
For Rest API Explorer:
URL:
POST https://dev1xxx42.service-now.com/api/503666/app/postattachmentapi/file
Query parameters:
tableName : incident
number: INC0010111
fileName: ShwetaTS.pdf
Request headers:
mimetype: application/pdf
Request Body
In the Raw section:
{"dataStream":"C:\Users\shwetak\Desktop\ShwetaTS.pdf"}

- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
01-18-2022 05:17 AM
I think you just passed text. It will not upload the file to the request.
For your requirement try this:
- In Body tab of PostMan select form-data
- Type Key as dataStream.
- If you mouse over the text dataStream, you will see a drop down on the right side. Open the drop down and then select File
- You will see option to Upload file in Value. Choose your file
- Submit the request
Check whether you get the dataStream now
Palani
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
01-18-2022 05:31 AM
Yes, I have tried that, and it works correctly. However, in my case uploading a file will not work. The path for the file will be passed as the input parameter. And I need to design something which will read the file and upload it on the record.
I have also tried by passing the base64 string as an input(the code changes slightly in this case). The file got uploaded and when downloaded I was able to open the file. the base64 string was achieved through the online conversion. Therefore I was trying to have the conversion into the script itself.

- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
01-18-2022 06:25 AM
Hi,
Technically it is not possible. ServiceNow or any web application can not read local file using API.
You can only upload the file to the API.
Thank you,
Palani
Palani
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
01-19-2022 03:27 AM
Hello
Thank you for all your help.
Regards,
Shweta