VMware Cloud Community
harj123
Enthusiast
Enthusiast
Jump to solution

get catalog resource using the catalogitemrequest.id

I am trying to do a test if my request created a catalogresource by comparing `CatalogItemRequest.id` and `catalogResource.requestId`. please see the code

var catalogResources = vCACCAFEEntitiesFinder.getCatalogResources(cafeHost);

for each (var resource in catalogResources) {

    System.log("resource name: " + resource.name + "resource request id: " + resource.requestId + "request id: " + request.id)

    if (resource.requestId == request.id) {

        provisionedDeployment = resource.name;

        System.log(provisionedDeployment)

    } else {throw "Error: the deployment not found"};

}

I get the output of

System.log("resource name: " + resource.name + "resource request id: " + resource.requestId + "request id: " + request.id)

like below

resource name: win2012r2-OUtestresource request id: 34ddfaab-f133-4985-aa32-eb7eacb90ff0 request id: 34ddfaab-f133-4985-aa32-eb7eacb90ff0

and it shows that the CatalogItemRequest.id is equal to catalogResource.requestId` but the check in the `if` statement fails and else statement is executed. any help is much appreciated

0 Kudos
1 Solution

Accepted Solutions
iiliev
VMware Employee
VMware Employee
Jump to solution

Hi,,

If the sample output you provided is a verbatim copy of the output you see in the console, then it seems there is an extra whitespace character between the resource.requestId value and "request id: " label. Having an extra non-visible whitespace character in one of the values would explain why the comparison is failing.

So could you replace the code on line 05

if (resource.requestId == request.id) {

with the following

if (resource.requestId.trim() == request.id.trim()) {

and try again?

Also, the current logic of the for loop seems incorrect. If you have more than one element in catalogResources variable, and assuming only one of these elements should match the request ID, then the code will throw in the 'else' clause for the first element that is not matching the request ID. To fix this, you need to move the 'throw' statement out of the for loop, and throw only if no match is found during the all loop iterations.

View solution in original post

0 Kudos
1 Reply
iiliev
VMware Employee
VMware Employee
Jump to solution

Hi,,

If the sample output you provided is a verbatim copy of the output you see in the console, then it seems there is an extra whitespace character between the resource.requestId value and "request id: " label. Having an extra non-visible whitespace character in one of the values would explain why the comparison is failing.

So could you replace the code on line 05

if (resource.requestId == request.id) {

with the following

if (resource.requestId.trim() == request.id.trim()) {

and try again?

Also, the current logic of the for loop seems incorrect. If you have more than one element in catalogResources variable, and assuming only one of these elements should match the request ID, then the code will throw in the 'else' clause for the first element that is not matching the request ID. To fix this, you need to move the 'throw' statement out of the for loop, and throw only if no match is found during the all loop iterations.

0 Kudos