logo

NJP

Create a Scheduled Job which matches items from two tables and writes to a third table

Import · Aug 17, 2022 · article

(function() {

var table1 = new GlideRecord('x_table_1');

table1.addEncodedQuery('copy Query from show/hide filter'); // This is optional if you want to filter for specific records in a large table to save time.

table1.query();

while (table1.next()) {

var table2 = new GlideRecord('x_table_2');

table2.addEncodedQuery('copy Query from show/hide filter'); // This is optional if you want to filter for specific records in a large table to save time.

table2.query();

while (table2.next()) {

// If there is a common field between the two tables use that. In my case a child and parent relationship

if (table1.u_field_x == table2.u_field_y) {

var table3 = new GlideRecord('x_table_3');

table3.setValue('u_field_1', table1.getUniqueValue('u_reference_field'));

table3.setValue('u_field_2', table1.u_string_field);

table3.setValue('u_field_3', table2.getDisplayValue('u_name_field'));

}

}

}

})();

View original source

https://www.servicenow.com/community/developer-articles/create-a-scheduled-job-which-matches-items-from-two-tables-and/ta-p/2298802