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

Your script includes doesn't look like client callable use below

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

Made that change as well. Still nothing in the logs, i guess its somehow not entering the script include it self.

Can you share screenshot of client script? Make sure onChange field on script you have is correct.

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

Hello Tim,

 

Do i have to add a return statement in the script include for the client script to alert some response?

like:

return deprovision1;

 

Thanks.