How to add "AND" condition in addquery()

Manikandan2
Kilo Expert

Hi,

The below script is not working properly. In my script i want to add query with "AND" condition to check multiple fields like 

  • When state is 6,7,8
  • u_is_planned = Unplanned
  • Service Offering = Offername

If all the conditions are satisfied only then i want to update the description field. I know there is a syntax error in the below query. What correction i would need to do ?

gr = new GlideRecord('incident');
gr.addQuery('stateNOT IN6,7,8^u_is_planned=Unplanned^cmdb_ci.u_service_offering.u_offer_name=' + offername + '^ORDERBYsys_updated_on');
gr.query();
while (gr.next()) {
gr.description = gr.description + '.';
gr.update();
}

1 ACCEPTED SOLUTION

Manikandan2
Kilo Expert

For some reason the addEncodedquery() did not work however splitting the queries and executing worked fine 🙂 

View solution in original post

7 REPLIES 7

Mark Roethof
Tera Patron
Tera Patron

Hi there,

Since you are already using:

gr.addQuery('stateNOT IN6,7,8^u_is_planned=Unplanned^cmdb_ci.u_service_offering.u_offer_name=' + offername + '^ORDERBYsys_updated_on');

Why not use:

addEncodedQuery

Kind regards,
Mark

 

Kind regards,

 

Mark Roethof

Independent ServiceNow Consultant

10x ServiceNow MVP

---

 

~444 Articles, Blogs, Videos, Podcasts, Share projects - Experiences from the field

LinkedIn

This way you could also easily generate breadcrumb filters and copy the queries. Almost copy-pasting to get your addQuery running 🙂

find_real_file.png

If my answer helped you in any way, please then mark it as helpful.

Kind regards,
Mark

 

Kind regards,

 

Mark Roethof

Independent ServiceNow Consultant

10x ServiceNow MVP

---

 

~444 Articles, Blogs, Videos, Podcasts, Share projects - Experiences from the field

LinkedIn

Harsh Vardhan
Giga Patron

gr = new GlideRecord('incident');
gr.addEncodedQuery('stateNOT IN6,7,8^u_is_planned=Unplanned^cmdb_ci.u_service_offering.u_offer_name=' + offername + '^ORDERBYsys_updated_on');
gr.query();
while (gr.next()) {
gr.description = gr.description + '.';
gr.update();
}

 

try now

Calvaresi E_
Giga Expert

For AND conditions, just write another line with addQuery method.
For OR conditions, you can concatenate gr.addQuery(xx).addOrCondition(yy).
Otherwise you can use addEncodedQuery to write every condition in a single line, like:
gr.addEncodedQuery('u_configuration_itemIN' + current.u_configuration_item.toString());

Let me know if it helps.