- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
03-20-2024 10:18 PM
I would like to convert the file attached to the RITM to Base 64.
I want a code that can be tested in Background Script.
Solved! Go to Solution.
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
04-23-2024 10:43 PM
Did you test the script you shared? I'll share you the right script😁
var attachment = new GlideRecord('sys_attachment');
attachment.addQuery('table_sys_id', ritm_sys_id);
attachment.query();
while(attachment.next()){
// Base64 Encoding
var gsa = GlideSysAttachment();
var base64 = GlideBase64.encode(gsa.getByte(attachment));
var encoding = GlideStringUtil.base64Encode(base64);
}

- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
03-20-2024 10:22 PM - edited 03-20-2024 10:25 PM
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
03-20-2024 10:37 PM
Please try below code in background script:
// Define the RITM sys_id here
var ritmSysId = '1a7ec42b4730021096979530116d43e5';
// Fetch the RITM record
var ritm = new GlideRecord('sc_req_item');
if (ritm.get(ritmSysId)) {
// Get the attached file
var attachment = new GlideRecord('sys_attachment');
attachment.addQuery('table_sys_id', ritmSysId);
attachment.query();
if (attachment.next()) {
// Read the file content
var fileContent = new GlideSysAttachment().getContent(attachment);
// Convert to Base64
var base64File = GlideStringUtil.base64Encode(fileContent);
gs.info("Base64 encoded file content:\n" + base64File);
} else {
gs.info("No attachment found for RITM: " + ritmSysId);
}
} else {
gs.info("RITM not found with sys_id: " + ritmSysId);
}
Please Mark ✅Correct if this solves your query and also mark 👍Helpful if you find my response worthy based on the impact.
Thanks
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
03-20-2024 11:03 PM
Hi, @Maddysunil
Decoding the encoded value results in undefined.
Please tell me how to decode it.
[background script]
var attachment = new GlideRecord('sys_attachment');
attachment.addQuery('table_sys_id', '5c8ebfb997094210499879b71153afdf');
attachment.query();
if(attachment.next()){
// Read the file content
var fileContent = new GlideSysAttachment().getContent(attachment);
// Convert to Base64
var base64FileEncode = GlideStringUtil.base64Encode(fileContent);
gs.info("Base64 encoded file content:\n" + base64FileEncode);
var base64FileDecode = GlideStringUtil.base64Decode(base64FileEncode);
gs.info("Base64 encoded file content:\n" + base64FileDecode);
}
[log]
*** Script: Base64 encoded file content: dW5kZWZpbmVk
*** Script: Base64 encoded file content: undefined
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
03-20-2024 11:19 PM
// Define the RITM sys_id here
var ritmSysId = 'YOUR_RITM_SYS_ID';
// Fetch the RITM record
var ritm = new GlideRecord('sc_req_item');
if (ritm.get(ritmSysId)) {
// Get the attached file
var attachment = new GlideRecord('sys_attachment');
attachment.addQuery('table_sys_id', ritmSysId);
attachment.query();
if (attachment.next()) {
// Read the file content
var fileContent = new GlideSysAttachment().getContent(attachment);
// Decode the Base64 encoded content
var decodedContent = GlideStringUtil.base64Decode(fileContent);
gs.info("Decoded file content:\n" + decodedContent);
} else {
gs.info("No attachment found for RITM: " + ritmSysId);
}
} else {
gs.info("RITM not found with sys_id: " + ritmSysId);
}
Also you can go through below link:
Please Mark ✅Correct if this solves your query and also mark 👍Helpful if you find my response worthy based on the impact.
Thanks