The Zurich release has arrived! Interested in new features and functionalities? Click here for more

Make SCTASK variables editable to particular assignment group- Catalog Item

snehalkausa
Tera Contributor

Hello All,


When a catalog item is requested, two different SCTASK records are generated — one assigned to Group A and the other to Group B.

If the logged-in user (or impersonated user) is a member of Group A, then the variables on the SCTASK assigned to Group A should be editable for that user.

At the same time, the same user must not be able to edit the variables on the SCTASK assigned to Group B.

Kindly suggest.

3 REPLIES 3

G Ponsekar
Mega Guru

Hi @snehalkausa 

 

Please try below steps:

Step 1: On the catalog item, configure the variables to be read-only on the catalog task view.
  1. Navigate to your Catalog Item.
  2. In the "Variables" related list, open the variable you want to control.
  3. Go to the Variable Administration tab and ensure the Read only box is checked

Step 2: Create Display Business rule: Fill in the form with the following details:

  • Name: Populate Scratchpad for SCTASK Variable Editability
  • Table: Catalog Task [sc_task]
  • When: Display
  • Condition: current.assignment_group is not empty

Go to the Advanced tab and add the following script:

javascript
(function executeRule(current, previous /*null when async*/ ) {
    // Use gs.getUser().isMemberOf() to check if the current user is a member of the task's assignment group.    g_scratchpad.isGroupMember = gs.getUser().isMemberOf(current.assignment_group.toString());
})(current, previous);
 
Step 3: Create an onLoad Catalog Client Script
This script reads the value from g_scratchpad and dynamically controls the variable editor's read-only state.
  1. Open the Catalog Item record.
  2. In the "Catalog Client Scripts" related list, click New.
  3. Fill in the form:
    • Name: Control SCTASK Variable Editability
    • Applies to: A Catalog Item (uncheck "Applies on a Catalog Item view" and "Applies on Requested Items").
    • Type: onLoad
    • Table: sc_task
  4. In the Script field, add the following code: 
 
javascript
function onLoad() {
    // If the user is a member of the assigned group, make all variables editable.
    if (g_scratchpad.isGroupMember == true) {
        var allVariables = g_form.getEditableFields();
        for (var i = 0; i < allVariables.length; i++) {
            g_form.setReadOnly(allVariables[i], false);
        }
    }
}

 

If I could help you with your Query then, please hit the Thumb Icon and mark as Correct !!

 

Thanks, GP

Rafael Batistot
Kilo Patron

Hi @snehalkausa 

 

 

Use a UI Policy + Condition Script

 

You can create a UI Policy (or Client Script) on the sc_task form:

 

  • Condition: Check if the current SCTASK’s assignment_group contains the logged-in user.
  • Action: Make variables read-only unless the user belongs to that group.

 

Example condition script:


(function executeRule(current, gForm) {
var userID = gs.getUserID();
var groupID = current.assignment_group;

// Check if user is in the assignment group
var isMember = gs.getUser().isMemberOf(groupID);

if (!isMember) {
// Make variables read-only
g_form.setReadOnly('variable_name', true);
// repeat for all variables, or loop if many
}
})(current, gForm);

 

This ensures users outside the group see the variables as read-only.

 

 

 



Ajay_Chavan
Kilo Sage

you can use onLoad catalog client script + GlideAjax and check if logged in user is member of that group or not

this link has sample script

Is Memberof Group from Client side

 

Group-Based Variable Editing

Step 1: Create Script Include**
// Name: GroupMembershipUtils
isMemberOfGroup: function(userId, groupId) {
    var gr = new GlideRecord('sys_user_grmember');
    gr.addQuery('user', userId);
    gr.addQuery('group', groupId);
    gr.query();
    return gr.hasNext();
}

Step 2: onLoad Client Script (SCTASK Form)**
// Name: SCTASK Variable Access Control
// Table: sc_task
// Type: onLoad

function onLoad() {
    // Get current user and assignment group
    var userId = g_user.userID;
    var assignmentGroup = g_form.getValue('assignment_group');
    
    if (!assignmentGroup) return;
    
    // Check group membership via GlideAjax
    var ga = new GlideAjax('GroupMembershipUtils');
    ga.addParam('sysparm_name', 'isMemberOfGroup');
    ga.addParam('sysparm_user_id', userId);
    ga.addParam('sysparm_group_id', assignmentGroup);
    
    ga.getXMLAnswer(function(answer) {
        var isMember = (answer === 'true');
        
        // Make variables read-only if user not in assignment group
        if (!isMember) {
            makeVariablesReadOnly();
        }
    });
}

function makeVariablesReadOnly() {
    // Get all variables on the form
    var variables = g_form.getVariables();
    
    for (var i = 0; i < variables.length; i++) {
        g_form.setReadOnly(variables[i], true);
    }
}


UI Policy Approach
// Create UI Policy on sc_task table
// Condition: javascript&colon;!gs.getUser().isMemberOf(current.assignment_group)
// Action: Make variables read-only

Script Include

var GroupMembershipUtils = Class.create();
GroupMembershipUtils.prototype = Object.extendsObject(AbstractAjaxProcessor, {
    
    isMemberOfGroup: function() {
        var userId = this.getParameter('sysparm_user_id');
        var groupId = this.getParameter('sysparm_group_id');
        
        var gr = new GlideRecord('sys_user_grmember');
        gr.addQuery('user', userId);
        gr.addQuery('group', groupId);
        gr.query();
        
        return gr.hasNext().toString();
    }
});

 

 

Glad I could help! If this solved your issue, please mark it as Helpful and Accept as Solution so others can benefit too.*****Chavan A.P. | Technical Architect | Certified Professional*****