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
06-19-2023 06:18 AM
Hello Shweta, I am trying to do the same thing as for my project i need to upload a local file to servicenow. Can you please help me if you were able to achieve this? Also can you share exact details step by step how you were able to upload a local file to servicenow exactly.