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-17-2022 05:05 AM
Hi,
Check whether request.body.dataStream has proper value. You can add gs.log and make sure it has value.
GlideStringUtil.base64Encode works only in global scope. If not they you need to use gs.base64Decode. I think this is the difference between your PDI and customer instance
Palani
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
01-17-2022 06:11 AM
Hello
I checked for the logs and found the below value: "com.glide.communications.GlideScriptableInputStream@1bd7290".
Thank you,
Shweta

- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
01-17-2022 06:28 AM
Hi Shweta,
Since the input is datastream, you need to read it first before passing. Please find the updated code. I have added code to read data from the datastream
(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 stream = request.body.dataStream;
var fileData = "";
var reader = new GlideTextReader(stream);
var ln = "";
while ((ln = reader.readLine()) != null) {
fileData += ln;
}
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);
Palani
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
01-17-2022 06:46 AM
Thank you for your quick reply.
The issue still persists after adding the code to read the file
Thank you,
Shweta