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

pratikjagtap
Giga Guru

Hi @VijayKummamuru ,

 

Option 1: Move the Display Business Rule to the child table (incident, change_request, etc.)
If you're working specifically with Incidents, update the Display BR:

  • Table: incident (not task)
  • Leave the rest of your script as-is.
  • This will ensure g_scratchpad values are available to the client script.

Option 2: Apply Display BR to all child tables
If you want to keep the BR on Task but apply it to all child tables:

1.In the Display Business Rule:

  • Check Advanced.
  • Check Applies to: All application scopes.
  • Check When: Display.
  • Check Inherited (if visible — allows child tables to inherit the rule).

2.Add a log statement inside the BR to confirm if it's running:

 

gs.info("Display BR running on: " + current.getTableName());

 

If my response helped, please hit the 👍Thumb Icon and accept the solution so that it benefits future readers.

 

Regards,
Pratik

Vasantharajan N
Giga Sage
Giga Sage

@VijayKummamuru - Please use g_user.hasRoleExactly() in the client script to check the current logged has user role assigned instead of Display BR. 

 

Please try with the below onLoad client script and check

 

function onLoad() {
    //Type appropriate comment here, and begin script below
    if (!((g_user.hasRoleExactly("service_desk")) || (g_user.hasRoleExactly("incident_manager")) || (g_user.hasRoleExactly("major_incident_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");
    }

}

 


Thanks & Regards,
Vasanth

@Vasantharajan N  This 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 onCellEdit client script that runs on the list view of the table to control with same logic.


Thanks & Regards,
Vasanth