Preventing users from logging a P1 incident

Thereza Van der
Tera Contributor

Good afternoon all 🙂

 

I created a client script that prevents users from logging a P1 incident. When they do, they get a message saying they should contact the ServiceDesk. The incident gets saved as low priority. I would like people in a certain group (BST-ServiceDesk) to be able to change the priority to P1 if needed. Please help with my code. I get the message part right, but the part where only BST-ServiceDesk can upgrade it to P1 still gives an error.

 

I made the area where I think the problem is red and the error I am getting is: Error MessageonSubmit script error: TypeError: Cannot read properties of undefined (reading 'indexOf'):
function () { [native code] }

 

function onSubmit() {

    // Get the priority field value

    var priorityField = g_form.getValue('priority');

   

    // Check if the priority is set to P1

    if (priorityField === '1') {

        // Check if the user is part of the 'BST-Service Desk' group

        var userGroups = g_user.userGroups;

        if (userGroups.indexOf('BST-Service Desk') !== -1) {

            // Allow the ServiceDesk to change the priority to P1

            return;

        } else {

            // Display an error message

            g_form.addErrorMessage('P1 incidents are not allowed to be logged. Please contact the ServiceDesk for assistance.');

           

            // Set impact and urgency to 2 (Medium)

            g_form.setValue('impact', '2'); // Set impact to Medium

            g_form.setValue('urgency', '2'); // Set urgency to Medium

           

            // Save the form

            g_form.submit();

        }

    }

}

3 REPLIES 3

ChiranjeeviR
Kilo Sage

Hi @Thereza Van der ,

You can create business rule as below. If user tries to create or update incident priority to P1 then it will change impact and urgency to 3 automatically and will give message on the screen. Only Service desk can change the priority to P1.

 

ChiranjeeviR_0-1721048014032.png

 

 

ChiranjeeviR_1-1721048014039.png

Script: 

(function executeRule(current, previous /*null when async*/) {
 
if (!gs.getUser().isMemberOf('d625dccec0a8016700a222a0f7900d06'))    // Put service desk group sys_id
{
current.setValue('impact', 3);
current.setValue('urgency', 3);
gs.addInfoMessage('Incident has been logged, but you can contact the ServiceDesk to change the priority to 1 if needed.');
}  
 
})(current, previous);

 

You can create system property to store the sys_id of "BST-Service Desk" group and then use in above script using "gs.getProperty()" method as best practice.

 

ChiranjeeviR_2-1721048014045.png

 

Thanks and Regards,

Chiranjeevi R

 

Please mark as Correct Answer/Helpful, if applicable.

Thanks & Regards,
Chiranjeevi
ServiceNow Developer | | ITSM | | ServiceNow Discovery | | Event Management | | Service Mapping | | CMDB

Please mark as Correct Answer/Helpful, if applicable.

Musab Rasheed
Tera Sage
Tera Sage

Don't make it complicated, follow either of one solution.

1) Hide P1 values from fields and make it visible only for set of groups/users.

2) Use before BR and abort insert in case if it is not done by correct set of users/groups.

Please hit like and mark my response as correct if that helps
Regards,
Musab

Jim Coyne
Kilo Patron

Couple things:

- is this in a Catalog Item/Record Producer or the native form view?  If native view, why are non helpdesk users allowed to do that?

- you could add a message to the screen when the user changes the Priority to 1 instead of allowing them to submit it

- using Business Rules in these use cases are not very useful: why allow a user to "submit" something, throw an error then bring them back?  Not a nice experience.

- "g_user.userGroups" does not exist.  Take a look at this ServiceNow Guru article: User Object Cheat Sheet

- does that Group have a unique Role that could be leveraged instead?