- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
12-07-2022 04:59 AM
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 !!
Solved! Go to Solution.
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
12-16-2022 02:26 AM
@Rahul84 Have you tried this? if yes then please close the thread by marking all correct answers
ServiceNow Community Rising Star, Class of 2023
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
12-07-2022 11:11 PM
@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:
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.
ServiceNow Community Rising Star, Class of 2023
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
12-08-2022 01:39 AM
Thanks a lot @jaheerhattiwale . I will try the above solution and let you know the result.
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
12-08-2022 10:55 PM
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 !
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
12-08-2022 11:09 PM
@Rahul84 In script include you can use below line to get the logged in user's user name.
ServiceNow Community Rising Star, Class of 2023
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
12-08-2022 11:41 PM
Hello @jaheerhattiwale
Can you please let me know , will this work? the below code ?
In postman ,we have params like this