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

dmfranko
Kilo Guru

I would look this example over.   A couple of things that stand out are that the script include isn't going to know what current is.   You're going to need to pass in your change number via the client script call.   You also need to return the description via the server side call.


Community Alums
Not applicable

Just to be sure, does your Script Include have the 'Client Callable' box checked?


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.


Hi Guhan,



Thanks for the effort! I really appreciate it.


It does not work and I am not sure how to troubleshoot.


(Just to make sure we understand each other: The client script should run onChange of the "Standard Change number" field.)


Client callable checkbox is checked.



function getResponse(response) {


  g_form.showErrorBox("u_change_classification", "Hello World");


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


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


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


}


}




When I change the Standard Change num field, I see a "Hello World" message, so I know it is working, but the short description or description fields do not change.


How can I verify that the correct standard change is referenced, or that answers are taken from it?



harel