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,



Assuming the field 'u_standard_change_num' is a string type field, just replace the query condition in script include with below.



standardFields.addQuery('u_number', stdChange); //give the exact field name of number as in ur u_standard_changes table.


Hi Guhan,



u_number is a reference field.


One thing I found:


I changed the client script as follows:



function getResponse(response) {


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


      alert(answer);


}


}



I also changed the script include to return only one parameter:


if(standardFields.next()) {


                      return standardFields.u_name_of_the_change;


              }


              return '';


etc..



The answer I am getting in the alert is "null".


So, it is not returning anything?


Also, can I add in the script include a gs.log statement somewhere to see what standard change sys_id is being sent from the client script? because wherever I add it, I don't get anything in the log...


Yes, you can add gs.log statements like below and check them in the Script logs list



getFields : function() {  


 


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




gs.log('stdChange : '+stdChange); //Check the parameter




  var standardFields = new GlideRecord('u_standard_changes');  


  standardFields.addQuery('sys_id', stdChange);  


  standardFields.query();  


 


gs.log('Record count: '+standardFields.getRowCount()); //This will return no. of records matching the query condition. If it gives 0 then no records matching the condition




if(standardFields.next()) {  


                      return standardFields.u_name_of_the_change;  


}  


return '';


 


  },  


Hi Guran,



Yes, that is exactly what I did before posting. Nothing appears in the log. That is why I asked where to put the gs.log line.



Kumar,


Yes, it is a sys_id. I added an "alert(standardChangeNum);" line in the client script.


I assume that the line "ga.addParam('sysparm_std_change', standardChangeNum);" means to add parameter called sysparm_std_change, which is the standardChangeNum.


The alert returns the sys_id of the standard change I choose in the field u_standard_change_num.



So I do not understand whether the client script is even calling the script include or not...


Harel



You have added


standardFields.addQuery('sys_id', stdChange); to the query




Are you sure that "stdChange" that you have passed from client script consists of sys_id ???


can you try to put alert on client script and see if that field consists of sys_id or actual field....