Incidents visible to the user selected in "Requested For" variable

VanishreeG
Tera Contributor

We have hidden incidents from all users and made only visible to "Desktop support" group members.
Now , We have requirement to make incidents visible to the user selected in "Requested For" variable on record producer.

1 ACCEPTED SOLUTION

Community Alums
Not applicable

Try below code-

(function executeRule(current, previous) {
    var arr1 = [];

    // Get all incidents assigned to the specific group
    var grInc = new GlideRecord('incident');
    grInc.addEncodedQuery('assignment_group=955ff733fbe7d250a322f59ff4efdce7');
    grInc.query();
    while (grInc.next()) {
        arr1.push(grInc.sys_id.toString()); // Store sys_id of incidents in the array
    }

    var currentUserID = gs.getUserID(); // Get the current logged-in user ID
    var currentUser = gs.getUser();

    // Get incidents where the "On Behalf Of" user is stored
    var userIncidents = [];
    var grUserInc = new GlideRecord('incident');
    grUserInc.addQuery('u_on_behalf_of', currentUserID);  // Check if logged-in user is the "On Behalf Of" user
    grUserInc.query();
    while (grUserInc.next()) {
        userIncidents.push(grUserInc.sys_id.toString());
    }

    // If the user is NOT a member of 'People Solution Support', apply filter
    if (!currentUser.isMemberOf('People Solution Support')) {
        var encodedQuery = '';

        // Condition 1: Exclude incidents assigned to 'People Solution Support'
        if (arr1.length > 0) {
            encodedQuery += 'sys_idNOT IN' + arr1.join(',');
        }

        // Condition 2: Allow incidents where the user is the "Opened By"
        encodedQuery += '^ORopened_by=' + currentUserID;

        // Condition 3: Allow incidents where the user is listed in "On Behalf Of"
        if (userIncidents.length > 0) {
            encodedQuery += '^ORsys_idIN' + userIncidents.join(',');
        }

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

View solution in original post

7 REPLIES 7

J Siva
Tera Sage
Tera Sage

Hi @VanishreeG 
I believe you've used "Deny Unless" ACL to acheive your 1st requirement.
If yes, then you should update the script conditon on the same ACL.
Also the "requested for" variable musted be mapped to one of the fields on the incident table, like Affected caller/caller.

So in your script block write the below script,

var caller_id = current.getValue('caller');
var current_user = gs.getUserID();

if (gs.getUser().isMemberOf('Desktop support') || caller_id == current_user) {
    answer = true;
} else {
    answer = false;
}

We used before business rule to hide the incidents for Desktop support group.
Could you give more details about ACL script for the Incidents visible to the user selected in "Requested For" variable

Could you share the pic of the BR script? It'll be helpful to review and modify.

(function executeRule(current, previous) {
    var arr1 = [];

    // Get the RITMs with the specific cat_item
    var grInc = new GlideRecord('incident');
    grInc.addEncodedQuery('assignment_group=4555877785868');
    grInc.query();
    while (grInc.next()) {
        arr1.push(grInc.sys_id.toString()); // Store the sys_id of the inc in the array
    }

    var reqFor = gs.getUserID(); // Get the current user
    var currentUser = gs.getUser();
    if (!currentUser.isMemberOf('desktop support')) {

        var encodedQuery = 'sys_idNOT IN' + arr1.join(',') + '^ORopened_by=' + reqFor; // Filter by user who created the ticket  
      current.addEncodedQuery(encodedQuery); // Adding this encoded query to current to filter records
    }
})(current, previous);