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

Community Alums
Not applicable

Hi @MichaelZischeck ,

In that case you can use gr.addOrCondition()  this will work for you, you can refer below code for reference 

var qc = gr.addQuery('short_description', 'Test);

qc.addOrCondition('sys_tags.b', <sys_id here>);

qc.addOrCondition('sys_tags.b', <sys_id here>);

 

Please mark my answer correct and helpful if this works for you

Thanks and Regards 

Sarthak

unfortunately it won't... add OR Condition will always ^OR, I need ^NQ.

It's 2 totally unrelated queries and I need the results of both