Script to automatically update State upon receiving a comment.

faithanthon
Tera Contributor

Hey All, 
New to scripting, and I'm sure i've made some obvious faux paus, but I can't get this script to work in the dev environment for the life of me and am hoping for some additional input. The goal is that it stores the user from a recent comment, checks to see if they have the 'itil_admin' role, and if so moves the incident state and state to on-hold and the on hold reason to awaiting customer (3, 3, and 1). Once it works, I'll make an inverse script, this way it will flip between hold and off hold states based upon who comments. Thanks!

(function executeRule(current, previous /*null when async*/) {

    var lastcomment = new GlideRecord('sys_journal_field');
    lastcomment.addQuery('element_id', current.sys_id);
    lastcomment.addQuery('element', 'comments');
    lastcomment.orderByDesc('sys_created_on');
    lastcomment.setLimit(1);
    lastcomment.query();

    if (lastcomment.next()) {
            var commenterID = lastcomment.getValue('sys_created_by');
            var userGR = new GlideRecord('sys_user');

        if (userGR.get(commenterID) && userGR.hasRole('itil_admin')) {
            current.state = 3;
            current.incident_state = 3;
            current.hold_reason = 1;
            current.update();
        }
    }  
})(current, previous);
2 REPLIES 2

Swapna Abburi
Mega Sage
Mega Sage

Hi @faithanthon 

I think you are not required to query "sys_journal_field" table. Instead, you can update condition in business rule to run when comments changes. In BR script, you can validate role condition by using gs.hasRole("itil_admin") object and update fields accordingly.

 

The script you have written was not working as below line of code returns user ID of the user, not sys_id.

var commenterID = lastcomment.getValue('sys_created_by');

 

Please alter the script as below and try.

var lastcomment = new GlideRecord('sys_journal_field');
lastcomment.addQuery('element_id', current.sys_id);
lastcomment.addQuery('element', 'comments');
lastcomment.orderByDesc('sys_created_on');
lastcomment.setLimit(1);
lastcomment.query();
if (lastcomment.next()) {
    var commenterID = lastcomment.getValue('sys_created_by');
   
    var userGR = new GlideRecord('sys_user_has_role');
    userGR.addQuery('user.user_name', commenterID);
    userGR.addQuery('role.name', "itil_admin");
    userGR.query();
    if (userGR.next()) {
        current.state = 3;
        current.incident_state = 3;
        current.hold_reason = 1;
        current.update();
    }
}

 

Rafael Batistot
Tera Sage

Hi @faithanthonx, 

 

You are passing “userGR.hasRole('itil_admin') this not use in server-side with glideRecord

 

And “current.update()” is not a good practice because may generate  

unexpected loops 

 

Consider this solution 

 

(function executeRule(current, previous /*null when async*/) {

var lastcomment = new GlideRecord('sys_journal_field');
lastcomment.addQuery('element_id', current.sys_id);
lastcomment.addQuery('element', 'comments');
lastcomment.orderByDesc('sys_created_on');
lastcomment.setLimit(1);
lastcomment.query();

if (lastcomment.next()) {
var commenterID = lastcomment.getValue('sys_created_by');

// Verifica se o usuário tem o papel 'itil_admin'
var userHasRole = new GlideRecord('sys_user_has_role');
userHasRole.addQuery('user', commenterID);
userHasRole.addQuery('role.name', 'itil_admin');
userHasRole.query();

if (userHasRole.hasNext()) {
current.state = 3; // On Hold
current.incident_state = 3; // On Hold
current.hold_reason = 1; // Awaiting Customer
}
}

})(current, previous);