The CreatorCon Call for Content is officially open! Get started here.

How to call script include from UI Page client script section ?

fisfreak
Kilo Contributor

Hello,

I need to make a call to script include from UI Page, when i try to invoke a function from client script of UI Page, it is not returning me anything, where as same thing works from catalog client script. please find the code snippet below.

$(document).ready(function() {
	var ga = new GlideAjax("ControlVirtualMachinesAjax");
	ga.addParam("sysparm_name", "getVMSInRG");
	ga.getXML(ajaxResponse);
	
	function ajaxResponse(serverResponse) {
		alert("ServerResponse: " + serverResponse);
		
		}	

});

						

Same code works perfect from catalog client script but not from client script of UI Page. In the above code , control never comes inside ajaxResponse method.

Thanks,

Krishna P

 

20 REPLIES 20

Strange, Can you please remove the external libraries you have added in UI page and try it, This is straight & simple and ought to work.

Something is restricting/messing up with Glideajax libraries.

 

I removed all external libraries and tired, still not working.

find_real_file.png

HI, 

Can you share the script inside script include, Just try with a single return statement.

Also, check logs for any error seen while a call is being made to the server.

 

Below link will also help to debug the issue.

https://community.servicenow.com/community?id=community_question&sys_id=782c4365db9cdbc01dcaf3231f96191c

 

Thanks

Gaurav

Hi Gaurav,

Sorry for the delay, here is my script include code.

var ControlVirtualMachinesAjax = Class.create();
ControlVirtualMachinesAjax.prototype = Object.extendsObject(global.AbstractAjaxProcessor,
{
	_subscriptionId: "XXXXXXXXXXXXXXXXXXXXXXXXX",
	_accessToken: null,

	getVMSInRG : function(){
		// Make sure we are connected and have access token
		this._ensureAccessToken();
		var rgName = this._getResourceGroupFromSysId(gs.getUserID());
		gs.info("rgName: " + rgName);
		var response = this._getVmList(this._accessToken, this._subscriptionId, rgName);
		gs.info("After _getVmList, status code="+response.getStatusCode() +"response body="+response.getBody());
		if(response) {
			var output = new global.JSON().decode(response.getBody())["value"];
			var size = output.length;
			for(var i=0; i<size; i++){
				var vm = output[i];
				
				var vmName = vm["name"].toString();
				var privateIPAddress = this._getPrivateIPAddress(this._accessToken, this._subscriptionId, rgName, vmName);

				gs.info("name of the vm " + i + vmName);
				var vmObject = this._getVmInfo(this._accessToken, this._subscriptionId, rgName, vmName);
				gs.info("vmObject"+ i + new global.JSON().decode(vmObject.getBody()).properties.instanceView.statuses[1].displayStatus);
				var vmNS = this.newItem("vmNS");
				vmNS.setAttribute("status",new global.JSON().decode(vmObject.getBody()).properties.instanceView.statuses[1].displayStatus);
				vmNS.setAttribute("ipAddress", privateIPAddress);
			}
			
			var vmList = new global.JSON().encode(response.getBody());
			gs.info("getVMSInRG" + response.getBody());
						
			var rg = this.newItem("rg");
			rg.setAttribute("name", rgName);
			return vmList;
		}
		return null;
	},
		
	type:"ControlVirtualMachinesAjax"
		
});

catalog client script works perfect with the above code, and also i have added following in UI Page

<script language="javascript" src="service.sn-glideAjax.jsdbx"/>

but in the console i am getting below error after adding above line in UI Page

find_real_file.png

HI,

I am not sure how this will impact but can you try adding the service.sn_glideAjax.jsdbx to your UI page.(create a UI script first)

 https://developer.servicenow.com/app.do#!/training/article/app_store_learn_advanced_ui_helsinki_c_ModuleAngularJS/app_store_learn_advanced_ui_helsinki_t_ExerciseCreateAjaxAdapter?v=helsinki

find_real_file.png

The error suggests that that the page couldn't load the Glideajax scripts.

find_real_file.png

https://developer.servicenow.com/app.do#!/training/article/app_store_learn_advanced_ui_helsinki_c_ModuleAngularJS/app_store_learn_advanced_ui_helsinki_t_ExerciseUpdateUIPageForGlideAjax?v=helsinki