GlideRecord Query: need condition set

MichaelZischeck
Kilo Sage

when I add conditions in platform I get this for query:

short_descriptionSTARTSWITHTest
^NQsys_tags.b112ee371bd5c2101ff2dc6a9b4bcb89=b112ee371bd5c2101ff2dc6a9b4bcb89
^sys_tags.b635774d1bed0a101ff2dc6a9b4bcbd4=b635774d1bed0a101ff2dc6a9b4bcbd4

 

it returns 2 records, one where short description starts with Test and the other one which has to tags assigned.

 

i'd like to find the same two records with script, but as you can imagine: dynamically. I do NOT want to use the query from platform as for addEncodedQuery

 

i'd like to be able to "build" the query the same way.

I am going nuts here, once more.

I can find either one or the other record by using

either:

grKnow.addQuery('short_description','Test');

or:

grKnow.addQuery('sys_tags.b112ee371bd5c2101ff2dc6a9b4bcb89','b112ee371bd5c2101ff2dc6a9b4bcb89');
grKnow.addQuery('sys_tags.b635774d1bed0a101ff2dc6a9b4bcbd4','b635774d1bed0a101ff2dc6a9b4bcbd4');
 
I read about var qc = grKnow.addQuery(...)
then qc.addOrCondition, but this does not return the NQ see above, it returns OR, and somehow this doesn't work
 
I believe the addOrCondition does what it says: add to the last condition an OR condition.
 
what i'm looking for is NQ which would OR all previous conditions
 
the use case:
in customer service workspace i'd like to return knowledge articles based on either or conditions from the case at hand.
 
1 ACCEPTED SOLUTION

Deepak Shaerma
Kilo Sage

Hi @MichaelZischeck 
Conceptual script to use direct addencodedquery

var grKnow = new GlideRecord('your_table_name');

// Start the first query
grKnow.addQuery('short_description', 'STARTSWITH', 'Test');

// Since we cannot directly chain ‘NQ’, we execute and store these records
var matchingRecords = [];
grKnow.query();
while (grKnow.next()) {
    matchingRecords.push(grKnow.getUniqueValue());
}

// Begin a new query for the tag conditions
var grKnowTags = new GlideRecord('your_table_name');
grKnowTags.addQuery('sys_tags.b112ee371bd5c2101ff2dc6a9b4bcb89', 'b112ee371bd5c2101ff2dc6a9b4bcb89');
grKnowTags.addQuery('sys_tags.b635774d1bed0a101ff2dc6a9b4bcbd4', 'b635774d1bed0a101ff2dc6a9b4bcbd4');

grKnowTags.query();
while (grKnowTags.next()) {
    var uniqueId = grKnowTags.getUniqueValue();
    if (!matchingRecords.includes(uniqueId)) {
        matchingRecords.push(uniqueId);
    }
}

// At this point, ‘matchingRecords’ contains the unique ID of all records matching either condition set.

// To perform operations on these records, iterate through ‘matchingRecords’ IDs, retrieve and manipulate as needed
matchingRecords.forEach(function(recordId) {
    var gr = new GlideRecord('your_table_name');
    if (gr.get(recordId)) {
        // Perform your operations on each matching record
    }
});

Please Mark this Helpful and Accepted Solution. If this Helps you to understand. This will help both the community and me..
- Keep Learning ‌‌
Thanks & Regards 
Deepak Sharma 

View solution in original post

6 REPLIES 6

Deepak Shaerma
Kilo Sage

Hi @MichaelZischeck 
Conceptual script to use direct addencodedquery

var grKnow = new GlideRecord('your_table_name');

// Start the first query
grKnow.addQuery('short_description', 'STARTSWITH', 'Test');

// Since we cannot directly chain ‘NQ’, we execute and store these records
var matchingRecords = [];
grKnow.query();
while (grKnow.next()) {
    matchingRecords.push(grKnow.getUniqueValue());
}

// Begin a new query for the tag conditions
var grKnowTags = new GlideRecord('your_table_name');
grKnowTags.addQuery('sys_tags.b112ee371bd5c2101ff2dc6a9b4bcb89', 'b112ee371bd5c2101ff2dc6a9b4bcb89');
grKnowTags.addQuery('sys_tags.b635774d1bed0a101ff2dc6a9b4bcbd4', 'b635774d1bed0a101ff2dc6a9b4bcbd4');

grKnowTags.query();
while (grKnowTags.next()) {
    var uniqueId = grKnowTags.getUniqueValue();
    if (!matchingRecords.includes(uniqueId)) {
        matchingRecords.push(uniqueId);
    }
}

// At this point, ‘matchingRecords’ contains the unique ID of all records matching either condition set.

// To perform operations on these records, iterate through ‘matchingRecords’ IDs, retrieve and manipulate as needed
matchingRecords.forEach(function(recordId) {
    var gr = new GlideRecord('your_table_name');
    if (gr.get(recordId)) {
        // Perform your operations on each matching record
    }
});

Please Mark this Helpful and Accepted Solution. If this Helps you to understand. This will help both the community and me..
- Keep Learning ‌‌
Thanks & Regards 
Deepak Sharma 

interesting approach. thank you.

However, maybe I wasn't clear enough about that fact:

I need to return a "query" for the Filter configuration on related search.

I could, I assume, read all sys_ids of the found records and construct a query like sys_id in 'a,b,c,d,e'

 

If there's no better solution i'll accept your's as solution..

 

 

Maddysunil
Kilo Sage

@MichaelZischeck 

To dynamically build a query in script to achieve the same effect as the platform query you mentioned, you can use addOrCondition() method along with addQuery() method.

 

// Initialize a GlideRecord object for the table you're querying
var grKnow = new GlideRecord('knowledge');

// Create a new query condition
var qc = grKnow.addQuery('short_description', 'STARTSWITH', 'Test');

// Add OR conditions for the tags
var tag1 = qc.addOrCondition('sys_tags.b112ee371bd5c2101ff2dc6a9b4bcb89', 'b112ee371bd5c2101ff2dc6a9b4bcb89');
var tag2 = qc.addOrCondition('sys_tags.b635774d1bed0a101ff2dc6a9b4bcbd4', 'b635774d1bed0a101ff2dc6a9b4bcbd4');

// Execute the query
grKnow.query();

// Iterate through the results
while (grKnow.next()) {
    // Access each record as needed
}

 

Please Mark Correct if this solves your query and also mark 👍Helpful if you find my response worthy based on the impact.

 

Thanks

your approach I did check already, in literally every combination. doesn't work

it always results in "^OR" and not in "^NQ".

interesting fact:

 

if I use:

var qc =

var tag1 =

tag1.addCondition

 

it returns the "tagged" document

 

if I use:

var qc = 

var tag1 =

grKnow.addQuery

it returns the "Test" record

 

YET: and this is unbelievable. the encodedQuery for both is identical