Iterating a list of users in postman

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.

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

@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) :slight_smile:

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...

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?

Anything you could do to expand upon :point_up: 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

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 :laughing:

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 :slight_smile:

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

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.