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

As shown in the example client script (onChange script) you will see the getResponse function. This is a requirement when calling server side functions through Ajax. Your essentially telling service now the following. "Go out to the server, call this server side function and when you are complete return the result. The result will be returned in the "getResponse" function.

More info on Ajax here.

Give it a shot with the response function. Start with something simple like I have in the example to quickly see what the server is returning when the server side function (within the script include) is called.

This worked, thank you!

Quick question, now if i return the value of deprovision1 from script include, how do i alert it in the client script?

Could you help me with the return the statements?

 

Thanks.

The server will always return an xml object that you will need to parse. You can start with the following to figure out what xml you have and then dig into it to pull what you need.

//Place this code in the getResponse function in the client script
//getXMLAnswer will return the answer from the XML response.

var answer = response.responseXML.documentElement.getAttribute("answer");
alert(answer);

Thanks a lot!