Populate a record producer variable based on another variable?

ianw
Giga Contributor

Hi,

I have only been working with ServiceNow for a few weeks and have a situation I need to figure out how to do urgently.

I have a record producer that contains these three variables:

  1. Location Type (location_type)
  2. Location Name (location_name)
  3. Account (account)

The Location Type variable is a Lookup Select Box with the following Type Specifications:

  • Lookup from table: cmn_location
  • Lookup value field: Type
  • Include none: Yes
  • Unique values only: yes
  • Reference qual: javascript: 'account='+current.variables.account;

In the form, when a user changes a Location Type (type is a column in the cmn_location table) I want the list of Location Names to change. Since I am new to ServiceNow, I am unsure of what the best practices are and how to implement this that will perform the best.

The Location Name variable needs to be a Lookup Select Box too, not a reference field (for searching purposes).

Other considerations:

  • If the Location Type is "--None--" then the Location Name value should be none.
  • If the Location Type is changed the the Location Name should become none too.

I have had a go and writing a Script Include to do this, but since this is my first time doing this I don't know if I am actually returning the correct object in my script.

Here is the script:

var GetLocations = Class.create();

GetLocations.prototype = Object.extendsObject(AbstractAjaxProcessor, {

  getLocationsFromType: function(account,type)

  {

  var locName = new GlideRecord('cmn_location');

  locName.addQuery('account',account);

  locName.addQuery('type',type);

  locName.query();

  return locName;

  },

      type: 'GetLocations'

});

and then back on the Location Name variable in the type specifications I set the reference qual to:

javascript: new GetLocations().getLocationsFromType(current.variables.account,current.variables.location_type);

but this doesn't work and I have since read that I should do this in a Catalog Client Script. Can somebody please help me get my script include correct, and help me with the Catalog Client Script?

Thank-you.

1 ACCEPTED SOLUTION

BALAJI40
Mega Sage

Try below script,



var GetLocations = Class.create();


GetLocations.prototype = Object.extendsObject(AbstractAjaxProcessor, {



  getLocationsFromType: function(account,type)


  {


var locations = [];


  var locName = new GlideRecord('cmn_location');


  locName.addQuery('account',account);


  locName.addQuery('type',type);


  locName.query();


  while (locName.next()){


locations.push(locName.getValue('sys_id').toString());


}


return 'sys_idIN'+locations;


  },


      type: 'GetLocations'


});


View solution in original post

2 REPLIES 2

BALAJI40
Mega Sage

Try below script,



var GetLocations = Class.create();


GetLocations.prototype = Object.extendsObject(AbstractAjaxProcessor, {



  getLocationsFromType: function(account,type)


  {


var locations = [];


  var locName = new GlideRecord('cmn_location');


  locName.addQuery('account',account);


  locName.addQuery('type',type);


  locName.query();


  while (locName.next()){


locations.push(locName.getValue('sys_id').toString());


}


return 'sys_idIN'+locations;


  },


      type: 'GetLocations'


});


If I have answered your question, please mark my response as correct so that others with the same question in the future can find it quickly and that it gets removed from the Unanswered list.



If you are viewing this from the community inbox you will not see the correct answer button.   If so, please review How to Mark Answers Correct From Inbox View.



Thank you