How can I determine which table a reference field references in script?

liv_cp
Giga Contributor

Within a script include I'd like to determine the field type of certain fields and, if the type is reference, which table it references.

I don't have any issues with determining the field type and can successfully determine whether a field is a reference field or not using element descriptor and getInternalType().

However, I'm not sure of the best way to determine which table the field references.

Currently, my only idea is to glide the sys_dictionary table to find the field and hence which table it references but I'm sure there must be a function somewhere that gets this in a more elegant way?

1 ACCEPTED SOLUTION

Sebastiaan de V
Kilo Guru

Hi Liv,



You can use the function getReferenceTable() of the GlideElement, see the documentation: getReferenceTable()



var grINC = new GlideRecord('incident');
grINC.query('number','INC0010041'); // record assignment group assigned to "CAB Approval"
if (grINC.next()) {
  // Get the table name
  var tableName = grINC.assignment_group.getReferenceTable();
  gs.info( tableName );
}



Please Hit like, Helpful or Correct depending on the impact of the response


View solution in original post

5 REPLIES 5

Sebastiaan de V
Kilo Guru

Hi Liv,



You can use the function getReferenceTable() of the GlideElement, see the documentation: getReferenceTable()



var grINC = new GlideRecord('incident');
grINC.query('number','INC0010041'); // record assignment group assigned to "CAB Approval"
if (grINC.next()) {
  // Get the table name
  var tableName = grINC.assignment_group.getReferenceTable();
  gs.info( tableName );
}



Please Hit like, Helpful or Correct depending on the impact of the response


Hi Sebastiaan,



I've tried and tested this and it works perfectly - thank you!


Is there a way that this function can be used for Glide List fields?


getReferenceTable() does not work for Glide List fields, but you can use this method to find the reference table for a glide list: gliderecord.element.getED().reference. This is undocumented, but you can find this when you inspect the GlideElementDescriptor.



var inc = new GlideRecord('incident');


inc.get('number', 'INC0010096');


var tableName = inc.watch_list.getED().reference;


gs.print(tableName);


// => sys_user