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

Mark Roethof
Tera Patron
Tera Patron

Hi there,

Could you explain what you are functionally after? Is this your own script or copied from somewhere? Feels like this could be done a lot different/easier.

If my answer helped you in any way, please then mark it as helpful.

Kind regards,
Mark
2020 ServiceNow Community MVP
2020 ServiceNow Developer MVP

---

LinkedIn
Community article list

 

Kind regards,

 

Mark Roethof

Independent ServiceNow Consultant

10x ServiceNow MVP

---

 

~444 Articles, Blogs, Videos, Podcasts, Share projects - Experiences from the field

LinkedIn

Hi ,

can you help me with the easy approach you are Talking about .

i have sysid of record .

ar arr_fields=[];
var fields = new GlideRecord('x_445906_your_data_demo2');

fields.addQuery('sysid','3D1c4aa3702feb48108490dcb6f699b621'); // 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

}

 

Thankyou

Like I mentioned: what is functionally what you are trying to achieve?

If my answer helped you in any way, please then mark it as helpful.

Kind regards,
Mark
2020 ServiceNow Community MVP
2020 ServiceNow Developer MVP

---

LinkedIn
Community article list

 

Kind regards,

 

Mark Roethof

Independent ServiceNow Consultant

10x ServiceNow MVP

---

 

~444 Articles, Blogs, Videos, Podcasts, Share projects - Experiences from the field

LinkedIn

Harsh Vardhan
Giga Patron

i think you have already opened a thread for same question. please don't open duplicate thread , reply at one place so it will be easy for us to help you. 

 

https://community.servicenow.com/community?id=community_question&sys_id=2e8b1b78db6f80d02be0a851ca96...