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

calling a script include from a client script

oharel
Kilo Sage

Hi all,

A quick one (hopefully 😞

I have a change request form which has a "classification type" field. If the classification type is set to "standard" then a new field appears, in which the user can choose the relevant standard change request. Standard changes are on a different table (u_standard_change_requests).

I would like to make it so that if a standard change is chosen, and a standard change number is inserted in the relevant field, two other fields in the change request form are populated with the information from the standard change. The two fields are short description and description.

Standard.JPG

I created a script include:

Name: getStandardFields

var getStandardFields = Class.create();

getStandardFields.prototype = {

     

  getFields : function() {

  var standardFields = new GlideRecord('u_standard_changes');

  standardFields.addQuery('u_number', current.u_standard_change_num);

  standardFields.query();

    },

      type: 'getStandardFields'

};

I created a client script:

On change of the field "Standard Change num":

function onChange(control, oldValue, newValue, isLoading, isTemplate) {

    if (isLoading || newValue == '') {

          return;

    }

    //Type appropriate comment here, and begin script below

  var classificationType = g_form.getValue('u_change_classification');

    var ga = new GlideAjax('getStandardFields');//this is the script include

  ga.addParam("sysparm_name", "getFields"); //this is the function within the script include

  ga.getXMLWait();

  var findFields = ga.getAnswer();

  if (classificationType == 'Standard') {

  g_form.setValue('short_description', findFields.u_name_of_the_change); //This line is probably incorrect?

  }

}

u_name_of_the_change is the name of the field in the standard change, that I want to copy to short_description in the current change request form.

So my question is: how do I get to populate the change request "short_description" with values from the standard change?

Thoughts?

Harel

1 ACCEPTED SOLUTION

guhann
Mega Guru

Hi Harel,



I have modified your script include and client script. See below.



Script Include: (Ensure the client callable checkbox is checked)



var getStandardFields = Class.create();


getStandardFields.prototype = {


  getFields : function() {


  var stdChange = this.getParameter('sysparm_std_change');


  var standardFields = new GlideRecord('u_standard_changes');


  standardFields.addQuery('sys_id', stdChange);


  standardFields.query();



  if(standardFields.next()) {


  return standardFields.u_short_description + '|' + standardFields.u_description;


  }


  return '';


  },


  type: 'getStandardFields'


};




Client script:


function onChange(control, oldValue, newValue, isLoading, isTemplate) {


  if (isLoading || newValue == '') {


  return;


  }



  //Type appropriate comment here, and begin script below


  var classificationType = g_form.getValue('u_change_classification');


  if (classificationType == 'Standard') { //MAke server call only if the type is Standard


  var ga = new GlideAjax('getStandardFields');//this is the script include


  ga.addParam("sysparm_name", "getFields"); //this is the function within the script include


  ga.addParam("sysparm_std_change", g_form.getValue('u_standard_change_num'));


  ga.getXML(getResponse);


}



function getResponse(response) {


  var values = response.responseXML.documentElement.getAttribute('answer').toString().split('|');


  g_form.setValue('short_description', values[0]);


  g_form.setValue('description', values[1]);


}


}




Please let me know if any questions.


View solution in original post

22 REPLIES 22

Harel,



You just have some syntax errors. Can you paste the screenshot of both client script and script include.? not only the script section, including the name and other attributes of the script.


Kalaiarasan Pus
Giga Sage

Also, I just noticed that you have missed a important aspect in your script include and you must extend abstractajaxprocessor



'The sys_script_include code must extend the AbstractAjaxProcessor class and be client-callable.'



Refer:


GlideAjax - ServiceNow Wiki




Example :


var pushRequestedFor12 = Class.create();


pushRequestedFor12.prototype = Object.extendsObject(AbstractAjaxProcessor, {


  test : function()


  {


  return '123';


  },


  type: 'pushRequestedFor12'


});


Mr world wide
Mega Guru

Hi everyone please find my way of calling the script include from the Client script:

 

Client Script:

function onChange(control, oldValue, newValue, isLoading, isTemplate) {
	if (isLoading || newValue === '') {
		return;
	}
	
	//Current assignment group
	var assignment_group = newValue;
	
	if(assignment_group){
		var coe = '';
		var ga = new GlideAjax('sn_hr_core.PSPUtils'); //Script include
		ga.addParam('sysparm_name','getCOEbyAssignmentGroup'); //Method/Function
		ga.addParam('assignment_group',assignment_group); //Parameter
		ga.getXMLAnswer(function(response){
			coe = response;
			if(coe){
				g_form.setValue('u_hr_coe', coe);
			}
		});	
	}
}		

 

This is my script Include:

find_real_file.png

Code:

var PSPUtils = Class.create();
PSPUtils.prototype = Object.extendsObject(global.AbstractAjaxProcessor, {
	/*
		PSPUtils.prototype = {
		initialize: function() {
		},
	*/
	initialize : function(request, responseXML, gc) {
		global.AbstractAjaxProcessor.prototype.initialize.call(this, request, responseXML, gc);
	},
	/*_________________________________________________________________
		* Description: Get COE by Assignment group
		* Parameters: sys_id
		* Returns: string.
	____________________________*/
	getCOEbyAssignmentGroup: function (assignment_group) {
		var sys_id = this.getParameter('assignment_group') ? this.getParameter('assignment_group') : assignment_group;
		var result = '';
		if(sys_id){
			var grSysChoice = new GlideRecord('sys_choice');
			grSysChoice.addEncodedQuery("element=assignment_group^name=sn_hr_core_case^dependent_value="+sys_id);
			grSysChoice.orderBy('sequence');
			grSysChoice.setLimit(1);
			grSysChoice.query();
			if(grSysChoice.next()) {
				result = grSysChoice.getValue('value');
			}
		}
		return result;
	},
	
	type: 'PSPUtils'
});