I have a requirement where i have to Move the state of the demand from Submitted to Qualified state
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
a month ago
I have a requirement where i have to Move the state of the demand table record from Submitted to Qualified state. when all the task that was created in the Submitted state is closed complete or Closed skipped.
I've written a After update business rule as below on demand_task table
when state is one of 'Closed Completed / Closed Skipped '
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
a month ago
Hi,
Update the script
(function executeRule(current, previous /*null when async*/ ) {
var demandId = current.parent.sys_id.toString();
var grDem = new GlideRecord('dmn_demand');
if (grDem.get(demandId)) {
var grTask = new GlideRecord('dmn_demand_task');
grTask.addQuery('parent', demandId);
grTask.addEncodedQuery('stateNOT IN3,7');
grTask.query();
if (!grTask.next()) {
grDem.state = -4;
grDem.update();
}
}
})(current, previous);
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
a month ago
try this
Note: ensure you use the correct state values, choice labels, choice values etc
(function executeRule(current, previous /*null when async*/) {
// Only proceed if the current state is one of the closed states
var closedStates = ['Closed Complete', 'Closed Skipped']; // Replace with correct choice values if numbers
if (closedStates.indexOf(current.state.getDisplayValue()) === -1) {
return;
}
var demandGR = new GlideRecord('demand');
// Query the demand record referenced by this task
// Adjust 'demand' below if your reference field is named differently
if (!current.demand) {
return; // No demand linked, exit
}
if (!demandGR.get(current.demand.sys_id)) {
return; // Demand record not found, exit
}
// Query all tasks linked to this demand that are NOT closed complete or closed skipped
var taskGR = new GlideRecord('demand_task');
taskGR.addQuery('demand', demandGR.sys_id);
taskGR.addQuery('state', 'NOT IN', 'Closed Complete,Closed Skipped');
taskGR.query();
// If no such open tasks found, update demand state to Qualified (-4)
if (!taskGR.hasNext()) {
demandGR.state = -4; // Assuming -4 corresponds to Qualified
demandGR.update();
}
})(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
a month ago
@Ankur Bawiskar the state is moving from 'screening' to 'Qualified' without checking for demand tasks
even if the demand tasks are in open state
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
a month ago
are you using the correct field on demand_task which holds the demand?
Ankur
✨ Certified Technical Architect || ✨ 9x ServiceNow MVP || ✨ ServiceNow Community Leader