Hello! I am trying to use postman to iterate over a list of users to re-send an activation email. I am just diving into the functionality of Postman and am unable to find this. I have a Lifecycle POST url that is working when I do it one by one. Is there a way to have an array of userIds so I don’t have to set the userId over 100 times?
Welcome to the community! Its great to have you here.
What I would suggest for this is Postman Runner. With Postman Runner, you can create a json or csv file with those userIds, then in your Postman Request, set the userId variable (that refers to the data in your file) to the appropriate place in your POST Request, and the execute the Runner.
Have a look at the Postman Requests and Runner documentation here.
@odanylewycz,
This worked perfectly. I was able to create the list and iterate over all the values. The hardest part was getting the data. I tried to get it with Postman but no luck. Thank you again for the assistance!
And I’m curious, how were you getting the data? If it was via another web request, we could have scripted that out using its built in Nodejs environment, and fed that to your POST request for your activation emails.
How I got the data was probably not the best way. I was trying to run an API call and filter on an attribute. I was able to do this but the data that was returned was the entire user profile. I was unable to get the email address so I just copied and pasted. I will need to do this again in the future. Can I use postman to query the data I need and then run the POST?
Perfect! So what we can do is create a Postman Request with the “GET” you specified.
Then, we can parse the results in the test script, to obtain the userId, and create a new POST request in the test script to send off the reactivation email.
pm.test("Parse UserIDs And Send Activation Email", function () {
var users = JSON.parse(responseBody);
users.forEach(function (user) {
console.log(user.id)
var url = "domain.com" + "/api/v1/users/" + user.id + "/lifecycle/reactivate?sendEmail=true"
pm.sendRequest({
url: url,
method: "POST",
header: {
'Authorization': 'Basic ' + btoa('username:password')
}
}, function (err, res) {
console.log("Sent Activation!")
});
})
});
Note, the above assumes you use basic authentication. If some other form of authentication, add it to the header.
But with this, it will perform the POST the sent the activation email to any user to comes in the response for the GET request, you specify in your Postman collection.
@odanylewycz Thank you for this information. I need to see how to run a test script. Im still a noob and learning! But if that will work I will definitely use in the future!
@odanylewycz, first thank you for the detailed response and code examples for this iterative use case.
I’m working through a similar project, and this thread has been very helpful for me.
I have a bit a twist; however, that is giving me difficulties and would really appreciate your insight. I have a csv of automotive service records and need to write them into a new system via its API.
I have a collection consisting of two POST requests:
Creates a service_entry for a vehicle (this represents a single service event) and writes basic info related to the vehicle, date, odometer, and vendor who performed the work.
The other creates a single line item w/in that service_entry- called a service_task. A given service entry can have multiple line items that represent a complete service event.
I’m having trouble with 2 parts of this project: First, some service entries will have a single service_task while others will have multiple service_tasks and I want to be sure all are added before moving onto the next service entry. Conditional statement (?) Second, I’m having issues obtaining the id from the JSON response of my first request that is needed to create the line item entries.
Note: This sequence is a requirement of the API I’m working with, as I can’t create the entire entry in the way the user wants in one attempt from just the /service_entry endpoint.
Details:
The first POST references a {{vehicle_id}} variable - I’m using the .csv import data features of Postman to read in these values and put them into the url of the POST that creates the service entry. This is working for me
When it’s time to move onto the first service_task run, the {{id}} from the created service_entry is needed in the URL. This is the script I have - my intent is for this to fetch the {{id}} variable so I can use it in my next call.
and using the guidance there - thanks @danny-dainton!
I changed my test script from this (which was not working:
// Test for successful command
pm.test("Status 201, service entry created!", function () { pm.response.to.have.status(201); });
pm.test("Checking for service_entry_id", function () {
// Parse the response
pm.expect(pm.response.json())
.to.nested.include({
'args.id': JSON.stringify(pm.iterationData.get('id'))
});
console.log("Creating new line item service_task for entry: " + pm.iterationData.get("id"));
});
postman.setNextRequest("Create Service Line Item");
To this- which works to set the {{id}} variable in my subsequent request for the collection run :
// Test for successful command
pm.test("Status 201, service entry created!", function () { pm.response.to.have.status(201); });
// Parse JSON - set environment variable!
var jsonData = JSON.parse(responseBody);
pm.environment.set("id", jsonData["id"]);
postman.setNextRequest("Create Service Line Item");
Any tips or ideas on the first question regarding adding a conditional statement or loop to accomodate service entries that have > 1 service_task would be greatly appreciated.
Thanks!
Thank you very much for the kind words, I’m really glad you were able to get help from my earlier response!
I am also glad @danny-dainton’s content was helpful. He does have a lot of good stuff out there!
As for the issue you are still dealing with. How does the service_task look like? Is it a json attribute with a string value, integer value, an object, or an array (array of strings or array of integers)? I’m asking to see what the identifying criteria is for multiple service tasks. Are these service tasks coming from your csv file, or are you doing an API call to obtain them?
From what I see, it seems like you are pulling a service_task_id from the csv file. From here, I would check how many service_task_ids there are, which should then indicate if there are multiple or not.
Without the format of multiple services task ids, it’ll be difficult to provide any code that would check to see if you’re using a single or multiple service_task_ids.
Please provide that info if you can and I’ll do my best to help!
Note: I have been able to successfully create service task line items using a service_task_id field instead of using item_type and item_id as outlined in two separate fields below. (This wasn’t intentional).
Sample Request Body
https://{{base_url}}/api/v2/service_entries/{{id}}/service_entry_line_items
The {{id}} is populated from the earlier POST that creates the new service_entry
{
"type": "ServiceEntryServiceTaskLineItem",
"service_task_id": 3191105,
"description": "Instrument Panel Electrical, Service Subcategory ID = S55",
"labor_cost": $50.00,
"parts_cost": $40.00
}
Field
Description
type
string (required, string)
Used to denote what type of line item this is. Currently, the only valid value is WorkOrderServiceTaskLineItem .
item_type
string (required, editable)
Used in conjunction with item_id denotes the class name for the item association. Valid values are "ServiceTask" .
item_id
integer (required, editable)
Used in conjunction with item_id , denotes the foreign key for the item association. This is typically the ServiceTask id.
description
text (optional, editable)
Free text field for item description.
labor_cost
double (optional, editable)
Total labor cost for this line item. If there are labor line items present, the labor cost will be calculated from the sum of their totals and this value will be overwritten.
parts_cost
double (optional, editable)
Total parts cost for this line item. If there are part line items present, the labor cost will be calculated from the sum of their totals and this value will be overwritten.
–
The service_task_id is a reference to a large data set of service items that can be applied to any service_entry. This library of id’s is fixed and can be queried by w/ a GET to the API.
Currently, I’m using a csv for my data file, and have the service data organized with one service_entry per row, with multiple columns to account for any particular entry that has more than 1 service_task.
Thanks for always being so detailed, its definitely helpful.
So, if you can confirm with me, when you have a service entry with more that one service task, you create an individual column for every entry. So you have type,service_task_id,description,labor_cost,parts_cost, and then you repeat those columns for the second service task, correct? Just trying to be sure I have the proper understanding.
If my understanding is correct, what I would do is pre-format the json in the csv file, and then reference that as a variable.
So to further assist, how does your service_entry POST request look like? More specifically, where does the service_task go in the body of the POST request?
What I am thinking is you create a column called “service_tasks” and in that column, you place all the service tasks for that service entry. For example you can have one service_tasks set to:
[ {
"type": "ServiceEntryServiceTaskLineItem",
"service_task_id": 3191105,
"description": "Instrument Panel Electrical, Service Subcategory ID = S55",
"labor_cost": $50.00,
"parts_cost": $40.00
},
{
"type": "ServiceEntryServiceTaskLineItem",
"service_task_id": 3191125,
"description": "Instrument Panel Dash, Service Subcategory ID = S65",
"labor_cost": $55.00,
"parts_cost": $44.00
} ]
So that when you reference the variable {{service_tasks}} in your POST request body, you get all the tasks you need, regardless if there are multiple or not. So it should look like this:
That is assuming the above is your POST request, and that’s coming from the link you referenced. Hopefully this makes sense and that it works for you! Let me know if it does.
@odanylewycz, yes that is how I have been structuring my data csv.
Regarding the POST request that creates the service_entry I have my request body set up as a form-data type in Postman currently - I did this just to visually check all my variables and write descriptions in for things as I troubleshoot this.
From the documentation, I thought I would be able to pass the service_task(s) by ID into my request body of the first service_entry request based on the API’s documentation
however; I was disapointed to find out after testing that this outcome in the software isn’t an acceptable solution. This is for 2 main reasons:
The service_task isn’t able to be broken out into “parts” and “labor” w/ the available service_entry fields. Each service_task has it’s own values for each, regardless of how many tasks are in a single service_entry.
There is an additional service task description field available when POST is sent to an existing service_entry that is important to populate. There is a description for each service_task.
These keys are only in the line item support for service entries, which only works after a service_event already exists. I never thought about what you’re suggesting and was excited to try it.
If my understanding is correct, what I would do is pre-format the json in the csv file, and then reference that as a variable.
From your suggestion above, I’m not completely understanding what you mean by “preformating my JSON in the csv”. I tested pasting the following test JSON into the csv cell column service_task_ids
The response was was successful (201), but there isn’t any service_task data line items in the actual service_entry at all. (I verified this in the software UI as well…). Here is the full JSON response of this test:
I’m glad you tried it out! Looks like what I said worked, in the sense that it pulled the json data out of the csv cell successfully, but doesn’t seem like you got your expected response.
It looks like you did understand my suggestion correctly, and perhaps I didn’t provide enough implementation guidance.
So let me ask this. Exactly how does your request body need to look like, in order to create a proper service entry with multiple service tasks? It seems to me from what you sent (in the link), you only are doing it based on service_task_ids and not a break down of what that service task is.
So if you just want to do this via service_task_ids I would make it an array.
"service_task_ids": [
543234,
54325432
]
Thats apparently how its supposed to look, according to their documentation. So I would make a cell equal to [ 543234, 54325432]. I am including the brackets as to not mess up the csv file with the comma here to separate those service task ids.
So when you put it in request body, it should look like this:
Hi @odanylewycz, I’ve spent some time working through different versions of a solution here and unfortunately are still stuck. My tests with the service_task_ids array get a 201 from the API; however, no service task data is written into the software. It’s very strange.
I even created a single POST request w/ the key value pairs typed out in the form-data body. I included an array of actual service_task_id values = [3193541, 3191603] in the request. It was created successfully with all of my form data, but no service_task data was written to the entry.
I’m suspecting something is not accurate w/in the documentation and it’s required the service task is written only after the service_entry is created (as two separate POST calls) despite being listed as a key in the /service_entries request. I reached out to the dev team to confirm this point with a ton of examples from the work we’ve gone through here. I’m waiting to hear back from them and will update this thread w/ what they say either way.
While still thinking about this alternative, the sequence I’m envisioning would be putting a setNextRequest condition w/in a for loop that is iterating over the length of my service_task_ids array. Is this possible in your experience?
POST: Create Service Entry. Parse service_entry_id from the response
and use postman.setNextRequest(“Create Service Task Line Item”) while also creating and setting a {{variable}} for my service_task_ids array and their descriptions ( ?)
POST: Create Service Task Line Item with the first service_task_id from the array.
Repeat step #2 for the length of my service_task_ids array in my data file