I have a requirement where i have to Move the state of the demand from Submitted to Qualified state

Trupti Krishnam
Tera Contributor

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 '

 

   var gr = new GlideRecord('demand');
    gr.addQuery('parent', current.sys_id);
    gs.addInfoMessage("1");
    gr.query();
    while (gr.next()) {
        gr.state = '-4';
        gr.update();
    }
 
But is not working and not moving the state to Qualified Stage
9 REPLIES 9

Shruti
Mega Sage
Mega Sage

Hi,

Update the script

Shruti_0-1754568600523.png

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

 

 

Ankur Bawiskar
Tera Patron
Tera Patron

@Trupti Krishnam 

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.

Regards,
Ankur
Certified Technical Architect  ||  9x ServiceNow MVP  ||  ServiceNow Community Leader

@Ankur Bawiskar  the state is moving from 'screening' to 'Qualified' without checking for demand tasks 

even if the demand tasks are in open state 

TruptiKrishnam_0-1754577897105.pngTruptiKrishnam_1-1754577939154.png

 

 

 

@Trupti Krishnam 

are you using the correct field on demand_task which holds the demand?

Regards,
Ankur
Certified Technical Architect  ||  9x ServiceNow MVP  ||  ServiceNow Community Leader