How to update state of the TASK based on previous task using Flow designer?

SpartanW
Tera Contributor

Hello, everyone.

 

I created a flow designer and generated three tasks at once, in the order of 1 to 3.

The first task is in an open state, and the rest are pending state while being created.

I want to update the state to Open for the 2nd task when the previous or first task is completed and it should continue.

How can we do using Flow Designer?

See the attachment picture.

SpartanW_0-1750427246042.png

Thanks.

7 REPLIES 7

Ankur Bawiskar
Tera Patron
Tera Patron

@SpartanW 

if you want the 2nd task to be opened after 1st is completed then why not add that in sequence?

why to add them in parallel?

what's your actual business requirement?

If my response helped please mark it correct and close the thread so that it benefits future readers.

Regards,
Ankur
Certified Technical Architect  ||  9x ServiceNow MVP  ||  ServiceNow Community Leader

Hi @Ankur Bawiskar ,

This is actual Business Requirement - 

all related Catalog Tasks should be generated. The tasks that should be completed first should be generated in State = Open, and the tasks that should be completed later should be generated in State = Pending and moved to State = Open once the previous tasks are moved to Closed Complete or Closed Skipped.

@SpartanW 

Then I will suggest to use after update business rule on sc_task table to check if this is the 1st task which is getting closed, If yes then mark the next task as Open.

Business rule: After Update on sc_task

Condition: State [Changes to] Closed Complete/Close Skipped
Script: Something like this but please enhance and test

(function executeRule(current, previous /*null when async*/ ) {
   
    // Get all tasks for this request item, sorted by created date
    var taskGr = new GlideRecord('sc_task');
    taskGr.addQuery('request_item', current.request_item);
    taskGr.orderBy('sys_created_on');
    taskGr.query();

    var firstTask = null;
    var nextTask = null;
    var foundCurrent = false;

    // Find first task and the next task after the current one
    while (taskGr.next()) {
        if (!firstTask)
            firstTask = taskGr.sys_id.toString();

        if (taskGr.sys_id.toString() == current.sys_id.toString()) {
            foundCurrent = true;
            continue;
        }
        if (foundCurrent && !closedStates.includes(taskGr.state)) {
            nextTask = taskGr;
            break;
        }
    }

    // If current is the first task and there is a next task, open it
    if (current.sys_id.toString() == firstTask && nextTask) {
        nextTask.state = 1; // 1 = Open
        nextTask.update();
    }
})(current, previous);

If my response helped please mark it correct and close the thread so that it benefits future readers.

Regards,
Ankur
Certified Technical Architect  ||  9x ServiceNow MVP  ||  ServiceNow Community Leader

@Ankur Bawiskar , Is it not possible using the flow designer only?