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

Voona Rohila
Kilo Patron
Kilo Patron

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

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.

Hi @Satanik 

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

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