I have to convert AND and OR filters into Query operator
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
‎07-26-2017 08:17 AM
I have to convert AND and OR filters into Query operator, Can anyone please tell me what would be string for that?
- Labels:
-
Scripting and Coding
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
‎07-26-2017 08:37 AM
Hello Yasmin,
Check below. You can find more examples in wiki.
Scripting an OR Condition
An OR condition can be added to any query part within a business rule with the addOrCondition() method. The example below shows a query for finding all the incidents that have either a 1 or a 2 priority. The first addQuery() condition is defined as a variable and is used in the OR condition.
var inc = new GlideRecord('incident');
var qc = inc.addQuery('priority', '1');
qc.addOrCondition('priority', '2');
inc.query();
while (inc.next())
{ // processing for the incident goes here }
The following script is a more complex example, using two query condition variables doing the equivalent of (priority = 1 OR priority = 2) AND (impact = 2 OR impact = 3). The results of the OR condition are run with two variables, qc1and qc2. This allows you to manipulate the query condition object later in the script, such as inside an IF condition or WHILE loop.
var inc = new GlideRecord('incident');
var qc1 = inc.addQuery('priority', '1');
qc1.addOrCondition('priority', '2');
var qc2 = inc.addQuery('impact', '2');
qc2.addOrCondition('impact', '3');
inc.query();
while (inc.next()) { // processing for the incident goes here }
Thanks.
PS: Hit like, Helpful, Correct and Endorse, if it answers your question.
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
‎07-26-2017 08:38 AM
Hello Yasmin,
SQL queries are pretty straightforward and quite easy to read, understand and write (in most cases).
Here are some resources that could be useful to your case:
- SQL Syntax: https://www.w3schools.com/sql/sql_syntax.asp
- SQL AND, OR and NOT operators: https://www.w3schools.com/sql/sql_and_or.asp
- Operator Precedence in MySQL: https://dev.mysql.com/doc/refman/5.7/en/operator-precedence.html
Here is an example query that shows all the columns of the records from the "suppliers" table where the "state" field has value equals to "California" and the "supplier_id" is different from 900, or the "supplier_id" is equal to 100.
SELECT *
FROM suppliers
WHERE (state = 'California' AND supplier_id <> 900)
OR (supplier_id = 100);
As you can see, the OR operator separates two conditions:
- "state" field has value equals to "California" and the "supplier_id" is different from 900
- "supplier_id" is equal to 100
I hope these meet your needs.
Feel free to ask more and hit the like button below if this is the correct answer
Bye bye!
Francesco
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
‎07-26-2017 12:11 PM
Hi Yasmin,
This may help you, if you define AND and ORs in a list filter and check the result set is in line with expectation, you can snarf an encoded query string for reuse.
Encoded Query Strings - ServiceNow Wiki
..
3 Generating Encoded Query Strings through a Filter
You can generate an encoded query string through a filter on any list and paste the string into a URL query or a reference qualifier.
- Open a list of records.
- Construct the filter.
- Click Run.
- Right-click the end of the filter breadcrumbs and select Copy query from the context menu.
- Use your computer's keyboard copy command to copy the query in the dialog box to your system clipboard.
- Click OK to close the dialog box.
- Use the query string to construct a URL or an advanced reference qualifier.
3.1 Using the CONTAINS Operator
When you use the CONTAINS operator on a list filter, the system translates the filter to a LIKE query. For example, if you filter for active records with numbers that contain 123, the URL is https://InstanceName.service-now.com/incident_list.do?sysparm_query=active%3Dtrue%5EGOTOnumberLIKE12...
If the reply was informational, please like, mark as helpful or mark as correct!