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

You can be your best help .. Put few logging statements in the script provided by Guhan and see if it is getting called ...


Harel,



Put some log statements in script include function. Ensure you have specified the exact table name where the standard changes are store.



And in client script, inside the getResponse function put an alert for the response. see below.



function getResponse(response) {  


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


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


  alert('Short Desc: '+values[0]);


  alert('Desc: '+values[1]);


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


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


}




If you still face issues, please paste the code you are trying in script include and client script. Also mention the exact table name and field names.


Daniel Draes
ServiceNow Employee
ServiceNow Employee

Check the example in Dan's comment. You will need to use a GlideAjax call for this. Everyting else is doomed to fail or a performance desaster.



In a nutshell: your Script Include will need to extend class AbstractAjaxProcessor, and your ClientScript will need to make use of class GlideAjax to call the script include.


Harel,



As Kalai pointed,   extend the AbstractAjaxProcessor class . my bad   I just modified ur function rather didn't notice it's missing.


Hi Guhan (and all ),



I went over the link Dan said, as well as over the rest of the comments. I have a very basic understand of what to do, and therefore miss some of the actual logic and/or syntax to do it - hence the request for help


I think I am calling the GlideAjax, though.


So:


Table name: change_request


Fields:


  • u_change_classification (should be set to "Standard" for this to work)
  • u_standard_change_num (should be filled with a standard change number)
  • short_description
  • description


Table name: u_standard_changes


u_name_of_the_change (this will be copied to short_description in change_request)


u_description (this will be copied to description in change_request)




Client script in change_request:


Type: onChange


Field Name: Standard Change num (u_standard_change_num)


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


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


          return;


    }


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


  alert('Short Desc: '+values[0]);


  alert('Desc: '+values[1]);


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


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


}


}




Script include:


var getStandardFields = Class.create();


getStandardFields.prototype = Object.extendsObject(AbstractAjaxProcessor),{


     


  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'


};