# Iterating a list of users in postman

**URL:** <https://community.postman.com/t/iterating-a-list-of-users-in-postman/10976>\
**Category:** Help Hub\
**Tags:** variables\
**Created:** [February 24, 2020, 2:48pm UTC](https://community.postman.com/t/iterating-a-list-of-users-in-postman/10976 "2020-02-24T14:48:53Z")\
**Posts on this page:** 4\
**Page:** 2

<div class="post-metadata">

**Author:** ![odanylewycz](https://sea2.discourse-cdn.com/getpostman/user_avatar/community.postman.com/odanylewycz/32/3240_2.png) [@odanylewycz](https://community.postman.com/u/odanylewycz)\
**Post date:** [March 10, 2020, 6:37pm UTC](https://community.postman.com/t/iterating-a-list-of-users-in-postman/10976/21 "2020-03-10T18:37:58Z")

</div>

Hi @j_who,

Yep! That should work, except of using `setNextRequest`, I would just script out the http call directly in the test script. So given our previous posts in this thread, you should have enough info on how to do this, using this syntax mentioned above. For the `service_task_ids` array, I would create an array of service\_task objects, so that you can use the “dot” syntax for each service task that you iterate over, I have a rough example below, this should cover - #2 and #3. I assume #1 is self explanatory, that being the Postman request you create in the UI.

```auto
service_task_list.forEach(function (service_task) {

pm.sendRequest({
url: url,
method: "POST",
header: {
    'Authorization': 'Basic ' + btoa('username:password')
},
body: {
        mode: 'raw',
        raw: JSON.stringify({ service_task_id: "{{service_task.id}}", description: "{{service_task.description}}" }) //etc, etc
    }
}, function (err, res) {
console.log("Sent Activation!")
});
})

```

I know this is a bit of a rough outline, but I hope it helps! Depending on how you organize the data in your csv file, you can create the service\_tasks objects in the script, or your read the json object from the file. Let me know if you would like some clarity on that. In my opinion, it would be easier to just have the json object in the csv file, unless you have other places you need to use pieces of that data.

Thanks,  
Orest

---

<div class="post-metadata">

**Author:** ![j\_who](https://sea2.discourse-cdn.com/getpostman/user_avatar/community.postman.com/j_who/32/3886_2.png) [@j\_who](https://community.postman.com/u/j_who)\
**Post date:** [March 23, 2020, 2:25pm UTC](https://community.postman.com/t/iterating-a-list-of-users-in-postman/10976/22 "2020-03-23T14:25:39Z")

</div>

@odanylewycz Hi, I’ve been working on this, and have been in touch w/ the developer as I couldn’t get any `service_task_id` array to successfully work. I wanted to thank you for your example above.

Somewhat frustratingly, the culprit was syntax - not explicitly included in the documentation. _(They’ve told me they are going to update my suggestions to their v2 ReadMe docs page based on this feedback)_ 🙂

I was only missing an integer corresponding to the number of Service Entry Line Items being sent - for example, for my first Service Entry Line Item, I would need to use these parameters:

`service_entry_line_items_attributes[0][type]`

and then to continue to add additional line items, you would simply increment each time.

> `service_entry_line_items_attributes[0][type]` _\*required_  
> `service_entry_line_items_attributes[0][service_task_id]` _\*required_  
> `service_entry_line_items_attributes[0][description]`  
> `service_entry_line_items_attributes[0][labor_cost]`  
> `service_entry_line_items_attributes[0][parts_cost]`

> `service_entry_line_items_attributes[1][type]` _\*required_  
> `service_entry_line_items_attributes[1][service_task_id]` _\*required_  
> `service_entry_line_items_attributes[1][description]`  
> `etc. etc...`

 ![Screen Shot 2020-03-11 at 8.03.07 AM](https://us1.discourse-cdn.com/getpostman/original/3X/e/a/eab5d42d46865cb3d83d305adf0a8dccfafcd213.png)

**So with this ironed out, I am thinking the solution is very close!**

I understand the logic in your most recent example and the benefits of just calling out the `pm.sendRequest` in the test script, but I’m not experienced enough with the dot notation you mention to know how to format these line item attributes correctly, and dynamically.

- An alternative I’ve been doing - while not great - is filtering my data outside of Postman and then doing a collection run for all PO’s that have Z number of line items, then Y number of line items, then X and so forth.
- This is b/c the `service_entry_line_items_attributes[0][type]` and `service_entry_line_items_attributes[0][service_task_id]` are required fields, however many I have selected in my body, I need to ensure the data has values for those keys.

I’m also wondering if it would just be simpler to have two requests - one to create the `service_entry` and the other to populate the applicable `service_entry_line_items` from my csv?

> [@odanylewycz](#):
>
> Depending on how you organize the data in your csv file, you can create the service\_tasks objects in the script, or your read the json object from the file. Let me know if you would like some clarity on that.

Anything you could do to expand upon ☝ would be awesome. I have my csv currently configured so there is a column for every one of the `attributes[]` mentioned above in a single row. My maximum service entry has 18 line items.

Thank you - J

---

<div class="post-metadata">

**Author:** ![odanylewycz](https://sea2.discourse-cdn.com/getpostman/user_avatar/community.postman.com/odanylewycz/32/3240_2.png) [@odanylewycz](https://community.postman.com/u/odanylewycz)\
**Post date:** [March 24, 2020, 4:50am UTC](https://community.postman.com/t/iterating-a-list-of-users-in-postman/10976/23 "2020-03-24T04:50:13Z")

</div>

Hi @j_who!

I hope you’re doing well.

You’re welcome! Good thing you got a hold of the developer! I can’t say thats the first time I have had to reach out to development staff in order to get better clarity on their documentation 😆

So bear with while I get the basis here again, but it seems like from the documentation you have sent before, and this post you just submitted, that you can create the service entry and its line items all in one request? If so, that would be convenient, although I can see it getting more complicated as you have mentioned.

However with the proper formatting, you should be able to do it, at least thats what I am thinking. And if you do it this way, you wont have to do two requests, and therefore not have to use pm.sendRequest (Thats if my understanding is correct).

Anyway, based on your request, I would make it like so (with Content-Type header as application/json)

```
{

"completed_at": "2020-03-05",
"vehicle_id": 753696,
"service_entry_line_items_attributes[0][service_task_id]": 10684,
"service_entry_line_items_attributes[0][description]": "hello world",
"service_entry_line_items_attributes[0][type]": "ServiceEntryServiceTaskLineItem",
"service_entry_line_items_attributes[0][labor_cost]": 42,
"service_entry_line_items_attributes[0][parts_cost]": 90,
"meter_entry_attributes[value]": 10,
"service_entry_line_items_attributes[1][service_task_id]": 2819562,
"service_entry_line_items_attributes[1][description]": "foo",
"service_entry_line_items_attributes[1][type]": "ServiceEntryServiceTaskLineItem"

}

```

Now, if I were to do this with a csv file using Postman Runner, you could consolidate it like so:

```
{

"completed_at": "2020-03-05",
"vehicle_id": 753696,
{{service_entry_line_items}}
"meter_entry_attributes[value]": 10,

}

```

Here, `service_entry_line_items`, is the name of your column in your csv file, and the value is:

```
"service_entry_line_items_attributes[0][service_task_id]": 10684,
"service_entry_line_items_attributes[0][description]": "hello world",
"service_entry_line_items_attributes[0][type]": "ServiceEntryServiceTaskLineItem",
"service_entry_line_items_attributes[0][labor_cost]": 42,
"service_entry_line_items_attributes[0][parts_cost]": 90,
"service_entry_line_items_attributes[1][service_task_id]": 2819562,
"service_entry_line_items_attributes[1][description]": "foo",
"service_entry_line_items_attributes[1][type]": "ServiceEntryServiceTaskLineItem"

```

Literally, all of that is the value, so when the collection runner goes to pull it out of your csv, its already there. Now this does mean that you might have to preformat the data that you receive, so that its in the proper JSON format, but this saves you from having to do multiple calls, or making a more complicated pre-request script. Or better yet, if whomever gives you this data can format it in this way, then that makes life easier for you 🙂

Now if you go with two requests, you can use the example I wrote above. What I mentioned above here should explain what I wrote in my last reply. If you make that above as a json object (would just have to add the curly braces to the front and the back), then you pull it in as a serialized object, and use it as a native javascript object. And that is where the dot syntax comes in. And a column of `service_entry_line_items`, the values would literally look like so:

```
[ {
        "service_task_id": 10684,
        "description": "hello world",
        "type": "ServiceEntryServiceTaskLineItem",
        "labor_cost": 42,
        "parts_cost": 90,
}, {
        "service_task_id": 2819562,
        "description": "foo",
        "type": "ServiceEntryServiceTaskLineItem", ...
    }
]

```

In this array, you have two JSON objects. Now since they are JSON objects, you can use them as you would in the `pm.sendRequest` function I wrote above. But again, this requires preformating the csv to get these objects in there ahead of time.

Honestly either way is fine, but its the best way to do it dynamically. Its difficult to do it by having the column header as the value, because you know that some may have more service line items than others, and if you try to use it in a standard templated request, it might now work because it could cause the request to be improperly formated, because a column like `part_cost_3` doesn’t have a value for say the second service entry.

I really do hope this explanation helps and makes sense. If not, I can try to explain it in some other way, maybe with a video.

Hope this gets you what you need!

Best,  
Orest

---

<div class="post-metadata">

**Author:** ![JohnyB](https://avatars.discourse-cdn.com/v4/letter/j/ec9cab/32.png) [@JohnyB](https://community.postman.com/u/JohnyB)\
**Post date:** [July 6, 2020, 9:39pm UTC](https://community.postman.com/t/iterating-a-list-of-users-in-postman/10976/24 "2020-07-06T21:39:34Z")

</div>

Good afternoon hope all is well! I’m new to the postman and I’m trying to add a user list from 0. All users at ones or one by one any help will be appreciated. Thanks ahead of time.

[Previous page](https://community.postman.com/t/iterating-a-list-of-users-in-postman/10976.md?page=1)
