Autofill Lookup Select Box based on data from another Lookup Select Box

hanaphouse
Giga Guru

I have 2 lookup select box:

  • Requested_for -> sys_user
  • Department -> cmn_department

The goal I have is to autofill cmn_department lookup select box based on value from sys_user.

Here's what I have done so far:

  • Create a Catalog item
  • Create a Client Script

The Client Script:

find_real_file.png

 

The Catalog Item:

find_real_file.png

find_real_file.png

When I opened the form in Service Portal, the client script is not working.

find_real_file.png

1 ACCEPTED SOLUTION

Brad Bowman
Kilo Patron
Kilo Patron

The typical application of these fields is for both of them to be reference fields.  Add the same tables to the reference qualifier, then things like active=true and whatever else, then set the default value for Requested for to

javascript: gs.getUserID()

So this populates Requested for when the form loads to the current user, but they can select a different user by typing a partial name with autocomplete, or opening the reference search window.  To get Department to populate with that of the Requested for, create an onChange catalog client script on Requested for.  Since Requested for is getting set on load by default this may also work to populate the Department when the form loads.  If it doesn't, once you get the script worked out just create a similar one onLoad. The client script will need to call a script include to return the department.  Your client script will look something like this

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

 var ga = new GlideAjax('FieldLookup'); //Name of the Script Include
 ga.addParam('sysparm_name','getField'); //name of function in script include
 ga.addParam('sysparm_sysid',g_form.getValue('requested_for');
 ga.getXML(fieldCallback);
}
 
function fileldCallback(response) {
 var answer = response.responseXML.documentElement.getAttribute("answer");
 g_form.setValue('department', answer);//name of the variable to update
}

Then you need to create a script include, ensuring the Client callable box is checked

var FieldLookup = Class.create();
FieldLookup.prototype = Object.extendsObject(AbstractAjaxProcessor, {
 
 getField: function() {
  var sysid = this.getParameter('sysparm_sysid');
  var usr = new GlideRecord('sys_user');
  usr.addQuery('sys_id', sysid);
  usr.query();
  if(usr.next()) {
   return usr.department.toString();
  }
 },

type: 'FieldLookup'
});

View solution in original post

1 REPLY 1

Brad Bowman
Kilo Patron
Kilo Patron

The typical application of these fields is for both of them to be reference fields.  Add the same tables to the reference qualifier, then things like active=true and whatever else, then set the default value for Requested for to

javascript: gs.getUserID()

So this populates Requested for when the form loads to the current user, but they can select a different user by typing a partial name with autocomplete, or opening the reference search window.  To get Department to populate with that of the Requested for, create an onChange catalog client script on Requested for.  Since Requested for is getting set on load by default this may also work to populate the Department when the form loads.  If it doesn't, once you get the script worked out just create a similar one onLoad. The client script will need to call a script include to return the department.  Your client script will look something like this

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

 var ga = new GlideAjax('FieldLookup'); //Name of the Script Include
 ga.addParam('sysparm_name','getField'); //name of function in script include
 ga.addParam('sysparm_sysid',g_form.getValue('requested_for');
 ga.getXML(fieldCallback);
}
 
function fileldCallback(response) {
 var answer = response.responseXML.documentElement.getAttribute("answer");
 g_form.setValue('department', answer);//name of the variable to update
}

Then you need to create a script include, ensuring the Client callable box is checked

var FieldLookup = Class.create();
FieldLookup.prototype = Object.extendsObject(AbstractAjaxProcessor, {
 
 getField: function() {
  var sysid = this.getParameter('sysparm_sysid');
  var usr = new GlideRecord('sys_user');
  usr.addQuery('sys_id', sysid);
  usr.query();
  if(usr.next()) {
   return usr.department.toString();
  }
 },

type: 'FieldLookup'
});