Script to automatically update State upon receiving a comment.
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
07-10-2025 01:21 PM
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!
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
07-10-2025 03:32 PM
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.
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
07-10-2025 05:42 PM
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);