Auto closure of open Demand tasks when demand is closed

Tracy Hardman
Tera Contributor

We have chosen to close demands when a project is created and I found in the DemandUtil Script Include where the closure happens; however, this doesn't close the open demand tasks.  Does anyone have a solution for closing the demand tasks along with the demand?  Would you recommend updating them in the same section of this script?  I assume I could do another query against dmn_demand_task and iterate through the open ones setting them to closed?  Thoughts?

DemandUtil.checkAndCloseDemand = function(sysId){
    if (JSUtil.nil(sysId))
        return;
    var gr = new GlideRecord('dmn_demand');
    gr.get(sysId);
    if (gr.isValidRecord() && gr.getValue('close_demand') == 'on_closing_project'  && gr.getValue('state') != '9'){
        gr.setValue('state', '9');
        gr.update();
    }    
};

3 REPLIES 3

Musab Rasheed
Tera Sage
Tera Sage

Hello,

You can write after business rule on demand table and tweak below script accordingly. It's simple

https://community.servicenow.com/community?id=community_question&sys_id=db9ea15fdb497f44200f0b55ca961976

Regards

Please hit like and mark my response as correct if that helps
Regards,
Musab

I did try an after Business rule, but it doesn't seem to fire, when you click on the related link Create project, the project is created and the demand closes complete, but demand task is still open.  Here is the BR:

find_real_file.png

function onAfter(current, previous) {
    //This function will be automatically called when this rule is processed.
    var gr = new GlideRecord("dmn_demand_task");
    gr.addQuery("parent", current.parent.sys_id);
    gr.addEncodedQuery("state!=3");
    gr.query();
    while (gr.next()) {
        gr.setValue('work_notes', gs.getMessage("Closing task as the corresponding demand is closed"));
        gr.setValue("state", 3);
        gr.update();
    }
}

Am I missing something easy?  Thanks.

Saurabh S
Giga Guru
Giga Guru

I would suggest, run an after update Business Rule on dmn_demand_task table and make the condition as below:

 

1. Demand is <Name of your Demand>

2. State is Closed Incomplete

and the below script in advanced section:

var dmntask = new GlideRecord("dmn_demand_task");
    dmntask.addQuery("sys_id", current.parent.sys_id);
    dmntask.query();
    if(dmntask.next()) {
        dmntask.state = <state>;
        dmntask.stage = <stage>;
        dmntask.update();
    }