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

I had the logs where it showed up the exact query result based on condition.

ChallaR
Giga Guru

HI @imran rasheed ,

 

i have updated the script please add more if condition if you have more .

// Type: Query Business Rule (e.g., beforeQuery on your target table)
// Condition: Active = true
(function executeRule(current, previous /* null when async */) {

    // Use the user's sys_id for reliable comparisons
    var currentUser = gs.getUserID();

    // Start the first OR branch: user-related fields
    var userBlock = current.addQuery('assigned_to', currentUser);
    userBlock.addOrCondition('opened_by', currentUser);

    // Add root-level OR branches for group-based service owners
    // NOTE: dot-walk uses a single dot and compares to sys_id values
    if (gs.getUser().isMemberOf('ABC')) {
        current.addOrQuery('parent.u_service_owner', '2122342312');  // ABC service owner sys_id
    }

    if (gs.getUser().isMemberOf('DEF')) {
        current.addOrQuery('parent.u_service_owner', '232323432');    // DEF service owner sys_id
    }

    // No addEncodedQuery() here—let the query tree you built run as-is
})();

 

Thanks,

Rithika.ch

@imran rasheed  if this is helpful please close the thread and accept the solution