while (gr.next()) skips first record but works for second, third, fourth, etc.

Brett Watson
Tera Contributor

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

 

1 ACCEPTED SOLUTION

Anil Lande
Kilo Patron

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);
Please appreciate the efforts of community contributors by marking appropriate response as correct answer and helpful, this may help other community users to follow correct solution in future.
Thanks
Anil Lande

View solution in original post

4 REPLIES 4

Anil Lande
Kilo Patron

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);
Please appreciate the efforts of community contributors by marking appropriate response as correct answer and helpful, this may help other community users to follow correct solution in future.
Thanks
Anil Lande

Anil Lande
Kilo Patron

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()

 

Please appreciate the efforts of community contributors by marking appropriate response as correct answer and helpful, this may help other community users to follow correct solution in future.
Thanks
Anil Lande

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!

Glad to know your issue is resolved 🙂

Happy Learning 👍

Please appreciate the efforts of community contributors by marking appropriate response as correct answer and helpful, this may help other community users to follow correct solution in future.
Thanks
Anil Lande