The simple Script Include / Client Script - check if the table has records

MysUser
Tera Contributor

Hello,

I have a client script that trigger the script include - this should give the answer "true" whet the table "u_proc" has at least one record. Something is wrong and I get the response "null" (the response should be "true", because the table has records). Do you know what is wrong?

Client Script:

function onChange(control, oldValue, newValue, isLoading) {
   if (isLoading || newValue == '') {
      return;
   }

var ga = new GlideAjax('test'); //This argument should be the exact name of the script include. 
ga.addParam('sysparm_name', 'testfunction'); //sysparm_name is the name of the function in the script include to call.
ga.addParam('sysparm_my_name', 'aaa'); //This is an optional parameter which we can reference in the script include. 
//ga.getXML(myCallBack); //This is our callback function, which will process the response.
	

function myCallBack(response) { //the argument 'response' is automatically provided when the callback funciton is called by the system.
    //Dig out the 'answer' attribute, which is what our function returns. 
	
    var greeting = response.responseXML.documentElement.getAttribute('answer'); 
	
    alert(greeting); //alert the result to make sure all is well.

}
}

Script Include:

var test = Class.create();
test.prototype = {
    initialize: function() {
		
		
    },
	
	testfunction: function(){
		var userName = this.getParameter('sysparm_my_name');
		var target = new GlideRecord('u_proc'); 
		//target.addQuery('u_user', userName);
		target.query(); // Issue the query to the database to get relevant records 
		while (target.next()) { 
		
			return 'true';
	}
		
	},

    type: 'test'
};
6 REPLIES 6

Anurag Tripathi
Mega Patron
Mega Patron

In your script include comment out the initialize function and use the below

testfunction: function(){

var userName = this.getParameter('sysparm_my_name');

var target = new GlideRecord('u_proc');

target.addQuery('u_user', userName); // make sure u_user is not a reference field, if it is reference then it wont match to aaa but a sys_id

target.query();

if(target.next()){  //use if instead of while

return 'true';

}

},

 

 

In your Client script why is //ga.getXML(myCallBack); commented?? uncomment this.

 

 

-Anurag

Deepak Ingale1
Mega Sage

You have commented out the callBack function call, please uncomment it 

 

//ga.getXML(myCallBack); //This is our callback function, which will process the response.

 

Note: Please mark reply as correct / helpful if it answers your question

Also make sure that your SCRIPT include is client callable and extended from AbstractAjaxProcessor script include

 

https://docs.servicenow.com/bundle/geneva-servicenow-platform/page/script/server_scripting/reference...

 

Below is sample script inclide

AJAX processor script include
/*
 * MyFavoritesAjax script include Description - sample AJAX processor returning multiple value pairs
 */ 
var MyFavoritesAjax = Class.create();
MyFavoritesAjax.prototype = Object.extendsObject(AbstractAjaxProcessor, {
 
	 /*
	 * method available to client scripts call using:
	 * var gajax = new GlideAjax("MyFavoritesAjax");
	 * gajax.addParam("sysparm_name","getFavorites");
	 */
	getFavorites: function() { // build new response xml element for result
         var result = this.newItem("result");
	  result.setAttribute("message","returning all favorites");
 
	  //add some favorite nodes with name and value attributes 
         this._addFavorite("color","blue");
         this._addFavorite("beer","lager");
         this._addFavorite("pet","dog");
       },
	 // all items are returned to the client through the inherited methods of AbstractAjaxProcessor
	 _addFavorite: function(name, value) { 
          var favs = this.newItem("favorite");
	   favs.setAttribute("name",name);
	   favs.setAttribute("value",value); },
 
	type:"MyFavoritesAjax"
 
 });

siva_
Giga Guru

Hello MysUser ,

 

When you say that your client script should trigger script include then your script include must be client callable which means your script include should extend AbstractAjaxProcessor class only then your script include will be called else it will not even be called and in your script please make these highlighted changes and let me know if that worked 

function onChange(control, oldValue, newValue, isLoading) { if (isLoading || newValue == '') { return; } var ga = new GlideAjax('test'); //This argument should be the exact name of the script include. ga.addParam('sysparm_name', 'testfunction'); //sysparm_name is the name of the function in the script include to call. ga.addParam('sysparm_my_name', 'aaa'); //This is an optional parameter which we can reference in the script include. ga.getXML(myCallBack); -- > uncomment  //This is our callback function, which will process the response. function myCallBack(response) { //the argument 'response' is automatically provided when the callback funciton is called by the system. //Dig out the 'answer' attribute, which is what our function returns. var greeting = response.responseXML.documentElement.getAttribute('answer'); alert(greeting); //alert the result to make sure all is well. } }

testfunction: function(){ var userName = this.getParameter('sysparm_my_name'); var target = new GlideRecord('u_proc'); //target.addQuery('u_user', userName); target.query(); // Issue the query to the database to get relevant records if(target.hasNext()) { return 'true'; } },

 

Hope this helps 

MARK THIS RESPONSE AS CORRECT IF IT REALLY HELPS

Thanks,

Siva