How to loop through object in sub flow ? (The object is returned by an ACTION in flow designer)

Snehal13
Kilo Sage

My action makes an API call and this API returns the below response body

{
    "requestDetails": {
        "comments": "mycomments",
        "requestParams": [
            {
                "requestkey": "342",
                "Start Date": "2025-05-15 12:44:25",
                "Request Type": "AddRequest",
                "value": "asasasasa01G3PCBKB170TEP3CC2BBH",
                "status": "Request Created"
            }
        ],
        "Tasks": [
            {
                
                "Task ID": 999,
                "State": "New",
                "comments": " test comments"
            },
           {    
                "Task ID": 29,
                "State": "Error",
                "comments": " test 123comments"
            }  // these tasks could be multiple each with a diff ID and State

        ],
    },
    "msg": "SUCCESS",
    "errorCode": "0"
}

The "Tasks" can be multiple or single. It depends on the API. 

The action returns couple of values back to the subflow - errorCode, entire 'Tasks' object.

 

When errorCode is 0, I want to loop through the Tasks object in my subflow to do the following. The conditional check for validating errorCode is already in place. not getting how to iterate through the object in subflow to achieve -

1. create a new sc task when task id with state = Error.

1. update main ritm to complete when all task id with state = Completed.

 

How to loop through object in sub flow ? I am using flow designer

 

2 ACCEPTED SOLUTIONS

OlaN
Giga Sage
Giga Sage

Hi,

If you define the output from the action as an object containing an array you should be able to loop through the array, just as you would when performing a lookup of records.

See attached image.

loop-through-array.png

View solution in original post

If you don't know which one of them contains data, you'll need to loop through all three arrays.

Or, if possible, have the output of the action concatinate all the data into one big array, and loop through that instead.

View solution in original post

9 REPLIES 9

Akiladevi Raje1
Giga Guru

 @Snehal13 

Use below code for 1. create a new sc task when task id with state = Error.

var jsonReq={
"requestDetails": {
"comments": "mycomments",
"requestParams": [
{
"requestkey": "342",
"Start Date": "2025-05-15 12:44:25",
"Request Type": "AddRequest",
"value": "asasasasa01G3PCBKB170TEP3CC2BBH",
"status": "Request Created"
}
],
"Tasks": [
{

"Task ID": 999,
"State": "New",
"comments": " test comments"
},
{
"Task ID": 29,
"State": "Error",
"comments": " test 123comments"
} // these tasks could be multiple each with a diff ID and State

],
},
"msg": "SUCCESS",
"errorCode": "0"
}

if(jsonReq.errorCode=='0')
{
gs.info("legth:"+jsonReq.requestDetails.Tasks.length);
var numTasks=jsonReq.requestDetails.Tasks.length;

for(var i=0;i<numTasks;i++){
var createTasks = new GlideRecord("sc_task");
createTasks.initialize();
for (var key in jsonReq.requestDetails.Tasks[i]) {
gs.info("key"+[key]+"<value>"+jsonReq.requestDetails.Tasks[i][key]);
//createTasks.short_description =jsonReq.requestDetails.Tasks[i][key]; //mapping

}
gs.info("insert"+createTasks.insert());
}
}
Please mark the answer helpful and correct if this resolves your issue.

Note - How to loop through object in sub flow ? I am using flow designer

Ankur Bawiskar
Tera Patron
Tera Patron

@Snehal13 

you can use JSON parser step in flow

check these videos

Episode 25 - Complex Objects in ServiceNow Flow Designer

How To - Use the integrationHub JSON Parser - Paris Release 

also check these links

Script for JSON Parse Action in Flow Designer 

Flow/subflow array.object value extraction 

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

I have object from action received into a subflow. Lets says the object is below. I have to loop through the "Tasks" below in my sub flow and reach each task id and state and based on the state, want to create sc task or update ritm. 

{
    "requestDetails": {
        "comments": "mycomments",
        "requestParams": [
            {
                "requestkey": "342",
                "Start Date": "2025-05-15 12:44:25",
                "Request Type": "AddRequest",
                "value": "asasasasa01G3PCBKB170TEP3CC2BBH",
                "status": "Request Created"
            }
        ],
        "Tasks": [
            {
                
                "Task ID": 999,
                "State": "New",
                "comments": " test comments"
            },
           {    
                "Task ID": 29,
                "State": "Error",
                "comments": " test 123comments"
            }  // these tasks could be multiple each with a diff ID and State

        ],
    },
    "msg": "SUCCESS",
    "errorCode": "0"
}