when I change the user then selected user details will be populate automatically like email mobile.

Prasad Kumar
Tera Contributor

I have added three variables which are one is "Name [reference of sys_user], another one is Email [string type], and the last variable is Mobile [string type]" for catalog items
if I select the user in the Name field then the details will be populated in the email and mobile fields automatically.

1 ACCEPTED SOLUTION

Sumanth16
Kilo Patron

Hi @Prasad Kumar ,

 

 

onChange client script on your reference field:

 

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

 

  if (isLoading) {

 

  return;

 

  }

 

  if(newValue === ''){

 

 

 

  g_form.setValue('<mobile number variable name>','');

 

  g_form.setValue('<email id variable name','');

 

  }

  var ga = new GlideAjax('GetUserDetails');

 

  ga.addParam('sysparm_name','getDetails');

 

  ga.addParam('sysparm_user_id',newValue);

 

  ga.getXML(CallBack);



  function CallBack(response) {

 

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

 

  answer = answer.evalJSON(); //Transform the JSON string to an object

 

  g_form.setValue('<manager variable name>',answer.manager);

 

  g_form.setValue('<mobile number variable name>',answer.number);

 

  g_form.setValue('<email id variable name',answer.email);



  }

 

}

 

Script Include and select client callable option:

 

Name: GetUserDetails

 

Client callable: true

 

Script:

 

var GetUserDetails = Class.create();

 

GetUserDetails.prototype = Object.extendsObject(AbstractAjaxProcessor, {

 

  getDetails: function(){

 

  var obj = {};

 

  var usr= new GlideRecord('sys_user');

 

  usr.get(this.getParameter('sysparm_user_id'));

 

  obj.manager = usr.getValue('manager');

 

  obj.email = usr.getValue('email');

 

  obj.number=usr.getValue('mobile_phone');

 

  var json = new JSON();

 

  var data = json.encode(obj);//JSON formatted string

 

  return data;

 

  },

 

  type: 'GetUserDetails'

 

});

 

If I could help you with your Query then, please hit the Thumb Icon and mark it as Correct !!

 

Thanks & Regards,

Sumanth Meda

 

View solution in original post

2 REPLIES 2

Sumanth16
Kilo Patron

Hi @Prasad Kumar ,

 

 

onChange client script on your reference field:

 

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

 

  if (isLoading) {

 

  return;

 

  }

 

  if(newValue === ''){

 

 

 

  g_form.setValue('<mobile number variable name>','');

 

  g_form.setValue('<email id variable name','');

 

  }

  var ga = new GlideAjax('GetUserDetails');

 

  ga.addParam('sysparm_name','getDetails');

 

  ga.addParam('sysparm_user_id',newValue);

 

  ga.getXML(CallBack);



  function CallBack(response) {

 

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

 

  answer = answer.evalJSON(); //Transform the JSON string to an object

 

  g_form.setValue('<manager variable name>',answer.manager);

 

  g_form.setValue('<mobile number variable name>',answer.number);

 

  g_form.setValue('<email id variable name',answer.email);



  }

 

}

 

Script Include and select client callable option:

 

Name: GetUserDetails

 

Client callable: true

 

Script:

 

var GetUserDetails = Class.create();

 

GetUserDetails.prototype = Object.extendsObject(AbstractAjaxProcessor, {

 

  getDetails: function(){

 

  var obj = {};

 

  var usr= new GlideRecord('sys_user');

 

  usr.get(this.getParameter('sysparm_user_id'));

 

  obj.manager = usr.getValue('manager');

 

  obj.email = usr.getValue('email');

 

  obj.number=usr.getValue('mobile_phone');

 

  var json = new JSON();

 

  var data = json.encode(obj);//JSON formatted string

 

  return data;

 

  },

 

  type: 'GetUserDetails'

 

});

 

If I could help you with your Query then, please hit the Thumb Icon and mark it as Correct !!

 

Thanks & Regards,

Sumanth Meda

 

James Chun
Kilo Patron

Hey @Prasad Kumar,

 

You can use the 'Auto-populate' functionality.

Within your catalog variable, configure the 'auto-populate' tab something like the below:

JamesChun_1-1709495939090.png

 

Cheers