Auto populate Location(location) Based on the Users selection.

Sitakanta Bej12
Tera Contributor

My Requirement is in service catalog request  we have two variables 
1) reee_joiner_name (Type=Reference. Reference table is "sys_user")
2)Location(Type=SingleLine Text)
When we select the reee_joiner_name , Location will be autopopulate based on the reee joiner name for user.

I have written the below Onchange catalog client script ::


var Rejoin = g_form.getReference('reee_joiner_name',callback);
   function callback(Rejoin){
       g_form.setValue('phone_number',Rejoin.mobile_phone);
       g_form.setValue('location',Rejoin.location);
   }

According to this code Phone number will displaying fine. But in the Location field(location) they displaying the sys_id of the location. But my requirement is displaying Location name.

Please help me Regarding this.

 

11 REPLIES 11

Sagar Pagar
Tera Patron

Hi @Sitakanta Bej12,

 

For reference field sys_id is fine to autocomplete.

 

For String type field, you need to dotwalk with name field.

 

Try this updated scripts:

 

var Rejoin = g_form.getReference('reee_joiner_name',callback);
   function callback(Rejoin){
       g_form.setValue('phone_number', Rejoin.mobile_phone);
       g_form.setValue('location', Rejoin.location.name);
   }

 

 

If my response helps to solve your issue. Kindly mark it as helpful & correct. It will be helpful for future readers.
Thanks,
Sagar Pagar

The world works with ServiceNow

Hi Sagar

 

I have checked with that one also , it is showing me Location as "Undefined".

Hi @Sitakanta Bej12,

 

Try this:

Script Include:

var getUserDetails = Class.create();
getUserDetails.prototype = Object.extendsObject(AbstractAjaxProcessor, {

getLocationAndMobile: function() {
	var userLocation = new GlideRecord('sys_user');
	userLocation.get(this.getParameter('sysparm_user_id'));
	return userLocation.getDisplayValue("location") + "#" + userLocation.getValue("mobile_phone");

},

type: 'getUserDetails'
});

 

Client script:

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

      var locationGA = new GlideAjax("getUserDetails");
      locationGA.addParam("sysparm_name", "getLocationAndMobile");
      locationGA.addParam("sysparm_user_id", newValue);
      locationGA.getXML(getUserLoc);

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

          var results = answer.split("#");

          g_form.setValue("location", results[0]);
          g_form.setValue("mobile_phone", results[1]);

      }
  }

 

 

If my response helps to solve your issue. Kindly mark it as helpful & correct. It will be helpful for future readers.
Thanks,
Sagar Pagar

The world works with ServiceNow

Hi Sagar,

 

It's working fine but we want get the newValue that line code is missing

 

var newValue = g_form.getValue('reee_joiner_name');

 

Thank & regards,

chandra