Only users with specific roles (e.g., Service Desk Agent, Incident Manager, Major Incident Team)

VijayKummamuru
Tera Expert

Wrote a client script and Display BR on Task table but not working 

 

(function executeRule(current, previous /*null when async*/ ) {

    g_scratchpad.service_desk = gs.getUser().hasRole('interaction_agent');
    g_scratchpad.incident_manager = gs.getUser().hasRole('incident_manager');
    g_scratchpad.majorincident_manager = gs.getUser().hasRole('major_incident_manager');


})(current, previous);
 
Onload Client script 
 
function onLoad() {
    //Type appropriate comment here, and begin script below
    if (!((g_scratchpad.service_desk) || (g_scratchpad.incident_manager) || (g_scratchpad.majorincident_manager))) {
        // Remove "Crisis" and "Critical" options from Impact field
        g_form.removeOption('impact', '1'); // Crisis
        g_form.removeOption('impact', '2'); // Critical

        // Remove "High" and "Critical" options from Urgency field
        g_form.removeOption('urgency', '1'); // High
        g_form.removeOption('urgency', '2'); // Critical

        // Show message guiding users to contact Service Desk
        g_form.showFieldMsg('priority',
            "Please contact Service Desk to raise priority to Critical or Crisis",
            "info");
    }




}
 
 
2 ACCEPTED SOLUTIONS

@VijayKummamuru  - You can use onCellEdit client script that runs on the list view of the table to control with same logic.


Thanks & Regards,
Vasanth

View solution in original post

@VijayKummamuru 

you can use list_edit ACLs on those 2 fields to block the field from getting updated in list

OR

you can use onCell Edit and stop the update, something like this

function onCellEdit(sysIDs, table, oldValues, newValue, callback) {

    var saveAndClose = true;
    if (!((g_user.hasRoleExactly("service_desk")) || (g_user.hasRoleExactly("incident_manager")) || (g_user.hasRoleExactly("major_incident_manager")))) {
        if (newValue.toString() == '1' || newValue.toString() == '2')
            alert('Please contact Service Desk to raise priority to Critical or Crisis');
        saveAndClose = false;
    } else {
        saveAndClose = true;
    }
    callback(saveAndClose);

}

OR

you can use before update business rule

Condition:

Priority Changes to 1 OR Priority Changes to 2 && !((gs.hasRole("service_desk")) || (gs.hasRole("incident_manager")) || (gs.hasRole("major_incident_manager")))

Script:

(function executeRule(current, previous) { // Add your code here

    gs.addErrorMessage('Please contact Service Desk to raise priority to Critical or Crisis');
    current.setAbortAction(true);

})(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

9 REPLIES 9

mohdarbaz
Kilo Guru

@VijayKummamuru ,

Add console.log statements to both the Business Rule and Client Script to check if they are being executed and if the roles are being correctly identified.

 

2nd alternative:

Go with client script only instead of Display BR

use g_form.hasRoleExaclty() method to check the required roles and remove options based for no role found.

 

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

 

Regards,

Mohd Arbaz.

Ankur Bawiskar
Tera Patron
Tera Patron

@VijayKummamuru 

Why not use g_user.hasRoleExactly() in client side and handle the logic?

Check script shared by @Vasantharajan N 

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

@Ankur Bawiskar  That is working i before  but the problem is user still able to change the impact and urgency through List view .can you guide me to solution for that

@VijayKummamuru 

you can use list_edit ACLs on those 2 fields to block the field from getting updated in list

OR

you can use onCell Edit and stop the update, something like this

function onCellEdit(sysIDs, table, oldValues, newValue, callback) {

    var saveAndClose = true;
    if (!((g_user.hasRoleExactly("service_desk")) || (g_user.hasRoleExactly("incident_manager")) || (g_user.hasRoleExactly("major_incident_manager")))) {
        if (newValue.toString() == '1' || newValue.toString() == '2')
            alert('Please contact Service Desk to raise priority to Critical or Crisis');
        saveAndClose = false;
    } else {
        saveAndClose = true;
    }
    callback(saveAndClose);

}

OR

you can use before update business rule

Condition:

Priority Changes to 1 OR Priority Changes to 2 && !((gs.hasRole("service_desk")) || (gs.hasRole("incident_manager")) || (gs.hasRole("major_incident_manager")))

Script:

(function executeRule(current, previous) { // Add your code here

    gs.addErrorMessage('Please contact Service Desk to raise priority to Critical or Crisis');
    current.setAbortAction(true);

})(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

Robert H
Mega Sage

Hello @VijayKummamuru ,

 

Your scripts are working fine, assuming that the purpose is the following:

If the user has at least one of the three roles it won't do anything but if the user has none of these roles then the Impact options get removed.

 

How are you testing this?

Check by impersonating a user who has one of the three roles.

Then check by impersonating a user who has none of them.

 

Regards,

Robert