Welcome to Community Week 2025! Join us to learn, connect, and be recognized as we celebrate the spirit of Community and the power of AI. Get the details  

client script and ajax call

amit98971223
Kilo Contributor

HI,

Im trying to get display value of a reference field. I know of the method g_form.getDisplayBox(), but I am trying to do it from ajax call.

I am only able to get the sys id of the reference field.

I tried the following but no luck.

Client side:

ga.addParam('sysparm_field_sysID',g_form.getValue('reference_field'));

script include:

var val1=this.getParameter('sysparm_field_sysID');

ans=val1.getDisplayValue() ;

How can I use getDisplayValue() in a script include or I cannot, please help me?

regards

AMit

1 ACCEPTED SOLUTION

Harsh Vardhan
Giga Patron

Hi Amit,



Please check the script below.



client script:



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


  if (isLoading) {


  return;


  }



  var test= g_form.getValue('enter your reference field');


  var check = new GlideAjax('your script include name');


  check.addParam('sysparm_name', 'script include function name');


  check.addParam('sysparm_comp', test);


  check.getXML(analyzeResponse);



  function analyzeResponse(response)


  {


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



  alert(answer);



}





script include:



name: test


Must be checked as client callable



var test= Class.create();


auto_fill_lochypcomp.prototype = Object.extendsObject(AbstractAjaxProcessor, {



  test: function()


  {



  var test= this.getParameter('sysparm_comp');


  var grp = new GlideRecord('sys_user');


grp.addQuery('name',test);


  grp.query();


  while(grp.next())


  {


//now you can perform your query here.


  }


      },



  type: 'test'


});




I hope it will help you.




Thanks,


Harshvardhan


View solution in original post

4 REPLIES 4

Dave Smith1
ServiceNow Employee
ServiceNow Employee

You'll need to create an object of AbstractAjaxProcessor in a class within the Script Include, and a GlideAjax object within your client script.



We cover this on our Scripting Course - have you attended that at all?


Hi Dave,
I probably haven't attended it, that is why the confusion..
I will surely read it


Harsh Vardhan
Giga Patron

Hi Amit,



Please check the script below.



client script:



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


  if (isLoading) {


  return;


  }



  var test= g_form.getValue('enter your reference field');


  var check = new GlideAjax('your script include name');


  check.addParam('sysparm_name', 'script include function name');


  check.addParam('sysparm_comp', test);


  check.getXML(analyzeResponse);



  function analyzeResponse(response)


  {


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



  alert(answer);



}





script include:



name: test


Must be checked as client callable



var test= Class.create();


auto_fill_lochypcomp.prototype = Object.extendsObject(AbstractAjaxProcessor, {



  test: function()


  {



  var test= this.getParameter('sysparm_comp');


  var grp = new GlideRecord('sys_user');


grp.addQuery('name',test);


  grp.query();


  while(grp.next())


  {


//now you can perform your query here.


  }


      },



  type: 'test'


});




I hope it will help you.




Thanks,


Harshvardhan


Thanks Harsh vardhan, it helped.