Attached CHG should be closed before closing Demand Task

Naveen87
Tera Guru

Hello Developers,

 

We have an UI action 'create change' on Demand task table.

Each Demand task will have an CHG attached to it.

Before closing the Demand Task, The attached CHG must be closed.

I have wrote a below BR but does not seem working. 

Can you please suggest something?

 

3 is closed for both CHG & Demand task

(function executeRule(current, previous /*null when async*/) {

	if (current.state == '3' && previous.state != '3') {
        var gr = new GlideRecord('change_request'); // Replace with the related table name
        gr.addQuery('u_demand_task', current.sys_id); // Assuming 'demand_task' is the reference field
        gr.addQuery('state', '!=', '3');
        gr.query();
        
        if (gr.hasNext()) {
            gs.addErrorMessage('Cannot close this Demand Task until related Changes are closed.');
			gs.info('Demand Task: ' + current.sys_id + ' has open related changes.');
            current.setAbortAction(true);
        }  else {
            gs.info('Demand Task: ' + current.sys_id + ' can be closed as all related changes are closed.');
        }
    }

})(current, previous);

I get the error even when CHG is closed.

Please suggest what went wrong here.

Naveen87_0-1725960217907.png

 

 

1 ACCEPTED SOLUTION

Danish Bhairag2
Tera Sage
Tera Sage

Hi @Naveen87 ,

 

You can add the filter condition in the BR when to run section as state changes to closed. Then u can try below code


        var gr = new GlideRecord('change_request'); // Replace with the related table name
        gr.addQuery('u_demand_task', current.sys_id); // Assuming 'demand_task' is the reference field
        gr.addQuery('state', '!=', '3');
        gr.query();
        
        if (gr.next()) {
            gs.addErrorMessage('Cannot close this Demand Task until related Changes are closed.');
	gs.info('Demand Task: ' + current.sys_id + ' has open related changes.');
            current.setAbortAction(true);
        }  else {
            gs.info('Demand Task: ' + current.sys_id + ' can be closed as all related changes are closed.');
        }

 

Thanks,

Danish

View solution in original post

6 REPLIES 6

Hi @Danish Bhairag2 , No luck. Please review & suggest

if (current.state == '3' && previous.state != '3') {


        var gr = new GlideRecord('change_request');
		gr.addQuery('u_demand_task', current.sys_id); // u_demand_task is the field on CHG which hold Demand Task number
        gr.addQuery('state', '!=', '3');
        gr.query();
        
        if (gr.hasNext()) {
            gs.addErrorMessage('Cannot close this Demand Task until related Changes are closed.');
			gs.info('Demand Task: ' + current.sys_id + ' has open related changes.');
            current.setAbortAction(true);
        }  else {
            gs.info('Demand Task: ' + current.sys_id + ' can be closed as all related changes are closed.');
        }
    }

This worked for me.

Naveen87_0-1726047114691.png

(function executeRule(current, previous /*null when async*/ ) {

    if (current.state == '3' && previous.state != '3') {
        var gr = new GlideRecord('change_request');
        gr.addQuery('u_demand_task', current.sys_id); // u_demand_task = Field on CHG
        gr.addQuery('state', '!=', '3');
        gr.query();

        if (gr.hasNext()) {
            gs.addErrorMessage('Cannot close this Demand Task until related Changes are closed.');
            gs.info('Demand Task: ' + current.sys_id + ' has open related changes.');
            current.setAbortAction(true);
        } else {
            gs.info('Demand Task: ' + current.sys_id + ' can be closed as all related changes are closed.');
        }
    }


})(current, previous);