Trying to add a script to a flow to manipulate sc_task and sc_req_item states

Jason Edinger
Tera Expert

Hello!

I've been working on converting a workflow to a flow and, long story short, I ran out of actions because of the enormity of the amount of requestable items and who is responsible for approving them. In order to minimize the amount of actions under IF statements, I was trying to consolidate on state changes. Here's what it originally looked like:

 

If SCTask state is 'closed complete', set RITM state to 'closed complete'.

If SCTask is 'closed incomplete', set ritm to 'closed incomplete'.

If SCTask is 'closed skipped', set ritm to 'closed skipped'.

 

So you can see that each yields an action. I would do this with sub flows but I cannot wrap my head around how that works, so I figured let me try a script (albeit pieced together from snippets of google searches):

 

 

var task = new GlideRecord('sc_task');
var ritm == new GlideRecord('sc_req_item');
task.query()
if (task.state == "Closed Complete"){
  ritm.state = "Closed Complete")
}
else if (task.state == "Closed Incomplete"){
  ritm.state = "Closed Incmplete")
}
else if (task.state == "Closed Skipped"){
  ritm.state = "Closed Skipped")
}

 

 

Suffice it to say when I closed the sctask, the ritm didn't similarly close. I'm not much for scripting and at this point I think it may be easier for me to just clean up the workflow (rather than trying to recreate in flow designer).

 

Anyone able to help? Thanks!

6 REPLIES 6

Community Alums
Not applicable

Hi @Jason Edinger ,

i think you need to use a script action after the wait for conditions 

the script should look like the below-
(function execute(inputs, outputs) {
// Query for the sc_task record
var task = new GlideRecord('sc_task');
if (task.get(inputs.sc_task_sys_id)) {
// Query for the associated sc_req_item record
var ritm = new GlideRecord('sc_req_item');
if (ritm.get(task.request_item)) {
// Update the state of the ritm based on the state of the task
if (task.state == 3) { // Closed Complete
ritm.state = 3;
} else if (task.state == 4) { // Closed Incomplete
ritm.state = 4;
} else if (task.state == 7) { // Closed Skipped
ritm.state = 7;
}
ritm.update();
}
}
})(inputs, outputs);

 

Ensure to pass the sc_task_sys_id to the script action. This can be done by setting it as an input in the flow.

Note- update the right state for ritm in the script.

 

If you find the above helpful and solves your issue then please give it a thumbs by marking it helpful and accepting the solution.

 

Thanks and Regards,

Sanjay Kumar

I think the problem i had with my script was i wasn't using the numerical values. Didn't try that but thank you for that. Will give it a shot.