Query business rule is not allowing the "requested for" to see catalog task

Rhonda9
Tera Expert

Hello,

 

I am trying to add an additional condition to my query business rule that will restrict the catalog only users with the pso_info_security role, It is restricting to the role and opened by but not when I add "requested_for" into the business rule.     When I add the requested for, the restriction no longer works. What am I doing wrong?  I removed the extra condition.   Please help.

(function executeRule(current, previous ) {

if(!gs.hasRole("pso_info_security")) { //the user is not a member of pso_fraud or information security or an admin
var userID=gs.getUserID();
var itemID='448d32c91bdda214d058c992604bcb28';

var encodedQuery="request_item.cat_item !="+ itemID + "^ORrequest_item.opened_by=" +userID;
current.addEncodedQuery(encodedQuery);

}
})(current, previous);
1 ACCEPTED SOLUTION

Ankur Bawiskar
Tera Patron
Tera Patron

@Rhonda9 

try this

(function executeRule(current, previous) {
    // If the user does NOT have the pso_info_security role
    if (!gs.hasRole("pso_info_security")) {
        var userID = gs.getUserID();
        var itemID = '448d32c91bdda214d058c992604bcb28';

        // Only allow if the user is opened_by or requested_for
        // Block the item for others
        var encodedQuery = "request_item.cat_item!=" + itemID +
                           "^ORrequest_item.opened_by=" + userID +
                           "^ORrequest_item.requested_for=" + userID;

        current.addEncodedQuery(encodedQuery);
    }
})(current, previous);

If my response helped please mark it correct and close the thread so that it benefits future readers.

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

View solution in original post

12 REPLIES 12

Hello @Rhonda9 ,

 

Thanks for the update. But isn't that the exact same requirement as for all the other Catalog Items then? Meaning it does not actually matter which Catalog Item the task is related to?

 

Please allow me to ask the question again in this way, to make sure there is no misunderstanding:

 

  • Scenario 1: Request is for the "itemID" Catalog Item and I am the requester/opened by user.
    Can I see the tasks related to this request? (Yes/No)
  • Scenario 2: Request is for the "itemID" Catalog Item and I am neither the requester nor opened by user.
    Can I see the tasks related to this request? (Yes/No)
  • Scenario 3: Request is for any other Catalog Item and I am the requester/opened by user.
    Can I see the tasks related to this request? (Yes/No)
  • Scenario 4: Request is for any other Catalog Item and I am neither the requester nor opened by user.
    Can I see the tasks related to this request? (Yes/No)

PS: I am aware that, if I have the pso_info_security role then the answer will be yes for all 4 scenarios. I only need to know the answers for users who don't have that role.

 

Regards,

Robert

Hello @Robert H  I got it to work as expected now but to answer your questions.   Scenario 1. Yes Scenario 2. No, Scenario 3. Yes  Scenario 4. Yes        I needed to change the dotwalk field from requeted_item to requested.   This is what I am using .... I have always struggled with scripting. Thank you so much for helping me out. 

(function executeRule(current, previous) {
    if (!gs.hasRole("pso_info_security")) {
        var userID = gs.getUserID();
        var itemID = '448d32c91bdda214d058c992604bcb28';

        // Corrected query with dot-walk to request item
        var encodedQuery = "cat_item!=" + itemID +
                           "^ORopened_by=" + userID +
                           "^ORrequest.requested_for=" + userID;

        current.addEncodedQuery(encodedQuery);
    }
})(current, previous);

Hello @Rhonda9 ,

 

Thanks for clarifying that you are talking about the "Requested for" of the REQ instead of the RITM, and that you are referring to the "Opened by" of the TASK instead of the RITM.

 

Good to hear that you found the solution yourself, but I would still recommend to write the script without using the "encoded query" syntax, to make it easier to read:

 

current.addQuery('cat_item', '!=', itemID)
    .addOrCondition('opened_by', userID)
    .addOrCondition('request.requested_for', userID);

 

Regards,

Robert

Ankur Bawiskar
Tera Patron
Tera Patron

@Rhonda9 

try this

(function executeRule(current, previous) {
    // If the user does NOT have the pso_info_security role
    if (!gs.hasRole("pso_info_security")) {
        var userID = gs.getUserID();
        var itemID = '448d32c91bdda214d058c992604bcb28';

        // Only allow if the user is opened_by or requested_for
        // Block the item for others
        var encodedQuery = "request_item.cat_item!=" + itemID +
                           "^ORrequest_item.opened_by=" + userID +
                           "^ORrequest_item.requested_for=" + userID;

        current.addEncodedQuery(encodedQuery);
    }
})(current, previous);

If my response helped please mark it correct and close the thread so that it benefits future readers.

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

Thankyou @Ankur Bawiskar  I tried this script but it is allowing the opened_by but not the requested_for to see the catalog tasks  And that's the problem that I am running into .