Encoded Query & Add Or Condition query

inderpal
Kilo Expert

Hi All,

I am stuck on a scenario where I need to query records from a table with the combination of one encoded query saved in a field or on the basis my static query.

For ex: I need to fetch incident tickets where active is true or my encoded query matches.

I tried following query but it seems addEncodedQuery method doesn't work with addOrCondition method.

current.addEncodedQuery("short_description=Test").addOrCondition("active","true");

 

Does anyone know how to make both Encoded query & addOrCondition working together? Please HELP!!

 

Thanks in advance,

Inderpal Singh

ipsindhuria1992@live.com

 

8 REPLIES 8

Upender Kumar
Mega Sage

Hi,

Create query in querybuilder and copy from there and paste in your code.

 

find_real_file.png

 

Thanks

inderpal
Kilo Expert

Thanks everyone for their help.

I guess I was not able to explain the use case.

Anyways, I have solved the issue by adding extra function in my query.

current.addEncodedQuery('active=true^NQcaller_idSTARTSWITHLHR User^current.getEncodedQuery()')

'^'+current.getEncodedQuery() is the key here.

 

Thanks 🙂

Swarup Patra
Kilo Guru

Sure, you can use the addQuery() method instead of addEncodedQuery() to achieve this. Here's how you can do it:

1. First, create a GlideRecord object for the table you want to query. For example, if you're querying the incident table, you would do:

javascript
var gr = new GlideRecord('incident');


2. Next, use the addQuery() method to add your first condition. For example, if you want to find records where the short description is "Test", you would do:

javascript
gr.addQuery('short_description', 'Test');


3. Then, use the addOrCondition() method to add your second condition. For example, if you want to find records where active is true, you would do:

javascript

gr.addOrCondition('active', true);


4. Finally, use the query() method to execute the query and the next() method to iterate through the results. For example:

javascript
gr.query();
while (gr.next()) {
// do something with gr, which now represents a record that matches your query
}


So, your final code would look something like this:

javascript
var gr = new GlideRecord('incident');
gr.addQuery('short_description', 'Test');
gr.addOrCondition('active', true);
gr.query();
while (gr.next()) {
// do something with gr
}


This code will return all records from the incident table where the short description is "Test" or active is true.


nowKB.com

If you want to know any information about Service Now . Visit to https://nowkb.com/home