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
Mega Sage
Mega Sage

@Rahul84 Create a onload client script and client callable script include as below

 

Client script code:

function onLoad() {
var fetchData = new GlideAjax('AjaxUtil'); //script include name
fetchData.addParam('sysparm_name', 'fetchData'); // Name is the function in the script include that we're calling
fetchData.getXML(triggerCallBack);

function triggerCallBack(response) {
var answer = response.responseXML.documentElement.getAttribute("answer");
if(answer){
//Assuming response is in an array like below
/*[{"label":"choice 1", "value":"1"}, {"label":"choice 1", "value":"1"}]*/

var responseBody = JSON.parse(answer);

for(var i=0; i<responseBody.length; i++){
g_form.addOption("<DROP DOWN FIELD NAME HERE>", responseBody[i].value, responseBody[i].label, i);
}
}
}
}

 

Script include:

jaheerhattiwale_0-1670483430692.png

 

Script include code:

var AjaxUtil = Class.create();
AjaxUtil.prototype = Object.extendsObject(AbstractAjaxProcessor, {

fetchData: function(){
var sm = new sn_ws.RESTMessageV2();
sm.setEndpoint("<END POINT HERE>");
sm.setHttpMethod("<HTTP METHOD HERE>");
sm.setBasicAuth("<USER NAME>", "<PASSWORD>");
var response = sm.execute();
var responseBody = response.getBody();
var httpStatus = response.getStatusCode();

if(httpStatus == 200){
return responseBody;
}

return null;
},

type: 'AjaxUtil'
});

 

 

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

Thanks a lot @jaheerhattiwale . I will try the above solution and let you know the result.

Hi @jaheerhattiwale ,

I need some addition to the above code in script include.

I have three params in my API URL, out of these three two are fixed params and one is dynamic(username).

One param is the "username" and which will be dynamic. It will be based on the user which is loading the catalog form.

Can you pls help to modify the above code to add params and one should have the dynamic one based on the current user which is loading/opening the catalog form.

Thanks in advance !

 

@Rahul84 In script include you can use below line to get the logged in user's user name.

 

gs.getUser().getName()
 
Use the to add in API URL param
 
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

Hello @jaheerhattiwale 

Can you please let me know , will this work? the below code ?

 

Rahul84_0-1670571534306.png

 

In postman ,we have params like this 

Rahul84_1-1670571616516.png