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

@MiY 

Hope you are doing good.

Did my reply answer your question?

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

@Ankur Bawiskar -san,

 

Thank you for your reply.

My ultimate goal is to use AWS AppFlow to link data from ServiceNow Table A with Table B's creation date.
I created a database view, but I was unable to select it in AWS AppFlow.
Therefore, as shown above, I need to create a field in Table A to display the creation date of Table B and display it.
I think I can meet the requirement by creating a business rule, but this table is already in production.
Therefore, the field will be empty for records created before this implementation.
To set a value in the field, I need to update the created records as a developer.
I think this should be avoided from a security perspective.
If the requirement could be met with a function field, the value would automatically be reflected in the field of the created record.
Therefore, if possible, I would like to achieve the requirement with a function field.
Is there a good way to do this? Please help!

 

Regards,

 

@MiY 

not possible.

If you are planning to use a new field then you can populate that with a fix script after you migrate your update set.

Then this field will have data in it.

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

@MiY 

Hope you are doing good.

Did my reply answer your question?

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

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!