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

@imran rasheed 

so query is correct but for your persona it's not getting applied properly?

can you give your complete business requirement here

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

Requirement is to restrict access to table records.
When logged in user is assigned person of ticket, then show those tickets.

When logged in user is opened by person of ticket, then show those tickets.

When logged in user is part of certain group, then show tickets where parent ticket, service owner of ticket is abc for example.

 

 

it seems to work when I try to push like below.

 var encodedQueries = []; 
var currentUser = gs.getUserID(); 
 
 
    encodedQueries.push("assigned_to=" + currentUser);
    encodedQueries.push("opened_by=" + currentUser);
 
 
 if (gs.getUser().isMemberOf("ABC")) {
        encodedQueries.push("parent..u_service_owner=2122342312");
    }

    if (gs.getUser().isMemberOf("DEF")) {
        encodedQueries.push("parent..u_service_owner=232323432");
    }
 // Combine all encoded queries with OR operator
    if (encodedQueries.length > 0) {
        var combinedQuery = encodedQueries.join('^OR');
        gs.addInfoMessage("Complete query: " + combinedQuery);
        current.addEncodedQuery(combinedQuery);

@imran rasheed 

since you said it seems to work

then in which case it's not working?

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

Instead of using addOr condition like below

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

if (qc.hasCondition()) {
    var fallbackQuery = "assigned_to=" + current.getValue("sys_id") + "^ORopened_by=" + current.getValue("sys_id");
    encodedQueryParts.push(fallbackQuery);
}

 Tried to have it separately, it seems to work.

 

 

var currentUser = gs.getUserID(); 
    encodedQueries.push("assigned_to=" + currentUser);
    encodedQueries.push("opened_by=" + currentUser);

Sarthak Kashyap
Mega Sage

Hi @imran rasheed ,

 

Can you please try with below script 

 

var encodedQueries = [];
    var userId = gs.getUserID();

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

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

    // Assignment fallback logic
    encodedQueries.push(
        "assigned_to=" + userId + "^ORopened_by=" + userId
    );

gs.log("encodedQueries = " + encodedQueries);

    // Combine with OR
    if (encodedQueries.length > 0) {
gs.log("Inside IF = " + encodedQueries.length);
        var combinedQuery = encodedQueries.join("^OR");
gs.log("combinedQuery = " + combinedQuery);
        gs.addInfoMessage("Complete query: " + combinedQuery);
        current.addEncodedQuery(combinedQuery);
    }

 

Also check the values coming in the log.

 

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

Thanks and Regards,

Sarthak