Populate EMail and phone of reference - reference via On change

RudhraKAM
Tera Guru

Can some one help me with the script 

 

As of now we have a reference field of sys_user_grmember where we are only populating one group users 

 

up on selecting the user we need to populate users email and phone , below is the script which is showing undefined, can some one help me where am I doing wrong?

 

 

RudhraKAM_0-1690164170621.png

 

 

function onChange(control, oldValue, newValue, isLoading) {
   if (isLoading || newValue == '') {
      return;
   }

  var user = g_form.getReference("project_commissioning_engineer", populateManager);
}
function populateManager(user) {
  g_form.setValue("project_commissioning_engineer_email", user.email);
 g_form.setValue("project_commissioning_engineer_phone", user.phone);	
	
   
}
5 REPLIES 5

AnveshKumar M
Tera Sage
Tera Sage

Hi @RudhraKAM ,

getReferences get the sys_user_grmember as the callback function parameter. So try this.

 

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

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

      return;

   }

 

  var user_mem = g_form.getReference("project_commissioning_engineer", populateManager);

}

function populateManager(user_mem) {

  g_form.setValue("project_commissioning_engineer_email", user_mem.user.email);

 g_form.setValue("project_commissioning_engineer_phone", user_mem.user.phone); 

 

   

}

Thanks,
Anvesh

Hello @AnveshKumar M  its still showing as undefined 

Hi @RudhraKAM ,

It seems dot walking is not allowed in client side GlideRecord.

It's better to create a client Callable Script Include and call that using the GlideAjax to get the email and mobile. Follow this,

 

Client Callable Script Include:

var GetUserInfo = Class.create();

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

 

    userData: function() {

        var user = this.getParameter("sysparm_user");

        var grUser = new GlideRecord('sys_user');

        grUser.get("sys_id", user);

 

        var result = {    

           "email": grUser.getValue('email'),

"phone": grUser.getValue('phone')

        }; 

        return JSON.stringify(result);

    },

    type: 'GetUserInfo'

});

 

On Change Client Script:

 

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

 

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

      return;

   }

 

  var user_mem = g_form.getReference("project_commissioning_engineer", populateManager);

}

 

function populateManager(user_mem) {

var ga = new GlideAjax('GetUserInfo');

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

 ga.addParam('sysparm_user',user_mem.user);

 ga.getXMLAnswer(userDataParse);

}

function userDataParse(answer){

if(answer){

var data = JSON.parse(answer);

g_form.setValue("project_commissioning_engineer_email", data.email);

 

 g_form.setValue("project_commissioning_engineer_phone", data.phone);

}

}

 

 

Thanks,
Anvesh

Harish KM
Kilo Patron
Kilo Patron

Hi may i know which version of servicenow your using? If it is before Utah you can use catalog setter variables to autopopulate both variables no script needed.

Utah Version:

HarishKM_0-1690256141681.png

Before Utah Version use Catalog Setter variables

HarishKM_1-1690256191812.png

 

Regards
Harish