Join the #BuildWithBuildAgent Challenge! Get recognized, earn exclusive swag, and inspire the ServiceNow Community with what you can build using Build Agent.  Join the Challenge.

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,

 

15 REPLIES 15

Bhimashankar H
Mega Sage

Hi @MiY ,

 

As per understanding, In Table A’s function field, retrieve and display the value of Table B’s Field 2, where Table B’s Field 3 references the current Table A record.

 

//This is for BR
//If you want for script include, please iterate records from table A first and replace current with table A's record
// 'current' refers to the record in Table A

var result = ''; // Default value (in case no match found)

// Query Table B where field_3 references current record in Table A
var grB = new GlideRecord('table_b'); // Replace 'table_b' with actual Table B name
grB.addQuery('field_3', current.sys_id); // Replace 'field_3' with actual field name referencing Table A
grB.query();
if (grB.next()) {
    result = grB.getValue('field_2'); // Replace 'field_2' with actual Table B field name
}

answer = result;

 

This function runs for each record in Table A. It finds the first Table B record where Table B’s Field 3 points to the current Table A record. It sets Table A’s Field 1 to Table B’s Field 2 value (or blank/empty if none found).

 

Replace the table and field names with your actual table names.

 

Thanks,
Bhimashankar H

 

-------------------------------------------------------------------------------------------------
If my response points you in the right directions, please consider marking it as 'Helpful' & 'Correct'. Thanks!

Hi @MiY ,

 

I hope you saw my reply. 


If my response points you in the right directions, please consider marking it as 'Helpful' & 'Correct'. It will help future readers as well having similar kind of questions and close the thread.

Thanks,
Bhimashankar H

@Bhimashankar H -san,

@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,

Hi @MiY ,

 

If you implement this solution then you need to write a fix script to apply the functionality for previous records.

 

Form the query that will give you records which does not have this functionality then process those records and update in script. This way this can be moved to PROD as well from DEV.

 

Thanks,
Bhimashankar H

-------------------------------------------------------------------------------------------------
If my response points you in the right directions, please consider marking it as 'Helpful' & 'Correct'. Thanks!

Bhuvan
Giga Patron

@MiY 

 

Function field defined inside reports cannot be accessed from server side scripts. Create a new field in Table A that will store the value rather than function field and use gliderecord as per requirements. 

 

If you do not want to create a new field at table level, create a database view if number of records will not grow exponentially.

 

If this helped to answer your query, please mark it helpful & accept the solution.

 

Thanks,

Bhuvan