The CreatorCon Call for Content is officially open! Get started here.

Flow designer

Pratiksha KC
Tera Guru

We have a variable named as - "How many tasks required" - Options will be - 

1. One task

2. Two tasks

3. Three tasks 

 

Depending of fulfiller's selection tasks are generating

 

Now the requirement is -  (We are not sure how many tasks are going to select by fulfiller)

 

Condition First - If any one task is in closed incomplete state and others are closed complete or closed skipped than, state of RITM should state as "closed Incomplete"

 

Condition Second- If any one task is in closed complete and others are in closed skipped than, state of RITM should be state as "Closed Complete"

 

Condition Third- If all tasks are in closed skipped than state of RITM should state as "Closed Skipped"

 

How can we achieve this using flow designer. 

 

2 ACCEPTED SOLUTIONS

Prinssi
Mega Sage

Hello @Pratiksha KC ,

 

Here's a sequence to consider, assuming that all tasks are created in parallel:

  1. Create a new flow with a trigger condition of SCTASK is updated with the conditions:
    • Active changes to false AND
    • Item is your catalog item for the request
  2. Create four integer Flow Variables:
    1. incomplete_state
    2. complete_state
    3. skipped_state
    4. open_tasks
  3. In the first step, use Set Flow Variables to set the Data value for all four Flow Variables to 0
  4. Then, use the Look up records action with the conditions of:
    • Item is your catalog item
    • RITM is the parent RITM of the current SCTASK record
  5. Next, use the For Each action and drag set the Items as the records that were looked up in the previous step
  6. In the For Each action branch, create the following logic:
    • If state == closed_incomplete
      • Set the flow variable value of incomplete_state to increment up by 1
    • Else if state == closed_complete
      • Set the flow variable value of complete_state to increment up by 1
    • Else if state == closed_skipped
      • Set the flow variable value of skipped_state to increment up by 1
    • Else
      • Set the flow variable value of open_tasks to increment up by 1
    • Note: To increment the flow variables, you will use the Set Flow Variables action.
      • Choose the applicable flow variable from the dropdown
      • Drag the data pill of the same flow variable to the new value section
      • Hover over the data pill you just added to show, and then select, the Transform Functions option
      • Under the Math transform section, select the Add option, and set the Number to 1
      • Click Apply
      • See example image below
  7. After the For Each action (back on the main branch of the Flow), create the following logic:
    • If incomplete_state is greater than or equal to 1 AND open_tasks is 0
      • Update the RITM to closed_incomplete state
    • Else if complete_state is greater than or equal to 1 AND open_tasks is 0
      • Update the RITM to closed_complete state
    • Else if skipped_state is greater than or equal to 1 AND open_tasks is 0
      • Update the RITM to closed_skipped state

    • Note: I did not add an "Else" statement here, because if there are any open tasks, we don't want to take action on the RITM until the remaining tasks have been closed

One more consideration to add: You may want to create this as a Subflow if there is any chance that this logic is to be repeated on any other Flow. To do this, you would complete steps 2-7 in the Subflow record. The Subflow would have two inputs: an SCTASK record, and a Catalog Item record. Your Flow would still be set up as outlined in step 1, and then you would add the Subflow as your first step, and pass in the input values.

 

Image referenced in step 6:

Prinssi_0-1753114009903.png

View solution in original post

Aniket Chavan
Tera Sage
Tera Sage

Hello @Pratiksha KC ,

Yes, this requirement is definitely achievable using Flow Designer only—everything can be handled within a single flow.

 

I had a bit of free time today, so just quickly tried and tested the logic in my personal developer instance (PDI), and it’s working exactly as expected 🙂

 

Let me walk you through the approach step-by-step so you can set this up on your side:

  1. Step 1: Add a "Get Flow Variables" Step
    Start by adding a "Get Flow Variables" action in your flow to retrieve the value of your catalog variable:

    Variable name: how_many_tasks_required

    The output from this is used in the script to determine how many tasks to expect.

  2. Flow Variable Logic
    First, I created a flow logic variable which does all the work in script and acts as a decision-maker for what the RITM’s state should be based on the states of its generated catalog tasks.

  3. Placed Flow Variable Calculation After Task Creation
    This flow variable step is placed after all your catalog tasks are created (so that their states can be correctly evaluated), and before you update the RITM’s state.

  4. Script Used in the Flow Variable Step 
    Here's the exact script I used inside the flow logic variable:

    /**
     * Script inside Flow Designer → Script step (Logic)
     */
    
    // Get the current RITM's sys_id
    var ritmSysId = fd_data.trigger.request_item;
    
    // Get the catalog variable value from flow variables
    var taskCountVar = fd_data._1__get_catalog_variables.how_many_tasks_required;
    
    // Map variable value to expected task count
    var expectedTaskCountMap = {
        'one_task': 1,
        'two_tasks': 2,
        'three_tasks': 3
    };
    
    var expectedTaskCount = expectedTaskCountMap[taskCountVar] || 0;
    
    // Query catalog tasks related to this RITM
    var taskGR = new GlideRecord('sc_task');
    taskGR.addQuery('request_item', ritmSysId);
    taskGR.query();
    
    // Initialize counters
    var countComplete = 0;
    var countIncomplete = 0;
    var countSkipped = 0;
    var totalTasks = 0;
    
    // Loop through the tasks and count states
    while (taskGR.next()) {
        totalTasks++;
    
        var taskState = taskGR.getValue('state');
    
        if (taskState === '3') {
            countComplete++;
        } else if (taskState === '4') {
            countIncomplete++;
        } else if (taskState === '7') {
            countSkipped++;
        }
    }
    
    // Determine RITM state based on task states
    var ritmState = null;
    
    if (countIncomplete > 0) {
        ritmState = 4; // Closed Incomplete
    } else if (countComplete > 0 && (countComplete + countSkipped === totalTasks)) {
        ritmState = 3; // Closed Complete
    } else if (countSkipped === totalTasks && totalTasks > 0) {
        ritmState = 7; // Closed Skipped
    }
    
    // Return the new state to be used in Update Record
    return ritmState;
    

and then you can simple use this flow variables output to set or update the RITM's state.

adding few screenshots just for your understanding.

FYI- I did not added the dynamic catalog task function since you were not facing any blockers there.

AniketChavan_0-1753115635092.png

 

 

AniketChavan_1-1753115669091.png

 

  • Place both the Script Logic and Update Record steps after your Catalog Task creation.

  • This way you can keep all processing within Flow Designer.

  • The script as written covers the three main conditions:

    • If any task is Closed Incomplete, the RITM is set to Closed Incomplete.

    • If at least one task is Closed Complete and the rest are Closed Skipped, the RITM is set to Closed Complete.

    • If all tasks are Closed Skipped, the RITM is set to Closed Skipped.

  • You can test this flow by setting up various task state combinations and confirming the RITM moves to the correct state.

Consider that, depending on how many tasks are created and what states they reach, you may wish to extend this logic further in the future to support additional scenarios.

 

Let me know if you have any questions or need further clarification.

 

🔹 Please mark Correct if this solves your query, and 👍 Helpful if you found the response valuable.

 

Best regards,
Aniket Chavan
🏆 ServiceNow MVP 2025 | 🌟 ServiceNow Rising Star 2024

View solution in original post

6 REPLIES 6

SumanthDosapati
Mega Sage
Mega Sage

@Pratiksha KC 

Are those tasks triggered parallelly? or one after the other?

 

 

@SumanthDosapati 

 

Parallelly 

If fulfiller is selecting two tasks or three tasks 

kaushal_snow
Mega Sage

Hi @Pratiksha KC ,

 

To achieve this using Flow, first create a subflow that iterates through the associated catalog tasks (SCTASKs) of the RITM, counts the occurrences of each relevant state (Closed Complete, Closed Incomplete, Closed Skipped), and updates the RITM state based on the specified conditions. For example, if any SCTASK is in a "Closed Incomplete" state, set the RITM state to "Closed Incomplete". If any SCTASK is in "Closed Complete" and none are in "Closed Incomplete", set the RITM state to "Closed Complete". If all SCTASKs are in "Closed Skipped", set the RITM state to "Closed Skipped".

 

 

Thanks and Regards,
Kaushal Kumar Jha - ServiceNow Consultant - Lets connect on Linkedin: https://www.linkedin.com/in/kaushalkrjha/

Ankur Bawiskar
Tera Patron
Tera Patron

@Pratiksha KC 

you can use after update business rule and update the RITM state

After Update: sc_task

Condition: State [Changes to] [Close Complete or Closed Incomplete or Close Skipped]

Script:

// Table: sc_task
// When: after update
// Condition: current.request_item.changes() -- or always if you want

(function executeRule(current, previous /*null when async*/) {
    var ritmID = current.request_item;
    if (!ritmID) return;

    // Query all tasks for this RITM
    var grTask = new GlideRecord('sc_task');
    grTask.addQuery('request_item', ritmID);
    grTask.query();

    var total = 0;
    var closedComplete = 0;
    var closedSkipped = 0;
    var closedIncomplete = 0;

    while (grTask.next()) {
        total++;
        switch (grTask.state.toString()) {
            case '3': // Closed Complete
                closedComplete++; break;
            case '7': // Closed Skipped
                closedSkipped++; break;
            case '4': // Closed Incomplete
                closedIncomplete++; break;
        }
    }

    var ritm = new GlideRecord('sc_req_item');
    if (ritm.get(ritmID)) {
        var newState = null;

        // --- Logic per your requirements ---
        // All Closed Skipped
        if (closedSkipped == total) {
            newState = 7; // Closed Skipped
        }
        // Any Closed Incomplete, and all others are Closed Skipped or Closed Complete
        else if (closedIncomplete > 0 && (closedIncomplete + closedComplete + closedSkipped) == total) {
            newState = 4; // Closed Incomplete
        }
        // Any Closed Complete, all others are Closed Skipped
        else if (closedComplete > 0 && (closedComplete + closedSkipped) == total) {
            newState = 3; // Closed Complete
        }

        // Update RITM state if needed
        if (newState && ritm.state != newState) {
            ritm.state = newState;
            ritm.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