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

shloke04
Kilo Patron

Hi Nitin,



Did your issue got Resolved? If yes please mark the answer as correct and close this thread.



Regards,


Shloke


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

Regards,
Shloke

Michael Ritchie
ServiceNow Employee
ServiceNow Employee

Nitin, you may find my getReferenceAdvanced solution much easier than some of the proposed solutions using GlideAjax.   Once you import the update set from Share you can make a simple function call to get data from a reference field's record.   You can read more about this solution here:


getReferenceAdvanced, g_form.getReference and GlideAjax Alternatives