The Zurich release has arrived! Interested in new features and functionalities? Click here for more

iIs it possible to fill a GlideRecord dot walk with a variable?

sleepycrown
Giga Guru

Hi hi,

 

I have a script that looks up a Lookup Multiple Choice variable then looks up valid items for that variable based on the table it pulls its choices from and the reference qualifier set. The part I'm stuck on is making use of the Lookup value field, which says which column to get data from. 

 

See my script below:

var lookupChoices = []; //array to hold the found choices
var catItem = some_sys_id;
var fieldName = lookup_multiple_choice;
var gr = new GlideRecord('item_option_new');
gr.addQuery('cat_item', catItem);
gr.addQuery('name', fieldName);
if  (gr.next()) {
  var fieldValue = gr.lookup_value; // in this case 'name'
  var gr2 = new GlideRecord(gr.lookup_table);
  gr2.addEncodedQuery(gr.reference_qual);
  gr2.query();
  while (gr2.next()) {
    lookupChoices.push(gr2.<column_name>.toString());
  }
}

I would like to be able to set <column_name> dynamically with whatever field the Lookup value field is looking at (in this case 'name'). I don't want to code a single column name because the table and column this variable could reference will vary in each place it's used. 

 

I store the column name pulled from the variable in

var fieldValue = gr.lookup_value;

However, it cannot be used like this in the dot walk: 

lookupChoices.push(gr2.fieldValue.toString());

It makes sense why this doesn't work, so I'm not necessarily looking for an explanation there, but rather another way to accomplish this without having to first know the column name. The second GlideRecord gives me the correct list, but I can't select the required column. 

 

Any ideas?

5 REPLIES 5

sleepycrown
Giga Guru

It's not likely that someone would need this, but I was able to get my desired result using a combination of GlideRecord and GlideQuery. GlideQuery allows us to select just a single column, which can be expressed with a variable. The GlideQuery result can be passed to a JSON string, which can then be split to give only the value from the first key/value pair. Check it out below!

var catalogItem = 'some_sys_id';
var catItemVarName = 'lookup_multiple_choice'; // name of the Lookup Multiple Choice variable
var lookupChoices = []; // array to hold list of choices that match the reference qualifier 
var getLookupMultChoiceSpecs = new GlideRecord('item_option_new');
  getLookupMultChoiceSpecs.addQuery('cat_item', catalogItem);
  getLookupMultChoiceSpecs.addQuery('name', catItemVarName);
  getLookupMultChoiceSpecs.query();
  if (getLookupMultChoiceSpecs.next()) {
    var getLookupMultChoices = new GlideRecord(getLookupMultChoiceSpecs.lookup_table); // in my example, the sys_user table 
    getLookupMultChoices.addEncodedQuery(getLookupMultChoiceSpecs.reference_qual); // in my example, department=a581ab703710200044e0bfc8bcbe5de8^company=81fbfe03ac1d55eb286d832de58ae1fd is used as a reference qualifier. The department sys_id is the baseline/out-of-the-box sys_id for the Finance department. The company sys_id is the baseline/out-of-the-box sys_id for the ACME France company.
    getLookupMultChoices.query();
    while (getLookupMultChoices.next()) {
      lookupChoices.push(getLookupMultChoices.sys_id.toString());
    }
  }
  var randomLookupMultChoice = Math.floor(Math.random() * lookupChoices.length);
  var getLookupChoiceColumn = new GlideQuery(getLookupMultChoiceSpecs.lookup_table)
    .where('sys_id', lookupChoices[randomLookupMultChoice])
    .select(getLookupMultChoiceSpecs.lookup_value) // in my example, the lookup_value field of the Lookup Multiple Choice variable is the 'name' field on sys_user. GlideQuery allows us to select a SINGLE column, which was the desired end goal.
    .toArray(1)
    var strObj = JSON.stringify(getLookupChoiceColumn); 
    gs.info(strObj); // returns [{"name":"random_name","sys_id":"4e826bf03710200044e0bfc8bcbe5d93"}]
    gs.info(strObj.split('"')[3]); // returns the value between the second set of double quotes (")