Call Rest api through Script Include and display through UI Action

Satanik1
Giga Guru

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

find_real_file.png

 

Thank you,
Satanik.

1 ACCEPTED SOLUTION

Ankur Bawiskar
Tera Patron
Tera Patron

@Satanik 

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);
		}
	});
}

find_real_file.png

 

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'
});

find_real_file.png

 

Demo:

find_real_file.png

Output I got from the API for the IP You gave when I checked in browser:

find_real_file.png

Regards
Ankur

Regards,
Ankur
✨ Certified Technical Architect  ||  ✨ 9x ServiceNow MVP  ||  ✨ ServiceNow Community Leader

View solution in original post

9 REPLIES 9

Ankur Bawiskar
Tera Patron
Tera Patron

@Satanik 

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

Regards,
Ankur
✨ Certified Technical Architect  ||  ✨ 9x ServiceNow MVP  ||  ✨ ServiceNow Community Leader

Ankur Bawiskar
Tera Patron
Tera Patron

@Satanik 

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);
		}
	});
}

find_real_file.png

 

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'
});

find_real_file.png

 

Demo:

find_real_file.png

Output I got from the API for the IP You gave when I checked in browser:

find_real_file.png

Regards
Ankur

Regards,
Ankur
✨ Certified Technical Architect  ||  ✨ 9x ServiceNow MVP  ||  ✨ ServiceNow Community Leader

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);

}

Hi,

It's another way of using GlideAjax and it would just return the answer

GlideAjax

Regards
Ankur

Regards,
Ankur
✨ Certified Technical Architect  ||  ✨ 9x ServiceNow MVP  ||  ✨ ServiceNow Community Leader

BillMartin
Mega Sage

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