Does the sequence matter? addQuery()

ServiceNow Use6
Tera Guru

Hi,

I wote this code, but it gives the wrong input, but when I change the sequence, it works fine. Can you please provide me with insight?

 

var gr = new GlideRecord('sys_user');
gr.addQuery(producer.email, 'email');
gr.query();
if(gr.next()){
	current.u_employee = gr.sys_id;
}

 

It works for this

 

var gr = new GlideRecord('sys_user');
gr.addQuery( 'email', producer.email);
gr.query();
if(gr.next()){
	current.u_employee = gr.sys_id;
}

 

Regards

Suman P.

2 ACCEPTED SOLUTIONS

Ehab Pilloor
Mega Sage

Hi @ServiceNow Use6,

Yes, sequence matters as first parameter ('email') should be a field on the GlideRecord table and added as a string in the script.

The second parameter is value stored in the field and can be string or dynamic (which is in your case, i.e. producer.email).

That's why the second version works: 'email' is the correct field name, and producer.email is the value you're querying. Always follow addQuery('field_name', value) — field name first, value second

 

Regards,

Ehab Pilloor

View solution in original post

Ankur Bawiskar
Tera Patron
Tera Patron

@ServiceNow Use6 

Yes and that's how addQuery() works.

1st parameter is always the field name either a direct field of table or a dot walked field

2nd parameter is always the value to be compared.

check docs for more help

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

View solution in original post

2 REPLIES 2

Ehab Pilloor
Mega Sage

Hi @ServiceNow Use6,

Yes, sequence matters as first parameter ('email') should be a field on the GlideRecord table and added as a string in the script.

The second parameter is value stored in the field and can be string or dynamic (which is in your case, i.e. producer.email).

That's why the second version works: 'email' is the correct field name, and producer.email is the value you're querying. Always follow addQuery('field_name', value) — field name first, value second

 

Regards,

Ehab Pilloor

Ankur Bawiskar
Tera Patron
Tera Patron

@ServiceNow Use6 

Yes and that's how addQuery() works.

1st parameter is always the field name either a direct field of table or a dot walked field

2nd parameter is always the value to be compared.

check docs for more help

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