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 : Yes I have tried this and logic is working but there are some issues. 

Found one issue/scenario.

[] Suppose I have loaded the catalog form with my name ( logged in with my id). So it will give me the response from the API for my id and we are storing the responses in one of the field. This is working fine.

But If I have loaded the form with my id and changing the "requested for" to some other name then , the drop down options are getting added. In the drop down field ( options available for the user which is loading the form+ Requested for user ( if changing to some other user name)).

[] Also currently the script is getting executed two times in backend. In logs the same things are getting printed two times at the same time with a difference if 2/3 secs.

Rahul84_0-1671105928080.png

 


Desired scenario :

If I am a loading the catalog form with my id , then it will show the API response for my id and same will be stored in drop down field.

But if I am changing the value of "Requested for" field , then the drop down field should only show the API response of the userid which is selected in "Requested for" field. It should clear out the choices for the previous user. Currently it is adding the choices.

@Rahul84 In both the client script add below line before glideajax call.

 

g_form.clearOptions("<DROP DOWN FIELD NAME HERE>");

 

Please mark all answers on this question as correct answers. 

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

before line number 5? or suggest the exact line

Rahul84_0-1671107330555.png

 

@Rahul84 Before line no. 5

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

Thanks a lot @jaheerhattiwale . Solution is working fine.

can you help with the below solution?

how we can set the endpoint and basic auth credentials based on the environment.

As of now I have the added the endpoint and credentials for non-prod in the script but for production movement how to change the endpoint and credential for prod ? and we need to have these non-prod endpoint and credentials in the script becoz need to test the same changes on test instance as well.

Rahul84_0-1671110424158.png

Thanks !