Join the #BuildWithBuildAgent Challenge! Get recognized, earn exclusive swag, and inspire the ServiceNow Community with what you can build using Build Agent.  Join the Challenge.

Business Rule is not working

Shidhi
Tera Contributor

Trying to cancel a change when the change task gets canceled, it is not working below is the code

BR on change_task table, order 10 triggers when the status is closed canceled

 

var gr = new GlideRecord('change_request');
gr.addQuery('active',true);
gr.addQuery('number', current.parent);
gr.query();
while(gr.next()){
	gr.state = '4';
	gr.update();
}

 I have seen a Set activity in the workflow which is making the change the progress to a further stage. But if I try to adjust the workflow to cancel the change if ctask is canceled some tasks are not getting created.

 

Any idea what is going wrong here.

 

Thank you!

2 ACCEPTED SOLUTIONS

Hajar BENJAHHAR
Mega Sage

Hello Shidhi, 

 

current.parent returns the sys_id rather than the number. In your script,  you have to use current.parent.number, instead.  Also,  there is no need to use a while loop,  as change_task is associated to only one change_request : 

 

 

var gr = new GlideRecord('change_request');
gr.addQuery('active',true);
gr.addQuery('number', current.parent.number);
gr.setLimit(1); // for performance. 
gr.query();
if(gr.next()){
	gr.state = '4';
	gr.update();
}

 

 

Best regards, 

Hajar

View solution in original post

Satishkumar B
Giga Sage
Giga Sage

@Shidhi your code looks correct. only Update gr.addQuery('number', current.parent); to gr.addQuery('number', current.parent.number); to correctly retrieve the number of the parent change request from the change task.

…………………………………………........................................................................................
Please Mark it helpful 👍and Accept Solution !! If this helps you to understand.

…………………………………………........................................................................................

View solution in original post

3 REPLIES 3

Harsh Vardhan
Giga Patron

Hi @Shidhi  try now

 

var gr = new GlideRecord('change_request');
gr.addQuery('active',true);
gr.addQuery('sys_id', current.parent);
gr.query();
while(gr.next()){
	gr.state = '4';
	gr.update();
}

 

Thanks,

Harsh

Hajar BENJAHHAR
Mega Sage

Hello Shidhi, 

 

current.parent returns the sys_id rather than the number. In your script,  you have to use current.parent.number, instead.  Also,  there is no need to use a while loop,  as change_task is associated to only one change_request : 

 

 

var gr = new GlideRecord('change_request');
gr.addQuery('active',true);
gr.addQuery('number', current.parent.number);
gr.setLimit(1); // for performance. 
gr.query();
if(gr.next()){
	gr.state = '4';
	gr.update();
}

 

 

Best regards, 

Hajar

Satishkumar B
Giga Sage
Giga Sage

@Shidhi your code looks correct. only Update gr.addQuery('number', current.parent); to gr.addQuery('number', current.parent.number); to correctly retrieve the number of the parent change request from the change task.

…………………………………………........................................................................................
Please Mark it helpful 👍and Accept Solution !! If this helps you to understand.

…………………………………………........................................................................................