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

Jack Littlewort
Giga Guru

Have you applied the filter manually to the change request table to double check nothing exists which would be causing the GlideRecord to find a change.

Below line did not do anything, Hence I have changed this line 

gr.addQuery('u_demand_task', current.sys_id); // Assuming 'demand_task' is the reference field

to

gr.addQuery('parent', current.sys_id); // Assuming 'demand_task' is the reference field

 Parent is a field on CHG record which hold Demand value & I found that it was validating all the CHG attached to this Demand instead of Demand task. When all the CHG attached to DMD was closed. BR was working as expected. 

Naveen87_0-1725962699639.png

Now the task is to show DMDTASK at Parent level & then validate.

I'm setting these values via UI action. 

Naveen87_1-1725962858143.png

 

Now when UI action 'create change' is clicked, Demand & Parent fields are not fetching anything. Suggest me on this. I believe once we get DTASK here & BR will work as expected.

Naveen87_2-1725962937406.png

 

instead of getValue('number') can you try getValue('sys_id')?

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