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

@Rahul84 Did you try that?

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

yes @jaheerhattiwale I have tried and solution is working fine. Thanks a lot for the help.

Just one issue. I cannot use the hard coded script to set the rest message things in the script include.
I need to call rest message from SI. But one challenge here is how to set query parameter for the current logged in user. "gs.getUser().getName()".
Any way to set this in REST Message as query parameter instead of using script in SI?

 

Rahul84_1-1670851704111.png

Rahul84_2-1670851779420.png

 

 

 

@Rahul84 Don't add the HTTP Query Parameter on Rest message as you added in the image you posted. Remove it from there as you have added below line in script include.

 

sm.setQueryParameter("userid",gs.getUser().getName())

 

This line takes care of that.

 

Can you please mark all the answers in this question as correct answer.

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

@jaheerhattiwale : can we use gs.getUser().getName() directly in rest message instead of using this in script?

Instead of the script like sethttpmethod,setendpoint , setqueryparamter.
We are planning to call the rest message directly inside sn_ws.RESTMessageV2();

@Rahul84 No. You have to use one or the other function in script include.

 

You can write the script directly in the rest message query param.

If you add variable there like ${userid} and generate auto variables the you must use setStringParameter() function instead of setQueryParameter() function.

 

So its ok to use setQueryParameter() function

 

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