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.

Two glide record conditions

Kush Sharma
Tera Guru

Guys - I have a requirement to search data in two tables in ServiceNow .

Example - I need to check value of customer_contact field in (sys_user) table if value found that's fine but if not then I need to check another table (customer_account) for field u_contact . 

Please help me , how to write two glide records in same function based on condition.

 

Thanks

1 ACCEPTED SOLUTION

Ankur Bawiskar
Tera Patron
Tera Patron

Hi,

it would look like this; enhance it from your side.

var gr = new GlideRecord("sys_user");
gr.addQuery("customer_contact", "your value");
gr.query();
if (gr.next()) {
	// do something
}
else{
	var rec = new GlideRecord("customer_account");
	rec.addQuery("u_contact", "your value");
	rec.query();
	if(rec.next()){
		// do something
	}
}

Regards
Ankur

Regards,
Ankur
Certified Technical Architect  ||  9x ServiceNow MVP  ||  ServiceNow Community Leader

View solution in original post

3 REPLIES 3

Yousaf
Giga Sage

Hi Kush,

 

You can GlideRecord one table and use if else and GlideRecord second table in else statement.

var gr = new GlideRecord('sys_user');
gr.addQuery();
gr.query();
if(gr.next){  //if or while
    //do this
}
else{
    var gr1 = new GlideRecord('customer_account');
    gr1.addQuery('u_contact','yourValue');
    //query
    //if or while{

    }
}

 

Mark Correct or Helpful if it helps.


***Mark Correct or Helpful if it helps.***

Mohith Devatte
Tera Sage
Tera Sage

Hello @Kush Sharma ,

you can try this 

var gr = new GlideRecord('sys_user');

gr.addQuery('customer_contact','<your_value>');

gr.query();

if(!gr.next())

{

var cus = new GlideRecord('customer_account');

cus.addQuery('u_contact','<your_value>');

cus.query();

if(cus.next())

{

//execute your logic

}
}
else
{
//execute your logiv
}

MARK MY ANSWER CORRECT IF IT HELPS YOU

Ankur Bawiskar
Tera Patron
Tera Patron

Hi,

it would look like this; enhance it from your side.

var gr = new GlideRecord("sys_user");
gr.addQuery("customer_contact", "your value");
gr.query();
if (gr.next()) {
	// do something
}
else{
	var rec = new GlideRecord("customer_account");
	rec.addQuery("u_contact", "your value");
	rec.query();
	if(rec.next()){
		// do something
	}
}

Regards
Ankur

Regards,
Ankur
Certified Technical Architect  ||  9x ServiceNow MVP  ||  ServiceNow Community Leader