How to show value of choice field based on if user is part of assignment group

JAIM
Tera Contributor

1.  i have one choice field
field name: Request and
Values: hardware and software.
when user opens the form if user is part of hardware assignment group then software value should be visible in the field if user is not part of assignment group then hardware value in the field should not be visible

6 REPLIES 6

kaustubhdub
Tera Contributor

@JAIM,

Actually, not sure about how you want to display the option, but maybe an onLoad client script would be helpful for your use case. If not, please describe your problem a bit more so I can help you better.

If my reply was helpful, please mark it Correct and close the question.

Regards,
Kaustubh Dubey

JAIM
Tera Contributor

Hi @kaustubhdub
Thanks for your response,
i have two variables requested for which is refer to sys_user table and Request variable type is selectbox with 2 questions software and hardware
Suppose we have 2 users abel tuter and abhrahim 
user who is part of hardware assignment group he should be able to see hardware value in Request variable if user is not part of hardware group then hardware value in Request variable should be not visible

JAIM_0-1750158716759.png

 

kaustubhdub
Tera Contributor

Hi @JAIM ,

You can achieve this by creating a Client Script (type: onLoad) and using a GlideAjax call to check if the user belongs to the Hardware Assignment Group.

> If the user is not in the Hardware group, hide the hardware option from the Request field. The software option will always be shown.

Basic Flow:

  1. Create a Script Include (accessible to client) to check if the current user is in the Hardware group.
  2. In the onLoad client script, call the Script Include using GlideAjax.
  3. If the user is not in the group, remove the hardware option from the Request field.

If this solution was helpful, please mark it as correct and close the question.

Regards,
Kaustubh Dubey

Ehab Pilloor
Mega Sage

Hi @JAIM,

Use Script Include and OnLoad Client Script for this requirement:

Script Include

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

  getAvailableRequestTypes: function() {
    var userID = this.getParameter('sysparm_user_id');
    if (!userID) return '';

    var result = [];
    
    // Check Hardware grp
    var grHardware = new GlideRecord('sys_user_grmember');
    grHardware.addQuery('user', userID);
    grHardware.addQuery('group.name', 'Hardware Assignment Group'); // Replace exact name of grp
    grHardware.query();
    if (grHardware.hasNext()) result.push('hardware');

    // Check Software grp
    var grSoftware = new GlideRecord('sys_user_grmember');
    grSoftware.addQuery('user', userID);
    grSoftware.addQuery('group.name', 'Software Assignment Group'); // Replace exact name of grp
    grSoftware.query();
    if (grSoftware.hasNext()) result.push('software');

    return result.join(',');
  }

});

 

Client Script

function onLoad() {
  var userID = g_form.getValue('requested_for') || g_user.userID;

  var ga = new GlideAjax('CheckUserGroupsForRequest');
  ga.addParam('sysparm_name', 'getAvailableRequestTypes');
  ga.addParam('sysparm_user_id', userID);
  ga.getXMLAnswer(function(response) {
    var allowed = response.split(',');

    // Initially remove all options
    g_form.clearOptions('request');

    // Then add allowed ones
    if (allowed.includes('hardware')) {
      g_form.addOption('request', 'hardware', 'Hardware');
    }
    if (allowed.includes('software')) {
      g_form.addOption('request', 'software', 'Software');
    }
  });
}

 

Please add appropriate group, choice and variable names to the scripts.

 

Regards,

Ehab Pilloor