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

Hello @VanishreeG ,

 

Please replace your entire code with the following:

 

var sysIdOfDesktopSupportGroup ='POPULATE THIS WITH THE CORRECT SYS_ID';
if (!gs.getUser().isMemberOf(sysIdOfDesktopSupportGroup)) {
    current.addQuery("opened_by", gs.getUserID());
}

This is assuming that "opened_by" is the field that your "Requested For" variable gets mapped to. If it's the Caller then adjust accordingly. Please also make sure the "Query" checkbox on the BR is selected.

 

Regards,

Robert

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);

@VanishreeG I honestly do not understand why this was chosen as the solution instead of the script that only requires 4 lines of code. And only 2 minutes after it was posted.