- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
02-21-2024 02:09 PM
I am trying to Call the following Public API to fetch the list of all countries: https://restcountries.com/v3.1/all
The API will return a JSON file I am retrieving the Name of each country from the API and then use the “Name” field of the countries to update the options of the field “Country Of Requester”.
First Step:
- "System Definition" > "Script Includes".
- Created a new Script Include named "CountryAPI".
- Added my Script
Second Step:
I created a new client script in my catalog item
On Load it calls the API and fill the select box:
On Load i am facing an error that the API is not defined although i have defined:
I would appreciate any help
Solved! Go to Solution.

- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
02-22-2024 09:39 AM
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
02-22-2024 10:53 AM
Hi @Georges_Younes,
I am assuming your REST API is returning the correct response and I advise you to look at some documentation around AJAX as suggested above.
A few things that look incorrect with your scripts:
- Script Include function needs to return a string, so add another line like the following:
countries = JSON.stringify(countries); //convert to string
return countries;
- Client Script
var ga = new GlideAjax('CountryAPI');
ga.addParam('sysparm_name','getCountries');
ga.getXML(parseResponse);
function parseResponse(response) {
var answer = response.responseXML.documentElement.getAttribute("answer");
alert("Response is " + answer);
var countries = JSON.parse(answer);
for(var i = 0; i < countries.length; i ++)
{
g_form.addOption('country_of_requester', countries[i].value, countries[i].text); //assuming 'text' key is the label
}
}
If it still doesn't work, add some logs in your script include and client script for debugging.
Cheers

- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
02-22-2024 10:58 AM
@Georges_Younes You can't directly call a script include within a client script. You need to use GlideAjax to make calls to script include. Please refer to the example here to know the syntex to call a script include via GlideAjax. https://developer.servicenow.com/dev.do#!/reference/api/utah/client/c_GlideAjaxAPI

- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
02-22-2024 09:39 AM
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
02-22-2024 10:53 AM
Hi @Georges_Younes,
I am assuming your REST API is returning the correct response and I advise you to look at some documentation around AJAX as suggested above.
A few things that look incorrect with your scripts:
- Script Include function needs to return a string, so add another line like the following:
countries = JSON.stringify(countries); //convert to string
return countries;
- Client Script
var ga = new GlideAjax('CountryAPI');
ga.addParam('sysparm_name','getCountries');
ga.getXML(parseResponse);
function parseResponse(response) {
var answer = response.responseXML.documentElement.getAttribute("answer");
alert("Response is " + answer);
var countries = JSON.parse(answer);
for(var i = 0; i < countries.length; i ++)
{
g_form.addOption('country_of_requester', countries[i].value, countries[i].text); //assuming 'text' key is the label
}
}
If it still doesn't work, add some logs in your script include and client script for debugging.
Cheers

- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
02-22-2024 10:58 AM
@Georges_Younes You can't directly call a script include within a client script. You need to use GlideAjax to make calls to script include. Please refer to the example here to know the syntex to call a script include via GlideAjax. https://developer.servicenow.com/dev.do#!/reference/api/utah/client/c_GlideAjaxAPI