Join the #BuildWithBuildAgent Challenge! Get recognized, earn exclusive swag, and inspire the ServiceNow Community with what you can build using Build Agent.  Join the Challenge.

Populate mobile number of the caller

Sam198
Mega Guru

Hi all,

I need to autopopulate the mobile number field from the sys_user table on the catalog item:

find_real_file.png

I am using javascript:gs.getUser().getRecord().getDisplayValue("mobile_phone"); in the default value tab of the variable "Contact Number" to get this autopopulated, but it does not, any suggestions?

I do not need change this field through onChange or any Catalog client scripts as I am leaving this field open anyway for them to change the contact number if they want to (because for example: changed phone number but not updated on sys_user record).

Thanks.

 

 

1 ACCEPTED SOLUTION

there's a gs.getUser() method for it, so you could use the following directly in the default value of the field or variable:

javascript: var userPhone; var user = new GlideRecord('sys_user'); if (user.get(gs.getUserID())) {userPhone = user.phone}; userPhone;

View solution in original post

14 REPLIES 14

Deepak Negi
Mega Sage
Mega Sage

Can you try logout and login again. I guess gs.getUser().getRecord() is cached at login.

 

Try with getValue once

logout and login - still same, it does not populate the number in the catalog field automatically.

 

Service_RNow
Mega Sage

Hi,

For Auto population of the above deabove-definedes you can write an Script Include and call the same Script Include in an OnLoad Catalog Client Script as mentioned below:

 

 

 

Script Include:

 

 

 

var requestor_details = Class.create();

 

requestor_details.prototype = Object.extendsObject(AbstractAjaxProcessor, {

 

 

 

  requestor_info: function() {

 

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

 

  var logged_user = gs.getUserID();

 

  var user_detail = new GlideRecord('sys_user');

 

  user_detail.addQuery('sys_id',logged_user);

 

  user_detail.query();

 

  while(user_detail.next())

 

  {

 

  result.setAttribute("user",user_detail.sys_id);

 

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

 

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

 

  result.setAttribute("location",user_detail.location);

 

 

 

  }

 

  },

 

 

 

  type: 'requestor_details'

 

});

 

 

 

find_real_file.png

 

 

 

2) Then you can write an On Load Catalog Client Script wither on your Catalog Item or on the Variable Set to have the variables populated as shown below:

 

 

 

Script:

 

 

 

function onLoad()

 

{

 

  var ga = new GlideAjax("requestor_details");

 

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

 

  ga.getXML(ajaxResponse);

 

  function ajaxResponse(serverResponse) {

 

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

 

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

 

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

 

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

 

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

 

  g_form.setValue('requestor_phone',phone);

 

  g_form.setValue('requestor_name',user);

 

  g_form.setValue('Location_user',loc);

 

  }

 

}

 

 

 

find_real_file.png

 

 

 

Replace your Variable Name appropriately in the Set Value Conditions say for example in place of the variable "Location_user" with your Location Variable.

 

 

 

In a Similar Way you can try for other Variables also.

 

 

 

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

 

 Thanks

Hi Ram,

Thanks for sharing the script, however, as i mentioned I am reluctant to use this long piece of codes where we could do this in one line for example I am autopopulating the caller manager with javascript: gs.getUser().getManagerID(); and it works perfect, so there should be a way to populate the phone number similar to this. I will keep looking.