- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
04-04-2023 06:59 PM
Kia ora Community,
Hoping someone can help me understand what I'm doing wrong.
Goal is to remove encryption context from attachments when business rule criteria are met. Something in the way I've written the code is resulting in the first attachment being skipped. If I only have one attachment the script ignores it. If I have two attachments it decrypts one and ignores the first, and so on.
Hopefully I've used the code sample insert functionality correctly.
Business rule is After > Update:
(function executeRule(current, previous /*null when async*/ ) {
// find attachment
var gr = new GlideRecord('sys_attachment');
gr.addQuery('table_sys_id', current.sys_id);
//gr.addQuery('table_name', current.getTableName());
//gr.addNotNullQuery('encryption_context');
gr.query();
// sys_id not found
if (gr.next()) {
gs.info("Business rule execution Decrypt position management attachments: attachments targeted for decryption - " + current.number + " / " + current.sys_id);
} else {
gs.info("Business rule execution Decrypt position management attachments " + current.sys_id + " not found in sys_attachment table or already no encryption context assigned.");
return false;
}
// decrypt attachment (remove encryption context)
while (gr.next()) {
var sysAttachment = new GlideSysAttachment();
try {
sysAttachment.changeEncryptionContext(gr.table_name, gr.table_sys_id, gr.sys_id, '');
gs.info('Business rule execution Decrypt position management attachments: Attachment decrypted: ' + gr.file_name);
} catch (e) {
gs.info('Business rule execution Decrypt position management attachments: ' + e.toString());
}
}
})(current, previous);
Thanks in advance,
Brett
Solved! Go to Solution.

- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
04-04-2023 07:41 PM
Hi,
Please try below:
(function executeRule(current, previous /*null when async*/ ) {
// find attachment
var gr = new GlideRecord('sys_attachment');
gr.addQuery('table_sys_id', current.sys_id);
//gr.addQuery('table_name', current.getTableName());
//gr.addNotNullQuery('encryption_context');
gr.query();
// sys_id not found
if (gr.hasNext()) {
gs.info("Business rule execution Decrypt position management attachments: attachments targeted for decryption - " + current.number + " / " + current.sys_id);
} else {
gs.info("Business rule execution Decrypt position management attachments " + current.sys_id + " not found in sys_attachment table or already no encryption context assigned.");
return false;
}
// decrypt attachment (remove encryption context)
while (gr.next()) {
var sysAttachment = new GlideSysAttachment();
try {
sysAttachment.changeEncryptionContext(gr.table_name, gr.table_sys_id, gr.sys_id, '');
gs.info('Business rule execution Decrypt position management attachments: Attachment decrypted: ' + gr.file_name);
} catch (e) {
gs.info('Business rule execution Decrypt position management attachments: ' + e.toString());
}
}
})(current, previous);
Thanks
Anil Lande

- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
04-04-2023 07:41 PM
Hi,
Please try below:
(function executeRule(current, previous /*null when async*/ ) {
// find attachment
var gr = new GlideRecord('sys_attachment');
gr.addQuery('table_sys_id', current.sys_id);
//gr.addQuery('table_name', current.getTableName());
//gr.addNotNullQuery('encryption_context');
gr.query();
// sys_id not found
if (gr.hasNext()) {
gs.info("Business rule execution Decrypt position management attachments: attachments targeted for decryption - " + current.number + " / " + current.sys_id);
} else {
gs.info("Business rule execution Decrypt position management attachments " + current.sys_id + " not found in sys_attachment table or already no encryption context assigned.");
return false;
}
// decrypt attachment (remove encryption context)
while (gr.next()) {
var sysAttachment = new GlideSysAttachment();
try {
sysAttachment.changeEncryptionContext(gr.table_name, gr.table_sys_id, gr.sys_id, '');
gs.info('Business rule execution Decrypt position management attachments: Attachment decrypted: ' + gr.file_name);
} catch (e) {
gs.info('Business rule execution Decrypt position management attachments: ' + e.toString());
}
}
})(current, previous);
Thanks
Anil Lande

- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
04-04-2023 07:45 PM
Hi,
the issue was because of below line:
// sys_id not found
if (gr.next()) {
You have used gr.next() in if , this was fetching 1st record of your query result.
Again you are doing while(gr.next()), and it started from 2nd record.
Use gr.hasNext() if you just want to check if you have got records in result. If you want to read/process gr record then you can use gr.next()
Thanks
Anil Lande
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
04-04-2023 07:50 PM
Kia ora Anil,
Thanks so much for the speedy reply, and for taking the time to explain the reasoning. You are what makes this community so awesome!

- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
04-04-2023 07:53 PM
Glad to know your issue is resolved 🙂
Happy Learning 👍
Thanks
Anil Lande