The CreatorCon Call for Content is officially open! Get started here.

Hide choice on incident form for some users

Thereza Van der
Tera Contributor

Hi everyone

 

Please see if you can see what is wrong with my code. I created a business rule that should hide the 'high priority' option on the incident form for all users except those in the BST-Service Desk group. It does not seem to work? Only Service Desk should be able to raise the priority to P1 (High).

 

 

function showHighPriorityForBSTServiceDesk() {
    var userGroups = g_user.getMyGroups(); // Get user's groups
    var priorityField = g_form.getControl('priority'); // Priority field control

    // Check if the user belongs to the "BST-Service Desk" group
    if (userGroups.includes('BST-Service Desk')) {
        // Show the high-priority option (e.g., add it back to the choice list)
        g_form.addOption(priorityField, '1', '1 - High');
    } else {
        // Hide the high-priority option
        g_form.removeOption(priorityField, '1');
    }
}
 
Thanks
Thereza
1 ACCEPTED SOLUTION

Hi @Thereza Van der,

 

Thanks for confirming. As an FYI and something you should be mindful of is the Priority lookup Rules. It's here where the Priority is calculated from a matrix of Urgency and Impact values.

To view these, type and select: Priority lookup Rules in the In the Navigation menu.

 

If group membership were not an issue, you could simply update the Lookup rule that when Urgency and Impact are '1 - High', the Priority could be set to '2 - High' (Rather than the Out Of Box default of '1 - Critical').

No code would be needed, however, group membership is in context so we will need to use a Script Include and Client Script with a slight tweak to what was provided before.

 

Rather than remove the option of Priority 1, as this is a read only and a calculated value, let's remove the impact value of 1 which essentially means based on the Priority Lookup Rules, if Impact of 1 can not be selected, then the Priority can never be 1 (Assuming the Lookup rules have not been changed)

 

Client Script:

 

function onLoad() {
   
    
    var priorityField = 'impact'; // Change this to 'impact' if you want to test on another field which should be editable (OOB config)
    var groupName = 'BST-Service Desk'; // Rather than hardcoding, can we use a form field such as Assignment Group to make this dynamic?
    var sysid = g_user.userID; //get current user sysid
    var userGroupGA = new GlideAjax('global.CheckUserGroup'); //script include name
    userGroupGA.addParam('sysparm_name', 'getgroup'); //function name
    userGroupGA.addParam('sysparm_name_sysid', sysid); //passing sysid to server
	userGroupGA.addParam('sysparm_name_groupName', groupName); //passing group name to server
    userGroupGA.getXMLAnswer(getGroup);

    function getGroup(response) {
        if (response == 'true') {
            g_form.addInfoMessage('Part of group');
			//g_form.addOption(priorityField, '1', '1 - High'); // You don't need to add this as it's part of the default list, you only need to remove it in the else
        } else {
            g_form.addInfoMessage('Not Part of group');
			g_form.removeOption(priorityField, '1');
        }
    }
}

 

 

 

Script Include: (The same as before, but just in case)

 

var CheckUserGroup = Class.create();
CheckUserGroup.prototype = Object.extendsObject(AbstractAjaxProcessor, {
    getgroup: function() {
        var usersysid = this.getParameter('sysparm_name_sysid');//getting usersy sid from client
		var groupName = this.getParameter('sysparm_name_groupName');//get group from client (Dynamic and should be used)

        var mem = new GlideRecord("sys_user_grmember");
        mem.addQuery('user', usersysid); //filtering current user
        mem.addQuery('group.name', groupName); 
        mem.query();
        if (mem.next()) {
            return true;
        } else {
            return false;
        }
    },
    type: 'CheckuserGroup'
});

 

 

 

To help others (or for me to help you more directly), please mark this response correct by clicking on Accept as Solution and/or Kudos.



Thanks, Robbie

View solution in original post

9 REPLIES 9

Robbie
Kilo Patron
Kilo Patron

Hi @Thereza Van der,

 

Appreciate some days in the life of a SN dev/admin can be challenging.

It seems you have a mix of Server side code and Client side code in your example and simply put - it wont work.

 

Here's the solution. You need to call a Script Include (Server Side code) from a Client Script (Client Side).

 

Use the below which is tried and tested. Please note however, Priority is normally a calculated field driven by Impact and Urgency.

Do you/your org not calculate this, it's a select-able and editable field?

 

Client Script:

 

function onLoad() {
   
    
    var priorityField = 'priority'; // Change this to 'impact' if you want to test on another field which should be editable (OOB config)
    var groupName = 'ACME Support'; // Rather than hardcoding, can we use a form field such as Assignment Group to make this dynamic?
    var sysid = g_user.userID; //get current user sysid
    var userGroupGA = new GlideAjax('global.CheckUserGroup'); //script include name
    userGroupGA.addParam('sysparm_name', 'getgroup'); //function name
    userGroupGA.addParam('sysparm_name_sysid', sysid); //passing sysid to server
	userGroupGA.addParam('sysparm_name_groupName', groupName); //passing group name to server
    userGroupGA.getXMLAnswer(getGroup);

    function getGroup(response) {
        if (response == 'true') {
            g_form.addInfoMessage('Part of group');
			//g_form.addOption(priorityField, '1', '1 - High'); // You don't need to add this as it's part of the default list, you only need to remove it in the else
        } else {
            g_form.addInfoMessage('Not Part of group');
			g_form.removeOption(priorityField, '1');
        }
    }
}

 

 

Script Include:

 

var CheckUserGroup = Class.create();
CheckUserGroup.prototype = Object.extendsObject(AbstractAjaxProcessor, {
    getgroup: function() {
        var usersysid = this.getParameter('sysparm_name_sysid');//getting usersy sid from client
		var groupName = this.getParameter('sysparm_name_groupName');//get group from client (Dynamic and should be used)

        var mem = new GlideRecord("sys_user_grmember");
        mem.addQuery('user', usersysid); //filtering current user
        mem.addQuery('group.name', groupName); 
        mem.query();
        if (mem.next()) {
            return true;
        } else {
            return false;
        }
    },
    type: 'CheckuserGroup'
});

 

 

Screen shot:

Screenshot 2024-07-16 at 13.49.22.png

 

To help others (or for me to help you more directly), please mark this response correct by clicking on Accept as Solution and/or Kudos.



Thanks, Robbie

@Robbie 

 

Attached the screenshots as requested.

Thereza Van der
Tera Contributor

Hi Robbie 🙂

 

Regarding your comment below you are correct, priority is a calculated field driven by Impact and Urgency.

 

Please note however, Priority is normally a calculated field driven by Impact and Urgency.

Do you/your org not calculate this, it's a select-able and editable field?

Hi @Thereza Van der,

 

Thanks for confirming. As an FYI and something you should be mindful of is the Priority lookup Rules. It's here where the Priority is calculated from a matrix of Urgency and Impact values.

To view these, type and select: Priority lookup Rules in the In the Navigation menu.

 

If group membership were not an issue, you could simply update the Lookup rule that when Urgency and Impact are '1 - High', the Priority could be set to '2 - High' (Rather than the Out Of Box default of '1 - Critical').

No code would be needed, however, group membership is in context so we will need to use a Script Include and Client Script with a slight tweak to what was provided before.

 

Rather than remove the option of Priority 1, as this is a read only and a calculated value, let's remove the impact value of 1 which essentially means based on the Priority Lookup Rules, if Impact of 1 can not be selected, then the Priority can never be 1 (Assuming the Lookup rules have not been changed)

 

Client Script:

 

function onLoad() {
   
    
    var priorityField = 'impact'; // Change this to 'impact' if you want to test on another field which should be editable (OOB config)
    var groupName = 'BST-Service Desk'; // Rather than hardcoding, can we use a form field such as Assignment Group to make this dynamic?
    var sysid = g_user.userID; //get current user sysid
    var userGroupGA = new GlideAjax('global.CheckUserGroup'); //script include name
    userGroupGA.addParam('sysparm_name', 'getgroup'); //function name
    userGroupGA.addParam('sysparm_name_sysid', sysid); //passing sysid to server
	userGroupGA.addParam('sysparm_name_groupName', groupName); //passing group name to server
    userGroupGA.getXMLAnswer(getGroup);

    function getGroup(response) {
        if (response == 'true') {
            g_form.addInfoMessage('Part of group');
			//g_form.addOption(priorityField, '1', '1 - High'); // You don't need to add this as it's part of the default list, you only need to remove it in the else
        } else {
            g_form.addInfoMessage('Not Part of group');
			g_form.removeOption(priorityField, '1');
        }
    }
}

 

 

 

Script Include: (The same as before, but just in case)

 

var CheckUserGroup = Class.create();
CheckUserGroup.prototype = Object.extendsObject(AbstractAjaxProcessor, {
    getgroup: function() {
        var usersysid = this.getParameter('sysparm_name_sysid');//getting usersy sid from client
		var groupName = this.getParameter('sysparm_name_groupName');//get group from client (Dynamic and should be used)

        var mem = new GlideRecord("sys_user_grmember");
        mem.addQuery('user', usersysid); //filtering current user
        mem.addQuery('group.name', groupName); 
        mem.query();
        if (mem.next()) {
            return true;
        } else {
            return false;
        }
    },
    type: 'CheckuserGroup'
});

 

 

 

To help others (or for me to help you more directly), please mark this response correct by clicking on Accept as Solution and/or Kudos.



Thanks, Robbie

Thereza Van der
Tera Contributor

Thank you Robbie for going out of you way to assist me. REALLY appreciate it!