Is there a way to get the full object of a reference field in a glide query?

nicksoltis
Kilo Expert

I am looking to query on a bridge (relationship) table to pull and store the reference fields as objects. This is hard to explain, so I will give you my example.

EX:

TABLES INVOLVED:   vtb_board, vtb_card, vtb_task, vtb_lane
Visual Task Board Card (vtb_card) has 3 reference fields that I would like to access (aka the "bridge" table)...
1. "task"   ->  reference to Personal Task (vtb_task)
2. "lane"   ->  reference to Visual Task Board Lane (vtb_lane)
3. "board" -> reference to Visual Task Board (vtb_board)

SCRIPT
data.taskData = [];

var gr = new GlideRecord("vtb_card");
gr.addActiveQuery();
gr.query();

while(gr.next()){
   var temp = {};
   temp.task = gr.task.getFullReferenceObject();
   temp.lane = gr.lane.getFullReferenceObject();
   temp.board = gr.board.getFullReferenceObject();
   data.taskData.push(temp);
}

I know that I can populate them by adding to the array of objects using multiple glide query's, but I figured that something like this would exist already in the SN universe.

Thanks,

Nick

5 REPLIES 5

Jon Barnes
Kilo Sage

use temp.task = gr.task.getRefRecord();

that is how you get a gliderecord from a reference field. Just keep in mind, storing GlideRecords in an array object can take up alot of memory depending on how big that array gets, so proceed with caution.

Also, is this a SP widget? If so, keep in mind that GlideRecords don't serialize to JSON well so this will almost certainly not work if you are planning to then access the objects on the client side. If you need to do that, it would be better for you to get a JSON friendly object representation of the glide records before you send them to the client.

Actually, scratch my #2. When I consol.log() the object using .getRefRecord() all of the child objects are empty.

nicksoltis
Kilo Expert

Thanks, I'll give that a try.

To complete the response:
1. The only reference record that would require a lot is the "vtb_task", the others are relatively small. Would you recommend using multiple queries on a table to pick and choose the fields from table to push? From a performance perspective?

2. This will be used in an SP Widget. I still have to test it out fully, but it seems to be working ok for what I need.

 

Yes I wrote a function in my instance that you can pass a GlideRecord and an array of fields you want, and it will return a JSON-friendly object that can be serialized and sent to the client easily. the format is like:

{

  field_1: {

    value: '',

    displayValue: ''

  },

  field_2: {

    value: '',

    displayValue: ''

  }

}