sc_task Business rule to move non agent comments from pending to work in progress.
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
3 weeks ago
Hi,
I seem to be running into issues with trying to set up a business rule for sc_task. We want any non-agent replies to the sc_tasks to move the state from pending to work in progress. We only want this to work for 3 assignment groups. Another factor is to not work when moveworks updates. I have the below script but it seems to not work when I comment to a ticket it seems to move the state back to work in progress. Any tips on the script would be appreciated. thank you.
// Before Update on sc_task
(function executeRule(current, previous /*null when async*/) {
// --- Act only on comment-only updates while Pending (5) ---
if (!current.comments.changes()) return; // No Additional Comments change
if (current.state.changes()) return; // State was manually changed in this update
if (previous.state != 5 || current.state != 5) return; // Was and is Pending
// --- Only for these assignment groups (by sys_id) ---
var allowedGroups = [
'ba0fd3c7db78e11053403978f4961920', // Service Delivery Optimization
'd625dccec0a8016700a222a0f7900d06', // IT Service Desk
'c75eb931dbab5700f4ee7b5b8c961971' // Desktop Support
];
if (allowedGroups.indexOf(String(current.assignment_group)) === -1) return;
// --- Updater checks (use session user in Before rules) ---
var userName = gs.getUserName() || '';
// If updated by MoveWorks, do NOT flip
if (userName === 'MoveWorks') return;
// If updater is an agent, do NOT flip
var isAgent = gs.hasRole('itil') || gs.hasRole('hr') || gs.hasRole('facilities_inc');
if (isAgent) return;
// --- Otherwise flip Pending -> Work in Progress (2) ---
current.state = 2;
})(current, previous);
- Labels:
-
Request Management
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
3 weeks ago
Hi @RBlor ,
Try below code, Before Update on sc_task....
Here, I am ensuring precise behavior by comparing the latest journal entry using current.comments.getJournalEntry( ) to its previous value, so the rule only fires for actual comment additions and not general record updates. ....
(function executeRule(current, previous /* null when async */) {
// 1. Check for added Additional Comments ONLY
var prevComment = previous.comments.getJournalEntry(1);
var currComment = current.comments.getJournalEntry(1);
if (!currComment || currComment === prevComment) return;
// 2. Ensure status remains Pending (5)
if (!(previous.state == 5 && current.state == 5)) return;
// 3. Only specific assignment groups
var allowed = new Set([
'ba0fd3c7db78e11053403978f4961920',
'd625dccec0a8016700a222a0f7900d06',
'c75eb931dbab5700f4ee7b5b8c961971'
]);
if (!allowed.has(String(current.assignment_group))) return;
// 4. Exclude user if update made by MoveWorks
if (current.sys_updated_by == 'MoveWorks') return;
// 5. Exclude users with agent roles
var agentRoles = ['itil', 'hr', 'facilities_inc'];
for (var role of agentRoles) {
if (gs.hasRole(role)) return;
}
current.state = 2;
})(current, previous);
If you found my response helpful, please mark it as ‘Accept as Solution’ and ‘Helpful’. This helps other community members find the right answer more easily and supports the community.
Kaushal Kumar Jha - ServiceNow Consultant - Lets connect on Linkedin: https://www.linkedin.com/in/kaushalkrjha/
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
3 weeks ago
thank you i just tested so when i set to pending, it sets the state to wip even without comments. same as the other suggested script
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
3 weeks ago
what debugging did you do and what are your findings?
(function executeRule(current, previous /*null when async*/) {
// Only trigger on comment update while in Pending state
if (!current.comments.changes()) return;
if (current.state.changes()) return;
if (previous.state != 5 || current.state != 5) return;
// Only for specific assignment groups
var allowedGroups = [
'ba0fd3c7db78e11053403978f4961920',
'd625dccec0a8016700a222a0f7900d06',
'c75eb931dbab5700f4ee7b5b8c961971'
];
if (allowedGroups.indexOf(String(current.assignment_group)) === -1) return;
// Get actual updater username
var updaterUserName = current.sys_updated_by + '';
if (updaterUserName === 'MoveWorks') return;
// Get updater's sys_id
var updaterGR = new GlideRecord('sys_user');
updaterGR.addQuery('user_name', updaterUserName);
updaterGR.query();
if (!updaterGR.next()) return; // user not found
// Check agent roles via sys_user_has_role
var agentRoleIds = [
'8fcb6f45c0a8016400a222a0f7900d0d', // itil
'5e1789e1c079a100e1e2a3f1decfac45', // hr
'77f5e459c079a100e1e2a3f1decfac67' // facilities_inc
];
var hasAgentRole = false;
var roleGR = new GlideRecord('sys_user_has_role');
roleGR.addQuery('user', updaterGR.sys_id);
roleGR.addQuery('role', 'IN', agentRoleIds.join(','));
roleGR.query();
if (roleGR.hasNext()) hasAgentRole = true;
if (hasAgentRole) return;
// Set state to Work in Progress
current.state = 2;
})(current, previous);
If my response helped please mark it correct and close the thread so that it benefits future readers.
Ankur
✨ Certified Technical Architect || ✨ 9x ServiceNow MVP || ✨ ServiceNow Community Leader
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
3 weeks ago
i'll try this now i have tried the other 2 scripts here but they seem to change state to wip even without comments. i know its the script since setting to inactive and trying to trigger the pending to wip will not trigger anything.
I just tried the script, and it seems to be set to WIP from pending just when changing state.