# How to make a 'Call To Collection'?

**URL:** <https://community.postman.com/t/how-to-make-a-call-to-collection/39366>\
**Category:** Help Hub\
**Tags:** collections\
**Created:** [September 22, 2022, 9:16am UTC](https://community.postman.com/t/how-to-make-a-call-to-collection/39366 "2022-09-22T09:16:39Z")\
**Posts on this page:** 4\
**Page:** 1

<div class="post-metadata">

**Author:** ![niin](https://avatars.discourse-cdn.com/v4/letter/n/a87d85/32.png) [@niin](https://community.postman.com/u/niin)\
**Post date:** [September 22, 2022, 9:16am UTC](https://community.postman.com/t/how-to-make-a-call-to-collection/39366/1 "2022-09-22T09:16:39Z")

</div>

ReadyApi has the ‘Run Test Case’ step, which will run one test case from within another test case, with the option to bring back data.

I’m looking for a similar action in the Postman client, but can’t find anything, so here I am asking: **Is there a run collection function in Postman?**

A little more detail.  
On my team there are several non-technical tester who need to run some API script manually. I’ve taught them to click the 3 dots and select Run Collection.

Most of our tests require a testuser to be logged in with a session id, so every single test has to start with the same 2 requests:

POST Validate User (yields a temp token used in the next request)  
POST login info (yields a session id)

I want to put these 2 requests into their own collection  
Have the collection return the session id and save it to a global variable  
The use this var for the manually started ad-hoc tests.

I’ve been looking at the postman.setNextRequest(‘test step name’); hoping I could replace the test step name with a link to the shared collection. That doesn’t seem to be the case.

---

<div class="post-metadata">

**Author:** ![michaelderekjones](https://sea2.discourse-cdn.com/getpostman/user_avatar/community.postman.com/michaelderekjones/32/14819_2.png) [@michaelderekjones](https://community.postman.com/u/michaelderekjones)\
**Post date:** [September 23, 2022, 4:39pm UTC](https://community.postman.com/t/how-to-make-a-call-to-collection/39366/2 "2022-09-23T16:39:12Z")

</div>

If every test in a collection has to start with the same two requests, then this is what the collection pre-request script is for.

![image](https://us1.discourse-cdn.com/getpostman/original/3X/7/3/73b2edfda0543e92f404762e9e4da619c69d69e2.png)

You can send requests using the sendRequest() feature and then store the session ID from the response into a collection variable (_collection not global, as I suspect you don’t need this session ID outside of the collection_).

The following is an example of a pre-request script to authenticated to Microsoft.

The fields you need, and the token will be dependant on your API but hopefully you can see how the request works.

```auto
pm.test("Check for collectionVariables", function () {
    let vars = ['clientId', 'clientSecret', 'tenantId', 'username', 'password', 'scope'];
    vars.forEach(function (item, index, array) {
        console.log(item, index);
        pm.expect(pm.collectionVariables.get(item), item + " variable not set").to.not.be.undefined;
        pm.expect(pm.collectionVariables.get(item), item + " variable not set").to.not.be.empty; 
    });

    if (!pm.collectionVariables.get("bearerToken") || Date.now() > new Date(pm.collectionVariables.get("bearerTokenExpiresOn") * 1000)) {
        pm.sendRequest({
            url: 'https://login.microsoftonline.com/' + pm.collectionVariables.get("tenantId") + '/oauth2/v2.0/token',
            method: 'POST',
            header: 'Content-Type: application/x-www-form-urlencoded',
            body: {
                mode: 'urlencoded',
                urlencoded: [
                    { key: "client_id", value: pm.collectionVariables.get("clientId"), disabled: false },
                    { key: "scope", value: pm.collectionVariables.get("scope"), disabled: false },
                    { key: "username", value: pm.collectionVariables.get("username"), disabled: false },
                    { key: "password", value: pm.collectionVariables.get("password"), disabled: false },                    
                    { key: "client_secret", value: pm.collectionVariables.get("clientSecret"), disabled: false },
                    { key: "grant_type", value: "password", disabled: false },
                ]
            }
        }, function (err, res) {
            if (err) {
                console.log(err);
            } else {
                let resJson = res.json();
                pm.collectionVariables.set("bearerTokenExpiresOn", resJson.expires_in);
                pm.collectionVariables.set("bearerToken", resJson.id_token);
            }
        });
    }
});

```

---

<div class="post-metadata">

**Author:** ![niin](https://avatars.discourse-cdn.com/v4/letter/n/a87d85/32.png) [@niin](https://community.postman.com/u/niin)\
**Post date:** [September 26, 2022, 6:10am UTC](https://community.postman.com/t/how-to-make-a-call-to-collection/39366/3 "2022-09-26T06:10:27Z")

</div>

> [@michaelderekjones](#):
>
> sendRequest()

Appreciate the thorough reply, thanks

---

<div class="post-metadata">

**Author:** ![system](https://sea2.discourse-cdn.com/getpostman/user_avatar/community.postman.com/system/32/29796_2.png) [@system](https://community.postman.com/u/system)\
**Post date:** [August 24, 2023, 4:57pm UTC](https://community.postman.com/t/how-to-make-a-call-to-collection/39366/4 "2023-08-24T16:57:15Z")

</div>

Closed on August 24, 2023, 4:57pm UTC

This topic was automatically closed 7 days after the last reply. New replies are no longer allowed.
