Close all change tasks when state is in Review state

hugogomes
Giga Expert

Hi community,

I am trying to close all the change tasks open for any change request that reaches the Review state.
I will not change the workflow to have a script include since I only want for this to happen to certain changes, in this case for the ones I have an Integration with Remedy.

The state in the change workflow is controlled by UI Action, this means that to change the state, it needs to be done manually.

In the case of the integration, we receive a message that automatically change the state to Review. Review is the last state of the workflow.

I have tried to include a BR to have the tasks closed but it seems that it is not working, and I think it should... since I have picked up the code we have on wiki.

BR: onBefore

Table: change_request

var ctask = new GlideRecord('change_task');

ctask.addQuery('change_request', current.sys_id);

ctask.query();

while(ctask.next()){

ctask.state.setValue(3);

ctask.work_notes = "Task automatically closed";

}

current.work_notes = "Tasks automatically closed";

Instead of having the "Closed" state I have "Closed Incomplete".

Any idea of why this is happening?

Thank you in advance!

1 ACCEPTED SOLUTION

I've found a solution for this.



Dumb mistake, it was only missing a ctask.update(); inside the while loop.



All working now


View solution in original post

7 REPLIES 7

lks
ServiceNow Employee
ServiceNow Employee

Hello Hugo,



I see that your BR is an onBefore. Isn't it should be an after or async BR script because you want to close a task once it reaches Review state? Also, specifically the check the order value of your script comparing with one which changes the state to Review. The order of scripts plays a crucial role when it involves workflow.


I have tried to change it to OnAfter and I have the same result, "Closed Incomplete".


Also the work note is not showing in the ctask.


lks
ServiceNow Employee
ServiceNow Employee

Hello Hugo,



I am pretty sure that it should work with an onAfter business rule script. Have you tried adding the debug logs to verify the flow of your script? When I say debug logs, I mean using gs.log statements that will help.



Also try replacing the script state "ctask.state.setValue(3);" with "ctask.state = '3';" and check if that makes any difference?


rajeevnow
Kilo Contributor

Hi,



Please check the choices values for change_request by opening a change ticket   - > (On state dropdown > show choice) -> check appropriate value for closed.



Code looks good to me,



Thanks,


Best Regards,


Rajeev Saraf