Variable Sets - make a field open if the user is part of a group

BrianS344076034
Tera Guru

So here is the situation we have and what we are trying to do

I have a Variable Set - User Information

It has the following

Customer Name (reference to active users)

Manager - Customers Manager

Business Reason for Request

 

What we are trying to do

Manager should be read only, unless the person is part of a group that is allowed to select someone else to approve their request.  We have some EA's that have multiple managers that might have to approve.

 

For the below 

We made a group called SN-Multi Approvers, then I added myself to the group for testing.

 

I found this as a possible solution

1. Create a Display Business Rule 
  1. Navigate to Business Rules in the Application Navigator.
  2. Click New.
  3. Fill in the form:
    • Name: A descriptive name (e.g., Set g_scratchpad for Group Membership).
    • Table: Select the table where the variable set is used (e.g., sc_req_item, sc_task, or the specific table if used outside the catalog).
    • Active: Checked.
    • Advanced: Checked.
    • When to run:
      • When: Display
  4. In the Script field, add the following code, replacing 'YOUR_GROUP_NAME' with the actual name of the group: 
 
javascript
(function executeRule(current, previous /*null when async*/) {
   // Check if the current user is a member of the specified group and store the result (true/false) in g_scratchpad   g_scratchpad.isGroupMember = gs.getUser().isMemberOf('YOUR_GROUP_NAME');
})(current, previous);
  1. Save the Business Rule. 
 
2. Create a Catalog Client Script 
  1. Navigate to Catalog Client Scripts under Service Catalog > Catalog Administration in the Application Navigator.
  2. Click New.
  3. Fill in the form:
    • Name: A descriptive name (e.g., Make Variable Read Only if Group Member).
    • Applies to: A Variable Set
    • Variable Set: Select the relevant variable set.
    • UI Type: All (ensures it works in both the standard UI and Service Portal).
    • Type: onLoad.
  4. In the Script field, add the following code, replacing 'your_variable_name' with the actual name of the variable you want to make read-only: 
 
javascript
function onLoad() {
   // Check the value from the g_scratchpad set by the Business Rule
   if (g_scratchpad.isGroupMember == 'true') {
      // Make the variable read-only.
      // Use the 'variables.' prefix for variables within a variable set in the catalog environment      g_form.setReadOnly('your_variable_name', true);
      
      // If you have multiple variables, you can set them individually:
      // g_form.setReadOnly('another_variable_name', true);   }
}
  1. Save the Catalog Client Script. 
Now, when a user who is a member of the specified group loads the form containing the variable set, the variable will be automatically set to read-only. 
 
 
My Assumption is this was designed to make the Manager field READ Only, so I set that to False and it should make is open for anyone that is in the group
Currently its not working, and wondering if I missed something or if anyone has any ideas
6 REPLIES 6

Rafael Batistot
Mega Patron

Hi @BrianS344076034 

 

 

Your logic is inverted and there are two ServiceNow-specific issues:

 

  1. Display Business Rules do NOT run for Service Catalog items
    • Display Business Rules do not execute when loading a catalog item / record producer / variable set.
    • Because of that, g_scratchpad is never populated  your client script receives nothing.
  2. Your script makes the field read-only for group members
    • But your requirement is the opposite:
      • Default: Manager = Read-only
      • Exception: Editable only for users in SN-Multi Approvers

 

 

Create a Catalog Client Script 

 

 

Applies to: Variable Set

Type: onLoad

UI Type: All

 

function onLoad() {

// Default: Manager is read-only
g_form.setReadOnly('manager', true);

// If user is in the allowed group, make it editable
if (g_user.isMemberOf('SN-Multi Approvers')) {
g_form.setReadOnly('manager', false);
}
}

 

 

appreciate it

looks like it tries to load, but I get javascript error

BrianS344076034_0-1771208504592.png

 

 

Ankur Bawiskar
Tera Patron

@BrianS344076034 

Display Business rule don't run on catalog form

Use onLoad catalog client script + GlideAjax for this

Script Include: It should be client callable

var checkRecords = Class.create();
checkRecords.prototype = Object.extendsObject(AbstractAjaxProcessor, {

    checkRecordPresent: function() {

        var id = this.getParameter('sysparm_userID');

        var gr = new GlideRecord('sys_user_grmember');
        gr.addQuery('group.name', 'YOUR_GROUP_NAME'); // use group name here
        gr.addQuery('user', id);
        gr.query();
        return gr.hasNext().toString();
    },

    type: 'checkRecords'
});

onChange catalog client script on Customer Name:

Applies to Catalog Item

UI Type - ALL


function onChange(control, oldValue, newValue, isLoading) {
    if (isLoading || newValue == '') {
        return;
    }

    var ga = new GlideAjax('checkRecords');
    ga.addParam('sysparm_name', "checkRecordPresent");
    ga.addParam('sysparm_userID', newValue);
    ga.getXMLAnswer(function(answer) {
        if (answer == 'false') {
            g_form.setReadOnly('another_variable_name', true);
        } else {
            g_form.setReadOnly('another_variable_name', false);
        }
    });

}

💡 If my response helped, please mark it as correct ✅ and close the thread 🔒— this helps future readers find the solution faster! 🙏

Thanks
The second script I put in the variable set, the first script where do I create that?