Use GlideRecord() in function field

MiY
Tera Contributor

Hi team,
I want to use GlideRecord() method in function definition field because need to query other table and get specific field value.

Detail requirements follows;

Create a field 1 (<-function field) in table A.

I want to set field 1 to part of table B's field 2.

table A is referenced by table B's field 3.

 

Can I build this function?

Please help me.

 

Regards,

 

14 REPLIES 14

Ravi Gaurav
Giga Sage
Giga Sage

Hi @MiY 

Your Requirement

  • Table A → has a new field (field_1) which is a function field.

  • Table B → has:

    • field_2 (whose value you want to show in Table A’s function field).

    • field_3 (reference to Table A).

So essentially:

  • Each record in Table B points to a record in Table A via field_3.

  • In Table A, you want field_1 (function field) to return the value of Table B.field_2.

    Example of BR

(function executeRule(current) {

var grB = new GlideRecord('u_table_b'); // Table B
grB.addQuery('u_field_3', current.sys_id); // Match where field_3 points to this Table A record
grB.query();

if (grB.next()) {
return grB.u_field_2; // return field_2 value
}

return ''; // nothing found

})(current);

--------------------------------------------------------------------------------------------------------------------------


If you found my response helpful, I would greatly appreciate it if you could mark it as "Accepted Solution" and "Helpful."
Your support not only benefits the community but also encourages me to continue assisting. Thank you so much!

Thanks and Regards
Ravi Gaurav | ServiceNow MVP 2025,2024 | ServiceNow Practice Lead | Solution Architect
CGI
M.Tech in Data Science & AI

 YouTube: https://www.youtube.com/@learnservicenowwithravi
 LinkedIn: https://www.linkedin.com/in/ravi-gaurav-a67542aa/

MiY
Tera Contributor

@Ravi Gaurav -san,
Thank you, I understand that this can be achieved by creating a Business Rule.
What I'm concerned about are records that were created before this implementation.
This requirement is occurring for an operational table that is already GoLive.
As a result, some records have already been created.
If this requirement could be achieved with a function field, it would be possible to fulfill this requirement without updating the records that have already been created.
However, I understand that with Business Rules, this implementation does not apply to records that have already been created.
Could you please tell me if there is a way to apply this change to past records even when using Business Rules?

 

Regards,

GlideFather
Tera Patron

Hi @MiY,

 

where do you want to create that function? Business rule, script include, flow, .....?

———
/* If my response wasn’t a total disaster ↙️ drop a Kudos or Accept as Solution ↘️ Cheers! */


Ankur Bawiskar
Tera Patron
Tera Patron

@MiY 

report function field doesn't allow to use GlideRecord.

You can only use pre-defined functions which ServiceNow allows.

Why not join both the tables using database view and then have report on that database view?

With this your requirement will be achieved.

If my response helped please mark it correct and close the thread so that it benefits future readers.

Regards,
Ankur
Certified Technical Architect  ||  9x ServiceNow MVP  ||  ServiceNow Community Leader