We're reclaiming inactive PDIs to keep them available for active builders. Learn what's changing, who's affected, and how to protect your work. Read More

Query business rule help

imran rasheed
Tera Contributor

I am trying to run a query business rule to check on multiple conditions which works one way or the other.

If I run the Ismemberof condition or the qc and/or condition within query BR separately it works. But when I try to combine both to check, only the qc condition works and does not include the ismemberof in encoded query.

 

 

 if (gs.getUser().isMemberOf("ABC")) {
        encodedQueries.push("parent..u_service_owner=2122342312");
    }

    if (gs.getUser().isMemberOf("DEF")) {
        encodedQueries.push("parent..u_service_owner=232323432");
    }
 var qc = current.addQuery("assigned_to", currentUser);
    qc.addOrCondition("opened_by", currentUser);
    if (qc && qc.hasCondition()) {
        var fallbackQuery = "assigned_to=" + currentUser + "^ORopened_by=" + currentUser;
        encodedQueries.push(fallbackQuery);
    }

    // Combine all encoded queries with OR operator
    if (encodedQueries.length > 0) {
        var combinedQuery = encodedQueries.join('^OR');
        gs.addInfoMessage("Complete query: " + combinedQuery);
        current.addEncodedQuery(combinedQuery);
12 REPLIES 12

Ankur Bawiskar
Tera Patron

@imran rasheed 

check this link

Using operator (^NQ) in encoded queries causes incorrect reference links in the list view 

try this once

var encodedQueryParts = [];

if (gs.getUser().isMemberOf("ABC")) {
    encodedQueryParts.push("parent.u_service_owner=2122342312");
}

if (gs.getUser().isMemberOf("DEF")) {
    encodedQueryParts.push("parent.u_service_owner=232323432");
}

var qc = current.addQuery("assigned_to", current.getValue("sys_id")); // comparison with user sys_id
qc.addOrCondition("opened_by", current.getValue("sys_id"));

// Combine 'assigned_to' and 'opened_by' with OR explicitly
if (qc.hasCondition()) {
    var fallbackQuery = "assigned_to=" + current.getValue("sys_id") + "^ORopened_by=" + current.getValue("sys_id");
    encodedQueryParts.push(fallbackQuery);
}

// Join all parts with OR and apply as encoded query
if (encodedQueryParts.length > 0) {
    var combinedEncodedQuery = encodedQueryParts.join("^OR");
    gs.addInfoMessage("Combined Query: " + combinedEncodedQuery);
    current.addEncodedQuery(combinedEncodedQuery);
}

💡 If my response helped, please mark it as correct and close the thread 🔒— this helps future readers find the solution faster! 🙏

Regards,
Ankur
Certified Technical Architect  ||  10x ServiceNow MVP  ||  ServiceNow Community Leader

Thanks for the response Ankur. But the script which you have given is same as what I have mentioned in the question. What is the difference?

@imran rasheed 

Sorry I missed that

I believe your script is using that NQ in query business rule which gives inconsistent result

please check the KB link I shared

💡 If my response helped, please mark it as correct and close the thread 🔒— this helps future readers find the solution faster! 🙏

Regards,
Ankur
Certified Technical Architect  ||  10x ServiceNow MVP  ||  ServiceNow Community Leader

I checked the kb article.
But ultimately I get the correct encoded query result in gs.addInfoMessage("Complete query: " + combinedQuery); where I am trying to print.