gliderecord two tables

gabriel_sk
Tera Contributor

Hello:)

I am trying to query a table by comparing 2 fields which are not on the same table using GlideRecord. Some of these 2 string fields contain identical data.

The result should display the records of the table2 that don't have matching entry on table1 for the specific field I am trying to query against.

Basically smth like this... but does not work

function onetest1() {

table1= new GlideRecord('u_table1');

table2= new GlideRecord('u_table2');

table2.addEncodedQuery('field2NSAMEAStable1.field1')

table2.query();

while (table2.next()){

gs.print(table2.field2);

}

}

If I query against 2 fields on the same table, the script works just fine...

I am testing it on Scripts Background... trying to learn how to work with GlideRecord

Any suggestion will be appreciated.

Thanks a lot:)

1 ACCEPTED SOLUTION

Pradeep Sharma
ServiceNow Employee
ServiceNow Employee

Hello Gabriel,



Please go through the below script for reference. The below script will compare short_description field of incident and problem and return short description of problem records that are not matched.


var arr = [];


onetest1();


function onetest1() {    


var gr= new GlideRecord('incident');   //Pass table 1 name here


gr.addNotNullQuery('short_description'); //Pass field name here


gr.query();


while(gr.next())


{


arr.push(gr.short_description.toString()); //Replace short_description with the exact field name to be matched


}


}


for(i=0;i<arr.length;i++)


{




var gr1 = new GlideRecord('problem'); //Pass table name 2 here


gr1.addQuery('short_description',arr[i]); //Pass table name 2 field column name that has to be matched with table name 1 field


gr1.query();


while(!gr1.next())


{


gs.addInfoMessage(gr1.short_description); //Replace short_description with the exact field name here


}


}  








Reference:


http://wiki.servicenow.com/index.php?title=GlideRecord


View solution in original post

5 REPLIES 5

Shishir Srivast
Mega Sage

Can we try like:



function onetest1() {  


var nonmatchingVal = [], encodedquerystr = '';


var table1= new GlideRecord('u_table1');


table1.addNotNullQuery('field_name');


table1.query();


while(table1.next()){


var table2 = new GlideRecord('u_table2');  


if(table2.get('field_name', table1.field_name))


encodedquerystr = encodedquerystr + 'field_name!=' + table1.field_name + '^OR';


}


encodedquerystr = encodedquerystr.substring(0, encodedquerystr.length - 3).trim();


var table = new GlideRecord('u_table2');


table.addEncodedQuery(encodedquerystr);


table.query();


while(table.next())


nonmatchingVal.push(table.field_name.toString());


gs.print(nonmatchingVal);


}