- 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 08:15 AM
Please test this API from Postman i.e. some external tool first and check are you getting valid response
For your debugging you can add info statements for response body, http status etc
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 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 09:02 AM
Hi Ankur,
Can't thank you more. This has worked like a charm. Got my idea cleared except one doubt
Why are we using
ga.getXMLAnswer(function(answer){
if(answer != ''){
alert(answer);
}
instead of-
function getResponse(response) {
var answer = response.responseXML.documentElement.getAttribute("answer");
answer = answer.toString();
alert (answer);
}
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
ā05-25-2021 09:06 PM
Hi,
It's another way of using GlideAjax and it would just return the answer
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
ā03-16-2025 02:30 AM
Sharing this tutorial to complement the article that demonstrates good coding practices like object oriented programming, service oriented architecture, global logging techniques, SOLID design patterns. Implementing a fully reliable, scalable and reusable code.
How to Call Third Party REST API Integrations in ServiceNow Using GlideAjax & Script Includes
Dynamic Script Include and GlideAjax in ServiceNow: Scalable & Reusable Code for Architects