Client script onChange

snowuser111
Kilo Guru

Hi,

Can anyone help please.

I have a reference field(sys_user) in incident . When a user is selected automatically the other field (u_user_manager) should populate the manager of the user.

Thanks

1 ACCEPTED SOLUTION

manikorada
ServiceNow Employee
ServiceNow Employee

You need to have a Client Script as:



  1. function onChange(control, oldValue, newValue, isLoading, isTemplate) {
  2.     if (isLoading || newValue == '') {
  3.           return;
  4.     }
  5.     var user = g_form.getReference('<<fieldname>>');
  6.     g_form.setValue('u_user_manager', user.manager);
  7.    
  8. }

View solution in original post

17 REPLIES 17

OK.


It is resolved via Script include. Now it works fine.


The above answers did not work for me. To get manager name of a user onChange use GlideAjax:



Write the onChange function as follows:


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


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


  return;


  }


  else


  {


  var ga = new GlideAjax('ScriptLibrary');//This is the name of your script include, make it client callable


  ga.addParam('sysparm_name','getUserManager');//This is your function in the script include


  ga.addParam('sysparm_user_name', newValue);//newValue is the user sys id--it must come from a reference field


  ga.getXML(GetParse);


  }


  function GetParse(response) {


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


  g_form.setValue('corresponding_manager', answer);//corresponding_manager is the field name that will be updated


  }


}



server side script include needs to be as follows:



var ScriptLibrary = Class.create();


ScriptLibrary.prototype = Object.extendsObject(global.AbstractAjaxProcessor, {




  getUserManager: function() {



  var   parent=new GlideRecord('sys_user');


  var id=this.getParameter('sysparm_user_name');


  parent.addQuery('sys_id', id);


  parent.query();


  parent.next();


  return   parent.manager;


  },


find_real_file.png



find_real_file.png


tamarow
Kilo Contributor

Consider using the strict comparison operator === instead of == in your boolean condition.