Join the #BuildWithBuildAgent Challenge! Get recognized, earn exclusive swag, and inspire the ServiceNow Community with what you can build using Build Agent.  Join the Challenge.

Make select box field (Country) in the catalog item filled by Real time API, Error API script

Georges_Younes
Tera Contributor

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

Screenshot (45).png

Second Step:

I created a new client script in my catalog item

On Load it calls the API and fill the select box:

 

Screenshot (46).png

On Load i am facing an error that the API is not defined although i have defined:

Screenshot (49).png

I would appreciate any help

 

 

3 ACCEPTED SOLUTIONS

James Chun
Kilo Patron

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

View solution in original post

Sandeep Rajput
Tera Patron
Tera Patron

@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

View solution in original post

3 REPLIES 3

pablo_itguy_pl
Mega Guru

Please have a look at GlideAjax

James Chun
Kilo Patron

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

Sandeep Rajput
Tera Patron
Tera Patron

@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