How to Get Reference Field Values of a Table ??

Tarun2
Tera Contributor

Hi , 

How to know the value of Reference Field values  in a Record ?   (Value)

var arr_fields=[];

var fields = new GlideRecord('sys_dictionary');

fields.addQuery('name','incident'); // table name

fields.addEncodedQuery('internal_type=reference');//getting reference type fields

fields.query();

while(fields.next())
{
arr_fields.push(fields.column_label.toString()+"("+fields.element.toString() + ")" );
}


for(var i=0; i<arr_fields.length; i++)
{
gs.print(arr_fields[i]);
}

I have multiple reference Fields in Record .

I am getting Reference Field Names (One By One )

  But  I want Reference Field Values .

Any Suggestion is Appreciated .

Thankyou , 

 

1 ACCEPTED SOLUTION

Harsh Vardhan
Giga Patron

working script: 

 

here i tested the script, for my incident table, same way you can do it for your table. 

 

var gr = new GlideRecord('incident');  // change the table name
gr.addQuery('sys_id','98e902e02f6b4c10f68d5ff62799b609');  // add record id
gr.query();
gr.next();

var fields = gr.getFields();
for(var i = 0;i<fields.size();i++){

 

var glideElement = fields.get(i);
if (glideElement.hasValue()) {

if(glideElement.getED().getInternalType() == 'reference'){

gs.print(glideElement.getDisplayValue());
}



}


}

View solution in original post

12 REPLIES 12

Mohit Kaushik
Mega Sage
Mega Sage

Hi Tarun,

I would like to know your requirement more properly. What is the meaning from the values of reference fields. 

If you want to know the values of reference fields for incident table for a particular record then you can tweak your script somewhat like this:

var fields = new GlideRecord('sys_dictionary');

fields.addQuery('name','incident'); // table name

fields.addEncodedQuery('internal_type=reference');//getting reference type fields

fields.query();

while(fields.next())
{
arr_fields.push(fields.column_name.toString()+"("+fields.element.toString() + ")" ); //instead of label change it to name
}


for(var i=0; i<arr_fields.length; i++)
{
gs.print(arr_fields[i]);

// suppose you are on the same record for which you need the value of reference fields

gs.print(current.arr_fields[i]);  //this line will print the value of fields

}

Thanks,
Mohit Kaushik
ServiceNow MVP (2023-2025)

Hi ,

i have a Table : Demo2s

find_real_file.png

I have 1 Record : Rite Aid

In that I Have 3 Reference Fields 

find_real_file.pngI want all 3 Reference Field values of Record Rite Aid : (RiteAid,INC000047,Created 2020-03-13 00:05:48)

 

I have : 

sys Id of RECORD -RITE AID.  :3D1c4aa3702feb48108490dcb6f699b621

Table name :Demo2s 

 How to glide Reference field Values ??

 

               

Okay,

so as you said you are able to get their name with the current script?

If so then write the same script in the business rule of Demo2s table and update the script with the changes i have mentioned in my previous reply and change the table name as well in your encoded query.

You need to change the column_label to column_name that will give you the backend name of the fields and once you have that names in the array then you can easily print the values by calling them in the for loop using current object. 

for eg

current.arr_fields[i].getDisplayValue(); // this will give you the value of ith field from the record. 

 

Please mark this answer correct if it resolved the query and mark it helpful too if it helped you at all.

 

Thanks,

Mohit Kaushik

Thanks,
Mohit Kaushik
ServiceNow MVP (2023-2025)

hi , 

i mean for a Particular record . 

how to add Query for a Particular Record Using Sysid .

How can i get value without passing  which Record to get values from ??

Kindly Help me , with the code 

Thankyou ,

var gr = new GlideRecord('<u_demo2s or demo2s whatever the name is>'); // change the table name
gr.addQuery('sys_id','1c4aa3702feb48108490dcb6f699b621'); // add record id
gr.query();
gr.next();

var fields = gr.getFields();
for(var i = 0;i<fields.size();i++){

 

var glideElement = fields.get(i);
if (glideElement.hasValue()) {

if(glideElement.getED().getInternalType() == 'reference'){

gs.print(glideElement.getDisplayValue());

}

 

}


}

 

Hi use this. sys_id does not start from 3D that is a uri equivalent of 3D means =. So enter the sys_id as '1c4aa3702feb48108490dcb6f699b621'

 

Thanks,

Mohit

Thanks,
Mohit Kaushik
ServiceNow MVP (2023-2025)