- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
â05-25-2021 06:15 AM
Hello Experts,
I am trying to do my hands on in Rest api integration in my PDI, using script include. Can anybody please confirm whether the script is ok? After this I am trying to call it via an UI action. Both the details are given below. Ideally on the button click, it should popup an alert and make two log entries. But nothing is happening.
Any help is much appreciated.
Script include:
var GetIPAddress = Class.create();
GetIPAddress.prototype = {
initialize: function() {
},
getLocation: function(){
var request, response, status, address;
gs.log("GETIPAddress");
address = '162.55.44.120';
request = new sn_ws.RESTMessageV2();
request.setEndpoint('https://api.ip2country.info/ip?'+address);
request.setRequestHeader("Accept","application/json");
request.setRequestHeader('Content-Type','application/json');
response = request.execute();
var JSONData = new global.JSON().decode(response);
gs.print('Country Code: '+ JSONData.countryCode + ' Country Name: '+JSONData.countryName);
gs.log(response.getBody());
return JSONData.countryCode;
//response = getresponse.getBody();
},
type: 'GetIPAddress'
};
UI Action
var confirmTemp = new GlideAjax('GetIPAddress');
confirmTemp.addParam('sysparm_name', 'getLocation');
confirmTemp.getXML(_handleResponse);
function getResponse(response) { //This function does not appear to be executing
var answer = response.responseXML.documentElement.getAttribute("answer");
answer = answer.toString();
alert(answer);
}
UI Action Screenshot
Thank you,
Satanik.
Solved! Go to Solution.
- Labels:
-
Service Desk
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
â05-25-2021 08:32 AM
This is the working code; in the alert for the answer it should give the country code and country name
UI Action:
Onclick - callAPI();
Client checkbox - true
Script:
function callAPI(){
var ga = new GlideAjax('GetIPAddress');
ga.addParam('sysparm_name', "getLocation");
ga.getXMLAnswer(function(answer){
if(answer != ''){
alert(answer);
}
});
}
Script Include: It should be client callable
var GetIPAddress = Class.create();
GetIPAddress.prototype = Object.extendsObject(AbstractAjaxProcessor, {
getLocation: function(){
gs.info('GETIPAddress');
var request, response, status, address;
address = '162.55.44.120';
request = new sn_ws.RESTMessageV2();
request.setEndpoint('https://api.ip2country.info/ip?' + address);
request.setHttpMethod('GET');
request.setRequestHeader("Accept","application/json");
request.setRequestHeader('Content-Type','application/json');
response = request.execute();
gs.info('Response body' + response.getBody());
var JSONData = new global.JSON().decode(response.getBody());
gs.info('Country Code: '+ JSONData.countryCode + ' Country Name: '+JSONData.countryName);
return JSONData.countryCode + ' , ' + JSONData.countryName;
},
type: 'GetIPAddress'
});
Demo:
Output I got from the API for the IP You gave when I checked in browser:
Regards
Ankur
Ankur
⨠Certified Technical Architect || ⨠9x ServiceNow MVP || ⨠ServiceNow Community Leader
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
â05-25-2021 07:25 AM
In your UI Action code use this to call script include
var confirmTemp = new GetIPAddress().getLocation(); //new ScripIncludename().methodname()
alert(confirmTemp);
Mark it helpful if this helps you to understand. Accept solution if this give you the answer you're looking for
Kind Regards,
Rohila V
2022-25 ServiceNow Community MVP
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
â05-25-2021 07:46 AM
Hi Rohila,
My Script Include itself is not working. Any idea by looking into it? I have updated it to the below code.
The actual api: https://api.ip2country.info/ip?162.55.44.120
var GetIPAddress = Class.create();
GetIPAddress.prototype = {
initialize: function() {
},
getLocation: function(){
gs.addInfoMessage('GETIPAddress');
var request, response, status, address;
//gs.log("GETIPAddress");
address = '162.55.44.120';
request = new sn_ws.RESTMessageV2();
request.setEndpoint('https://api.ip2country.info/');
request.setHttpMethod('get');
request.setRequestHeader("Accept","application/json");
request.setRequestHeader('Content-Type','application/json');
//request.setStringParameterNoEscape('address',address);
request.setQueryParameter('ip',address);
response = request.execute();
var JSONData = new global.JSON().decode(response);
gs.addInfoMessage('Country Code: '+ JSONData.countryCode + ' Country Name: '+JSONData.countryName);
//gs.log(response.getBody());
return JSONData.countryCode;
//response = getresponse.getBody();
},
type: 'GetIPAddress'
};
Thanks,
Satanik.

- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
â05-25-2021 08:03 AM
Hi
1. Did you first tried this in REST Message or REST API Explorer?
2. Are you getting any HTTP error?
Also change your UI Action script to this
var confirmTemp = new GlideAjax('GetIPAddress');
confirmTemp.addParam('sysparm_name', 'getLocation');
confirmTemp.getXML(getResponse);
function getResponse(response) { //This function does not appear to be executing
var answer = response.responseXML.documentElement.getAttribute("answer");
answer = answer.toString();
alert(answer);
}
Thank you
Prasad
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
â05-25-2021 08:13 AM
Below code is syntax
Modify accordingly.
var r = new sn_ws.RESTMessageV2('RestMessage Name', 'method'); //modify accordingly
var response = r.execute();
var responseBody = response.getBody();
var httpStatus = response.getStatusCode();
var resp = JSON.parse(responseBody);
return resp.countryCode;
Mark it helpful if this helps you to understand. Accept solution if this give you the answer you're looking for
Kind Regards,
Rohila V
2022-25 ServiceNow Community MVP