Search code examples
javascriptpostmanpostman-collection-runner

Postman set highest id from response's array as environmental variable


I have an endpoint X in my runner which returns the array containing tons of objects (each with "id" parameter). The object created as the last one has always the highest id.

In the next step of the runner I need to use some of the parameters ("id" and "taskLinkKey") from the object with the highest id, returned by endpoint X. So I'm using this formula to set them as environmental variables:

const responseJson = pm.response.json();
let arr = responseJson;
let lastTask = arr.length -1;
pm.environment.set("taskId", arr[lastTask].id);
pm.environment.set("taskLinkKey", arr[lastTask].taskLinkKey);
console.log(pm.variables.get("taskId"));
console.log(pm.variables.get("taskLinkKey"));

As you can see, the parameters from the last object in the array returned by endpoint X are set as my environmental variables. But the problem is that sometimes when endpoint X is called, the last object in the returned array is not the one with the highest id. I don't know why and I have no power over it. Could you suggest a solution that will set "id" and "taskLinkKey" parameters of the object with highest id as my environmental variables (instead of the last object in the array)?

For instance: Endpoint X returns:

[
 {
        "encodedKey": "8a19c3cb80fa96bb0180fad7c00f6170",
        "id": 2761,
        "creationDate": "2022-05-25T10:53:59+0000",
        "lastModifiedDate": "2022-05-25T10:53:59+0000",
        "dueDate": "2022-05-26T00:00:00+0000",
        "taskLinkKey": "8a19a20c80f8e1f70180fada15401a7e"
    },
    {
        "encodedKey": "8a19c46680f8e7b80180fa8af13c563c",
        "id": 2711,
        "creationDate": "2022-05-25T09:27:31+0000",
        "lastModifiedDate": "2022-05-25T09:27:32+0000",
        "dueDate": "2022-05-26T00:00:00+0000",
        "taskLinkKey": "8a19a20c80f8e1f70180fa70f1f20337"
    },
    {
        "encodedKey": "8a19d13180f8d7e20180fae137b431f5",
        "id": 2771,
        "creationDate": "2022-05-25T11:01:47+0000",
        "lastModifiedDate": "2022-05-25T11:01:47+0000",
        "dueDate": "2022-05-26T00:00:00+0000",
        "taskLinkKey": "8a19a24680f8cdfb0180fadbb8524fc3"
    },
    {
        "encodedKey": "8a19c46680f8e7b80180fa8af13c563e",
        "id": 2712,
        "creationDate": "2022-05-25T09:27:37+0000",
        "lastModifiedDate": "2022-05-25T09:27:37+0000",
        "dueDate": "2022-05-26T00:00:00+0000",
        "taskLinkKey": "8a19be1e80f21ed60180fa8b02c9678f"  
    }
]

And I want to have two environmental variables being set afterwards:

id: 2771

taskLinkKey: 8a19a24680f8cdfb0180fadbb8524fc3


Solution

  • You need to iterate over all the elements in the response.

    First time, you set the environment variables (in case the response has only one item, or the first item has the highest ID). For every following item, you check if the ID is bigger than what you have stored already. If yes, you overwrite with the new value.

    const responseJson = pm.response.json();
    let arr = responseJson;
    
    for (var i = 0;i<arr.length;i++) {
        if (!pm.environment.get("taskId")) {
            pm.environment.set("taskId", arr[i].id);
            pm.environment.set("taskLinkKey", arr[i].taskLinkKey);
        } else if (arr[i].id > pm.environment.get("taskId")) {
            pm.environment.set("taskId", arr[i].id);
            pm.environment.set("taskLinkKey", arr[i].taskLinkKey);
        }
    }