Not working- User Criteria

ujjwala_678
Tera Contributor

Hi All,

I've set up user criteria for a knowledge base to restrict article visibility to policy owners, approvers, reviewers, and members of the owning group. However, the logic isn't functioning as expected.
We have 'u_policy'  custom field present in kb_knowledge table pulling data from policy table where approvers, reviewers and owners are available

Could anyone advise on where I might need to adjust the configuration or logic to achieve the intended visibility restrictions?

Thank you for your help!

 

User Criteria: Global Security Reviewers
Advanced : true
Script:
answer = getValidUsers(user_id);

function getValidUsers(userID) {
    var result = false;

    // Check if the user is listed as an approver in 'u_policy.approvers'
    var res1 = false;
    var grUserRole = new GlideAggregate('kb_knowledge');
    grUserRole.addQuery('u_policy.approvers', userID);
    grUserRole.addAggregate('COUNT');
    grUserRole.query();
    if (grUserRole.next() && grUserRole.getAggregate('COUNT') > 0) {
        res1 = true;
    }

    // Check if the user is listed as a reviewer in 'u_policy.reviewers'
    var res2 = false;
    var grUser = new GlideAggregate('kb_knowledge');
    grUser.addQuery('u_policy.reviewers', userID);
    grUser.addAggregate('COUNT');
    grUser.query();
    if (grUser.next() && grUser.getAggregate('COUNT') > 0) {
        res2 = true;
    }

    var res3 = false;
    var grUsers = new GlideAggregate('kb_knowledge');
    grUsers.addQuery('u_policy.owner', userID);
    grUsers.addAggregate('COUNT');
    grUsers.query();
    if (grUsers.next() && grUsers.getAggregate('COUNT') > 0) {
        res3 = true;
    }


    var res4 = false;
    var validGroup = new GlideAggregate('kb_knowledge');
    validGroup.addQuery('u_policy.owning_groupDYNAMICd6435e965f510100a9ad2572f2b47744');
    validGroup.addAggregate('COUNT');
    validGroup.query();
    if (validGroup.next() && validGroup.getAggregate('COUNT') > 0) {
        res4 = true;
    }
    // Set result to true if either condition is met
    result = res1 || res2 || res3 || res4;

    return result;
}

 

1 ACCEPTED SOLUTION

The value of a list field is a comma-separated list of sys_ids, so unless the user is the only approver or reviewer, the query criteria will never be met.  Use this to ensure the query works if the user is the only or one of the approvers/reviewers

    grUserRole.addQuery('u_policy.approvers', 'CONTAINS', userID);

 

View solution in original post

3 REPLIES 3

Brad Bowman
Kilo Patron
Kilo Patron

Like any other script, I would say to add some logs to determine what is happening in the script, and you should see where it is going wrong in each or every GA result.  Are you using an out of box 'policy' table or custom?  Are the 'approvers' and 'reviewers' fields on the policy table the type of reference or list? Is a test case where the current user is an owner on the policy table working?  In validGroup, use addEncodedQuery for a string like this, and since you're hard-coding a sys_id it's not dynamic so change this to 

validGroup.addEncodedQuery('u_policy.owning_group=d6435e965f510100a9ad2572f2b47744');

or

validGroup.addQuery('u_policy.owning_group', 'd6435e965f510100a9ad2572f2b47744');

If the data is right but the GlideAggregates are not working for you, change these to simpler GlideRecord queries as you just looking for a record that meets the criteria in each case, not a count of how many records meet the criteria.  Since the queries are not dependent on each other, you can just return true within the if block of each one.  This way the rest of the script won't run if the current user is an approver, once we get this part working.

 

Hello Brad,

Thank you for your response on this😇

I'm currently working with the out-of-the-box Policy table, where both the Approvers and Reviewers fields are list-type fields. For the group criterion, I'm trying to verify if the logged-in user belongs to the Policy Owning Group.

I’ve set up the condition as "Owning Group is (dynamic) one of my groups." I'll make some adjustments to this configuration and will let you know if it resolves the issue.
Thank you!

The value of a list field is a comma-separated list of sys_ids, so unless the user is the only approver or reviewer, the query criteria will never be met.  Use this to ensure the query works if the user is the only or one of the approvers/reviewers

    grUserRole.addQuery('u_policy.approvers', 'CONTAINS', userID);