Hi guys,
I’m trying to use the below code in Test
=====================================================
async function getExternalAccountId(account_id, i) {
//Get the external_account_Id
await pm.sendRequest({
url: pm.environment.get("baseUrl") + '/restapi/v2.1/accounts/' + account_id,
method: 'GET',
header: {
"User-Agent": "PostmanRuntime",
"Connection": "keep-alive",
"Authorization": "Bearer " + pm.environment.get("accessToken")
},
}, async function (err, res) {
if (err) {
console.log(err);
} else {
var jsonAccountData = res.json();
var s = (i == 0) ? '' : ('_' + i);
await postman.setEnvironmentVariable("externalAccountId" + s, jsonAccountData.externalAccountId);
await console.log("External Account i = ",i);
}
}
);
}
async function processResponseBody(responseBody) {
var jsonData = JSON.parse(responseBody);
var accounts = jsonData.accounts;
if (jsonData.hasOwnProperty("sub")) {
await postman.setEnvironmentVariable("userId", jsonData.sub);
}
for (var i = 0; i < accounts.length; i++) {
if (accounts[i].is_default === true) {
await postman.setEnvironmentVariable("accountId", accounts[i].account_id);
await postman.setEnvironmentVariable("externalAccountId", '');
//Get the external_account_Id
await getExternalAccountId(accounts[i].account_id, 0);
await postman.setEnvironmentVariable("accountId" + '_' + (i + 1), postman.getEnvironmentVariable('accountId'));
// The below line is for Question 2!!!
await postman.setEnvironmentVariable("externalAccountId" + '_' + (i + 1), postman.getEnvironmentVariable('externalAccountId'));
await console.log("in Default main i = ", i);
} else {
await postman.setEnvironmentVariable("accountId" + '_' + (i + 1), accounts[i].account_id);
await postman.setEnvironmentVariable("externalAccountId" + '_' + (i + 1), '');
await getExternalAccountId(accounts[i].account_id, i + 1);
await console.log("main i = ", i);
}
}
}
// Main function
postman.clearEnvironmentVariable("userId");
postman.clearEnvironmentVariable("accountId");
postman.clearEnvironmentVariable("externalAccountId");
var i = 1;
while (pm.environment.has('accountId' + '_' + i)) {
postman.clearEnvironmentVariable("accountId" + '_' + i);
postman.clearEnvironmentVariable("externalAccountId" + '_' + i);
i++
};
processResponseBody(responseBody);
=====================================================
The Console result is
GET https://account-d.docusign.com/oauth/userinfo
in Default main i = 0
main i = 1
main i = 2
GET https://demo.docusign.net/restapi/v2.1/accounts/XXXXXX
External Account i = 3
GET https://demo.docusign.net/restapi/v2.1/accounts/XXXXXX
External Account i = 2
GET https://demo.docusign.net/restapi/v2.1/accounts/XXXXXX
External Account i = 0
Can you help me with 2 questions:
- Why await doesn’t help with the call orders?
- Why if I try to get the Environmental variable (see line above that commented for Question 2) it returned an empty value:
await postman.setEnvironmentVariable(“externalAccountId” + ‘_’ + (i + 1), postman.getEnvironmentVariable(‘externalAccountId’));
As a workaround I have to make the call getExternalAccountId again instead of the above line
await getExternalAccountId(accounts[i].account_id, i + 1);
Thanks