Join the #BuildWithBuildAgent Challenge! Get recognized, earn exclusive swag, and inspire the ServiceNow Community with what you can build using Build Agent.  Join the Challenge.

calling script include from on change client script

Nitesh Balusu
Giga Guru

Hello Developers,

 

I am trying to call a script include from an onchange client script and print some logs and some how it doesn't seem to be working. Could someone please point out the mistake in this?

 

//SCRIPT INCLUDE

var deprovision_employee = Class.create();
deprovision_employee.prototype = {
    getFields: function() {
	
	var deprovision1 = this.getParameter('deprovision_request');
	var ritm= new GlideRecord('sc_req_item');
	ritm.addEncodedQuery('short_descriptionSTARTSWITHDe-Provision an Employee or Contractor '+deprovision1);
	ritm.query();
		gs.info("deprovision is "+deprovision1);

		if(ritm.next()){
			
	gs.info("deprovision is "+deprovision1);
		
		}
    },

    type: 'deprovision_employee'
};


//CLIENT SCRIPT

function onChange(control, oldValue, newValue, isLoading) {
	if (newValue == '') {
		g_form.setReadOnly('current_device',true);
		return;
	}
	var deprovision=g_form.getDisplayBox('user_to_deprovision').value;
	
	var ga = new GlideAjax('deprovision_employee');

	ga.addParam("sysparm_name", "getFields"); 

	ga.addParam("deprovision_request", deprovision);

	//ga.getXML(getResponse);
}
1 ACCEPTED SOLUTION

tim_snow
Giga Contributor

3 things need to be done to get this to work for you. Give it a shot using the example code. Let us know if it works out for you.

1. Mark the script include as "client callable"

2. Uncomment the ga.getXML(getResponse); line in the client script

3. Add the getResponse function to the client script to be called when the ajax request returns client side. 

 

Example Script Include:

var deprovision_employee = Class.create();
deprovision_employee.prototype = Object.extendsObject(AbstractAjaxProcessor, {
    getFields: function() {
	
	var deprovision1 = this.getParameter('deprovision_request');
	var ritm= new GlideRecord('sc_req_item');
	ritm.addEncodedQuery('short_descriptionSTARTSWITHDe-Provision an Employee or Contractor '+deprovision1);
	ritm.query();
		gs.info("deprovision is "+deprovision1);

		if(ritm.next()){
			
	gs.info("deprovision is "+deprovision1);
		
		}
    },
    type: 'deprovision_employee'
});

 

Example Client Script

function onChange(control, oldValue, newValue, isLoading, isTemplate) {
   if (isLoading || newValue === '') {
      return;
   }
	var deprovision=g_form.getValue("host_name");
	var ga = new GlideAjax('deprovision_employee');
	ga.addParam("sysparm_name", "getFields"); 
	ga.addParam("deprovision_request", deprovision);
	ga.getXML(getResponse);
}

function getResponse(response) {
	alert(response);
}

View solution in original post

13 REPLIES 13

athm
Tera Guru

Hi Sree,

I see your script include class is not extending AbstractAjaxProcessor, this could be if the script include is created/saved and then making the client callable checkbox as true,

A client callable script include should have the below template.

var deprovision_employee = Class.create();
deprovision_employee.prototype = Object.extendsObject(AbstractAjaxProcessor, {

type: 'deprovision_employee'
});

 

 

Could you retry your code using the above template.

Hope this helps you Sree.

 

Hi Athm,

 

I tried this but i still don't see anything in the logs. Am i missing anything else please?

 

Thanks.

Mike Patel
Tera Sage

make sure client callable is check on script include and change 

var deprovision=g_form.getDisplayBox('user_to_deprovision').value;

to 

var deprovision=g_form.getValue('user_to_deprovision');

Hello Mike,

 

I actually need this statement.

var deprovision=g_form.getDisplayBox('user_to_deprovision').value;

user_to_deprovision is a reference field and i cannot use getDisplayValue in client script. So need to use getDisplaybox to get its display value. Will it not work without it?

Also the client script is a catalog client script.

 

Thanks.