API call through Onload of a catalog form

Rahul84
Tera Contributor

Hi Developers/ @Ankur Bawiskar 

This is related to one of the tricky requirement which I need to build.

I need to call an API on the load of a catalog form and the response I need to store in one of the catalog form field which is a drop down options field.

Please let me know how I can achieve this type of requirement.

Step by Step explanation would be helpful and also a type of sample code.

Thanks in Advance !!

1 ACCEPTED SOLUTION

@Rahul84 Have you tried this? if yes then please close the thread by marking all correct answers

Please mark the answer as correct or helpful based on impact
ServiceNow Community Rising Star, Class of 2023

View solution in original post

46 REPLIES 46

@jaheerhattiwale 

before line 5 ?

Rahul84_0-1671107929917.png

 

Hello @jaheerhattiwale ,
Need help in capturing the API response in one of the catalog form field which a drop down option field.

Below is the API response , we are getting , I need to fetch the name value (underlined with blue) and add that to drop down field as a option.

In response we can get any number of records each will have name parameter.

Please help in the below code which you have provided .

for (var i = 0; i < responseBody.length; i++) {
g_form.addOption("drop_down field name", responseBody[i].value, responseBody[i].label, i);
}

Rahul84_0-1670580360971.png

 



@Rahul84 First you need to parse the response body you received. The add for loop.

 

Do this in client script

 

var parsedResponseBody = JSON.parse(<RESPONSE FROM API HERE>);

var options = parsedResponseBody.result;

for (var i = 0; i < options.length; i++) {
g_form.addOption("drop_down field name", options[i].name, options[i].name, i);
}

 

Please mark as correct answer if this solves your issue.

Please mark the answer as correct or helpful based on impact
ServiceNow Community Rising Star, Class of 2023

Is the below line mentioned in for loop correct ? options[i].name is repeated two times

g_form.addOption("drop_down field name", options[i].name, options[i].name, i);

earlier you have suggested the below line.

g_form.addOption("<DROP DOWN FIELD NAME HERE>", responseBody[i].value, responseBody[i].label, i);

Also solution is not working , values are not coming in the field as drop down.

@Rahul84 There is no label there right so i thought choice label and value will be same

Please mark the answer as correct or helpful based on impact
ServiceNow Community Rising Star, Class of 2023