The CreatorCon Call for Content is officially open! Get started here.

Issue with auto populate field value

Nitin_NOW
Tera Guru

After reading all the threads related to auto populate the value of a field on catalog form, I still have some issue is getting this done.

I have variable which is referenced on user table on a catalog form. If a user is selected in that field, i have to populate the user's office phone number ( business number ) in one of the variable/field on the form. I have tweaked the existing code which is working fine to display other values like employee email id, job title etc.

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

 

  if(isLoading) return;

  if(newValue) {

  if (newValue != oldValue){

  getEmpData();

  } } }

  function getEmpData(){

  g_form.getReference('emp_name',function(strRequestedFor) {

  g_form.setValue('empl_email',strRequestedFor.email);

  g_form.setValue('emp_type',strRequestedFor.employee_number);

  g_form.setValue('empl_title',strRequestedFor.title);

  g_form.setValue('mobile_phone',strRequestedFor.mobile_phone);

      g_form.setValue('phone_number',strRequestedFor.user.phone);

The above highlighted one is the line I have added to the code, but it doesn't seems to be working. To get rid of this I have created a new onChange client script with the below code.

var id = g_form.getValue('phone');

                      var user = new GlideRecord('sys_user');  

                      user.addQuery('sys_id',id);  

                      user.query();  

                            if ( user.next() ) {  

                                  g_form.setValue('phone_number', user.phone);  

The above is also not working as expected and i don't see the office number is getting populated if i select a user in the user reference field. Any thoughts will be appreciated ?

Regards!

1 ACCEPTED SOLUTION

shloke04
Kilo Patron

Hi,



In order to achieve this you can write a Script Include and a OnChange Catalog Client Script which will Populate the User Phone Number when some User is selected in the User Variable as per the below Script:



Script Include:



var Requstor_Change = Class.create();


Requstor_Change.prototype = Object.extendsObject(AbstractAjaxProcessor, {



  getdetails : function() {


  var result = this.newItem("result");


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


  var user_info = new GlideRecord('sys_user');


  user_info.addQuery('sys_id',user_detail);


  user_info.query();


  while(user_info.next())


  {


  result.setAttribute("phone",user_info.phone);


  result.setAttribute("email",user_info.email);




  }


  },




      type: 'Requstor_Change'


});



find_real_file.png



2) Configure an OnChange Catalog Client Script on User Variable field as per the script below:



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


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


  return;


  }


  var ga = new GlideAjax("Requstor_Change");


  ga.addParam("sysparm_name", "getdetails");


  ga.addParam('sysparm_user_name',newValue);


  ga.getXML(ajaxResponse);


  function ajaxResponse(serverResponse) {


  var result = serverResponse.responseXML.getElementsByTagName("result");


  var phone = result[0].getAttribute("phone");


  var email = result[0].getAttribute("email");


  g_form.setValue('requestor_phone',phone);


  g_form.setValue('requestor_email',email);


  }}



find_real_file.png



Result:



find_real_file.png



Similarly you can have other field also updated in the above scripts which you want to pull from User Table.



Hope this helps.Mark the answer as correct/helpful based on impact.



Regards,


Shloke


Hope this helps. Please mark the answer as correct/helpful based on impact.

Regards,
Shloke

View solution in original post

6 REPLIES 6

Pradeep Sharma
ServiceNow Employee
ServiceNow Employee

Hello Nitin,



Your code has issues. You are comparing sys_user sys_id value with a phone number.


Error here                   user.addQuery('sys_id',id);   //Here in id you should pass the user reference field.



Also as a best practice I would suggest you to use GlideAjax approach instead of making direct server calls from client side.


Nitin_NOW
Tera Guru

Thanks pradeep for your update. I see the error in the code. But I have replaced 'id' to the value of the field on user table. But i still see it is not getting the value from the user table and populate it on the catalog item field.



function onChange(control, oldValue, newValue, isLoading)


{


                      var id = g_form.getValue('phone');


                      var user = new GlideRecord('sys_user');  


                      user.addQuery('sys_id',phone);   // Here 'phone' is the value of the field on user table. I also used 'user.phone' in place of 'phone' and still it is not                           working.


                      user.query();  


                            if ( user.next() ) {  


                                  g_form.setValue('phone_number', user.phone);  


}  


}


I don't see you have updated code


shloke04
Kilo Patron

Hi,



In order to achieve this you can write a Script Include and a OnChange Catalog Client Script which will Populate the User Phone Number when some User is selected in the User Variable as per the below Script:



Script Include:



var Requstor_Change = Class.create();


Requstor_Change.prototype = Object.extendsObject(AbstractAjaxProcessor, {



  getdetails : function() {


  var result = this.newItem("result");


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


  var user_info = new GlideRecord('sys_user');


  user_info.addQuery('sys_id',user_detail);


  user_info.query();


  while(user_info.next())


  {


  result.setAttribute("phone",user_info.phone);


  result.setAttribute("email",user_info.email);




  }


  },




      type: 'Requstor_Change'


});



find_real_file.png



2) Configure an OnChange Catalog Client Script on User Variable field as per the script below:



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


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


  return;


  }


  var ga = new GlideAjax("Requstor_Change");


  ga.addParam("sysparm_name", "getdetails");


  ga.addParam('sysparm_user_name',newValue);


  ga.getXML(ajaxResponse);


  function ajaxResponse(serverResponse) {


  var result = serverResponse.responseXML.getElementsByTagName("result");


  var phone = result[0].getAttribute("phone");


  var email = result[0].getAttribute("email");


  g_form.setValue('requestor_phone',phone);


  g_form.setValue('requestor_email',email);


  }}



find_real_file.png



Result:



find_real_file.png



Similarly you can have other field also updated in the above scripts which you want to pull from User Table.



Hope this helps.Mark the answer as correct/helpful based on impact.



Regards,


Shloke


Hope this helps. Please mark the answer as correct/helpful based on impact.

Regards,
Shloke