How to decode the base64 content of attached pdf file in record?
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
06-18-2021 09:24 AM
Hello Experts,
Need suggestion on how to decode the base64 content of the attached pdf file in the record.
Here is the situation, in the scoped application we're working on integration where we need to attach the pdf file to the incident by decoding the base64 content received in the response body.
As of now, by processing the response body we're able to attach the pdf file but when we check the pdf it contains base64 encoding.
Just to give you the background we tried everything which is related to OOB base64 decoding things and OOB attachment APIs.
Please note, following code is not working in the scoped app either.
var base64string = GlideStringUtil.base64Encode("^.^ JOHN ANDERSEN!");
gs.log(base64string);
//yields: Xi5eIEpPSE4gQU5ERVJTRU4h
var orig = GlideStringUtil.base64Decode(base64string);
//yields: ^.^ JOHN ANDERSEN!
gs.log(orig);
We tried every possible way using these
Please advise,

- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
06-18-2021 09:43 AM
Hello,
Have you confirmed that the content in the PDF is the same as the payload?
Are you sure you're actually receiving it in base64 in the first place?
When you place an example in a tool such as this, does it net what you'd expect? https://www.base64decode.org/
Also, sorry, some of your post is a bit confusing, but I think you're saying the example you gave above works...but doesn't work for your scenario within a scope? GlideStringUtil...
Please mark reply as Helpful/Correct, if applicable. Thanks!
Please consider marking my reply as Helpful and/or Accept Solution, if applicable. Thanks!
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
06-18-2021 11:16 AM
Hi Allen,
Before I answer your questions, let me give you a brief idea about the requirement.
If any non-English file is attached to the record then analyst just hit the UI action button Translate in English then BR get's trigger then BR calls the script include function.
Script include responsible to execute the REST message with the Request body containing the source file in base64 then in Response body we receives the translated text in pdf but in base64.
Our goal is to process the base64 content from Response body and get it attached to the same record. (I'm assuming I've explained well, if not please ask questions if you have any)
Here, are my inline answers to your questions.
- Have you confirmed that the content in the PDF is the same as the payload?
Ans: Just checked again, no it isn't the same. The response body that we received, there we could see encoded Base64 was huge. But in pdf, it shows this
- Are you sure you're actually receiving it in base64 in the first place?
Ans: Yes, I'm sure about this. Check the Response.OutputFile
{
"StatusInfo": {
"RequestId": "f498e563-53f7-4884-bff4-4019afab46ba",
"Status": "Success",
"MessageCode": null,
"Timestamp": "2021-06-10T08:47:00.4332429+00:00",
"ReturnValue": 0
},
"Response": {
"FileName": "b689d8d0-0bf2-4554-8476-9f58e5fc0efd_Translated.pdf",
"WordCount": 181,
"OutputFile":" Assume this is BASE64 encoded info "
}
}
- When you place an example in a tool such as this, does it net what you'd expect? https://www.base64decode.org/
Ans: Just checked, I've copied the value of Response.OutputFile from the above response body that we received. Then tried to decode using the https://www.base64decode.org/ but seems like we're getting garbage value. Strange for us!
Here is the code that we've written,
var tableName = 'x_aukms_kyc_kyc_idd_task';
var tableSysID = 'adca4ae3db7de01023a4ad174896199c';
var gr = new GlideRecord('sys_attachment');
gr.addQuery('table_name', tableName);
gr.addQuery('table_sys_id', tableSysID);
gr.query();
if (gr.next()) {
gs.info('line 37 Attachment file name is ' + gr.file_name);
var sa = new GlideSysAttachment();
//var attachment_convert = GlideStringUtil.base64Encode("gr");
var attachment_convert = sa.getContentBase64(gr);
gs.info('Converted file: '+ attachment_convert);
var r = new sn_ws.RESTMessageV2('x_aukms_kyc.IDVS Traslation TOKEN', 'TOKEN');
var response = r.execute();
var responseBody = response.getBody();
var httpStatus = response.getStatusCode();
gs.info(responseBody);
var responseBody = JSON.parse(responseBody);
var a = 'Bearer '+responseBody.access_token;
// try {
var sm1 = new sn_ws.RESTMessageV2('IDVS Translation Engine', 'Send File');
sm1.setRequestHeader("Authorization",a);
sm1.setStringParameterNoEscape('fileName', gr.file_name);
sm1.setStringParameterNoEscape('sourceFile', attachment_convert);
sm1.setStringParameterNoEscape('sourceLanguage', 'jp'); //hardcoded source language
sm1.setStringParameterNoEscape('destinationLanguage', 'en'); //hardcoded destination language
//sm1.setStringParameterNoEscape('IsOCRNeeded', false); //default
//sm1.setStringParameterNoEscape('OCRFormat', 'pdf'); //default
// sm1.setStringParameterNoEscape('ExcelFormat', null); //default
// sm1.setStringParameterNoEscape('TranslationEngine', 'geofluent'); //default
var response1 = sm1.executeAsync();
//response1.waitForResponse(60);
var responseBody1 = response1.getBody();
gs.info('line 36 Request body sent ' + sm1.getRequestBody());
gs.info('line 37 Response body received ' + responseBody1 );
var decodedResponseBody1 = JSON.parse(responseBody1);
var responseFileName = decodedResponseBody1.Response.FileName;
var encodedbase64File = decodedResponseBody1.Response.OutputFile;
gs.info('line 42 Status Code is ' + response1.getStatusCode());
gs.info('line 43 file name is ' + responseFileName);
gs.info('line 44 encoded is ' + encodedbase64File);
if (response1.getStatusCode() == '200') {
// var decodedbase64File = GlideStringUtil.base64DecodeAsBytes(encodedbase64File);
var decodedbase64File = gs.base64Decode(encodedbase64File);
var attachment = new GlideSysAttachment();
//encodedbase64File = attachment.getContentBase64(encodedbase64File);
//decodedbase64File = gs.base64Decode(encodedbase64File);
var rec = new GlideRecord(tableName);
var recordSysID = tableSysID;
rec.get(recordSysID);
var newfileName = responseFileName.replace(responseFileName, 'ENGLISH_' + gr.file_name);
gs.info('New file name is: '+newfileName);
var contentType = 'application/pdf';
var agr = attachment.writeBase64(rec, newfileName, contentType,encodedbase64File);
gs.info('The new attachment sys_id is: ' + agr);
//till this we're able to attach the file to the required record but doesn't seem the content of pdf match with the Response.OutputFile field in response body
//we're trying the below code just to see what happens
/*
var sourceAttachmentGlideRecord = new GlideRecord('sys_attachment');
if(sourceAttachmentGlideRecord.get(agr)){
var fileName = sourceAttachmentGlideRecord.getValue('file_name');
var contentType1 = sourceAttachmentGlideRecord.getValue('content_type');
var sourceAttachmentSysId = sourceAttachmentGlideRecord.getValue('sys_id');
}
var attachment1 = new GlideSysAttachment();
attachment1.writeContentStream(
recordSysID ,
fileName,
contentType1,
attachment1.getContentStream(sourceAttachmentSysId) );
gs.info("Attachment created");
*/
}
}

- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
06-18-2021 11:21 AM
Hi,
I hope my reply above was Helpful, if so, please mark it as such.
I'm a bit confused on where we are now because I've asked those questions, you've answered, and you're saying the payload itself isn't even base64 (or so it seems), right?
So...not sure what else we can do as it's a data issue on the other end and not SN's fault, I believe?
Please mark reply as Helpful/Correct, if applicable. Thanks!
Please consider marking my reply as Helpful and/or Accept Solution, if applicable. Thanks!
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
06-18-2021 11:28 AM