- 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-12-2022 06:29 AM - edited 12-12-2022 06:29 AM
okay Thanks @jaheerhattiwale .
Just have one more query & help required.
As of now , we are using gs.getUser().getName() for current logged in user who is loading the form and getting the response in the catalog field.
But we have one more field on catalog for as "Requested For". That means current logged in user can place a catalog request for others as well.
So if current logged in user , change the "Requested For" to some other user name , then we need to capture the API response for the "Requested For" user.
For the same I m planning to create one Onchange catalog client script with same logic that you had suggested but have doubt how to pass "Requested For" user id as input in API url in Script include. Some modification required in this line.
sm.setStringParameter("userid",gs.getUser().getName());
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
12-12-2022 06:41 AM
@Rahul84 then write a onchange client script and add following code:
Client script code:
function onChange(control, oldValue, newValue, isLoading, isTemplate) {
if (isLoading || newValue === '') {
return;
}
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.addParam('sysparm_user', g_form.getValue("<REQUESTED FOR FIELD NAME HERE>"); 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);
}
}
}
}
Modify script include code as below to handle both onload and onchange client script
Script include code:
var AjaxUtil = Class.create();
AjaxUtil.prototype = Object.extendsObject(AbstractAjaxProcessor, {
fetchData: function(){
var user = this.getParameter('sysparm_user');
var sm = new sn_ws.RESTMessageV2();
sm.setEndpoint("<END POINT HERE>");
sm.setHttpMethod("<HTTP METHOD HERE>");
sm.setBasicAuth("<USER NAME>", "<PASSWORD>");
if(user){
sm.setQueryParameter("userid", this.getUserId(user));
}else{
sm.setQueryParameter("userid", gs.getUser().getName());
}
var response = sm.execute();
var responseBody = response.getBody();
var httpStatus = response.getStatusCode();
if(httpStatus == 200){
return responseBody;
}
return null;
},
getUserId: function(user){
var userGr = new GlideRecord("sys_user");
if(userGr.get(user)){
return userGr.user_name.toString();
}
return null;
},
type: 'AjaxUtil'
});
Please mark all the answers in this question as correct answer.
ServiceNow Community Rising Star, Class of 2023