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.

Comparing values in two tables

Prashant Kumar6
Giga Guru

Hello Experts,
I have two tables :
1: "sys_atf_test" where I have field called "Name" string type.
2: "tm_test_case" where I have field called "ATF Test Name" string type.
I want to check if there is any common values in both table fields through script ,Also I want to print the value which is common in both table fields. Any help ?Thanks. 

 

1 ACCEPTED SOLUTION

@Prashant Kumar6 Try with following.

 

var glideATFTest = new GlideRecord('sys_atf_test');
glideATFTest.query();
while(glideATFTest.next){
var glideTestCase = new GlideRecord('tm_test_case');
glideTestCase.addEncodedQuery('u_atf_test_nameLIKE'+glideATFTest.getDisplayValue('name'));
glideTestCase.query();
if(glideTestCase.next()){
gs.info('Found a duplicate name '+glideTestCase.getValue('u_atf_test_name'));
}
}

Also, in order to find the correct query, I recommend you to do a manual search on u_atf_test_name field on list view of sys_atf_test table, copy the encoded query and use the same here in this script.

 

 

View solution in original post

5 REPLIES 5

Sandeep Rajput
Tera Patron
Tera Patron

@Prashant Kumar6 You can use the following script to check the common names.

 

var glideATFTest = new GlideRecord('sys_atf_test');
glideATFTest.query();
while(glideATFTest.next){
var glideTestCase = new GlideRecord('tm_test_case');
glideTestCase.addQuery('atf_test_name',glideATFTest.getValue('name'));//Replace atf_test_name with your field name in tm_test_case 
glideTestCase.query();
if(glideTestCase.next()){
gs.info('Found a duplicate name '+glideTestCase.getValue('atf_test_name'));//Replace atf_test_name with your field name in tm_test_case 
}
}

Hope this helps.

Thanks @Sandeep Rajput : Let me check and will get back to you . 

Hi @Sandeep Rajput : I tried and I am getting desired output as I know there are some duplicate records but it is not finding that :

PrashantKumar6_0-1696849409611.png

 

@Prashant Kumar6 Try with following.

 

var glideATFTest = new GlideRecord('sys_atf_test');
glideATFTest.query();
while(glideATFTest.next){
var glideTestCase = new GlideRecord('tm_test_case');
glideTestCase.addEncodedQuery('u_atf_test_nameLIKE'+glideATFTest.getDisplayValue('name'));
glideTestCase.query();
if(glideTestCase.next()){
gs.info('Found a duplicate name '+glideTestCase.getValue('u_atf_test_name'));
}
}

Also, in order to find the correct query, I recommend you to do a manual search on u_atf_test_name field on list view of sys_atf_test table, copy the encoded query and use the same here in this script.