Hide/show choice value based on groups

Rose17
Tera Contributor

Hi all,

Below is the requirement-

There is a choice field with values A, B and C.

1. Requirement 1- Value C should be available for selection to only members of group 1 and group 2.

That means group 1 and group 2 members can see all the 3 choice values- A,B and C and the users who are not members of group 1 and group 2 can see only 2 choice values- A and B.

2. Requirement 2- When a member of group1/ group2 selects value C in the choice field and saves the record and when a user who is not a member of the group opens the record, then the user should see value C as read only in the choice field

 

In short- non members should not have value C available for selection, but when value C is already added in the choice field by member then it should be shown in the choice field but it should be read only.

 


I have achieved requirement 1 by using onload client script and script include but the issue is when value C is already added in a record and when non members open the record, the value shown in choice field is None.

Not sure how to achieve both requirement 2 along with requirement 1 as both seems a bit contradictory to me.

 

Can someone help me achieve both the requirements?

@Ankur Bawiskar Your help is highly appreciated 

Thanks in advance.

 

3 REPLIES 3

Najmuddin Mohd
Mega Sage

Hi @Rose17 ,
After achieving  first requirement, to get the second requirement 
Create a new on-load client script

Call a GlideAjax to know a member is part of the selected group
If Yes,
    return
Else,
     var ans = g_form.getValue('field');

     if(ans == 'c'){

           g_form.setReadOnly('field');

     }

 

Hope this information helps.

Regards,
Najmuddin.

Sandeep Rajput
Tera Patron
Tera Patron

@Rose17 You can't make a specific choice read only in the select box. Only the entire field can be made read only.

Rohit99
Mega Sage

Hi @Rose17,

 

You may try with the following client script and script include:

Client Script :

   function onLoad() {
       var sysid = g_user.userID; //get current user sysid
       var ga = new GlideAjax('CheckuserGroup'); //script include name
       ga.addParam('sysparm_name', 'getgroup'); //function name
       ga.addParam('sysparm_name_sysid', sysid); //passing sysid to server
       ga.getXMLAnswer(getGroup);

       function getGroup(response) {
           if (response == 'false') {
               if (g_form.getValue('u_choice_a') != 'Choice 3') {
                   g_form.removeOption('u_choice_a', 'Choice 3');
               } else {
                   var getChoiceField = g_form.getElement("u_choice_a");
                   getChoiceField.options[3].setAttribute("disabled", "true");
               }

           }
       }
   }
 
Script Include :

var CheckuserGroup = Class.create();
CheckuserGroup.prototype = Object.extendsObject(AbstractAjaxProcessor, {
    getgroup: function() {
        var usersysid = this.getParameter('sysparm_name_sysid');
        var mem = new GlideRecord("sys_user_grmember");
        mem.addQuery('user', usersysid);
        mem.addQuery('group', 'cfcbad03d711110050f5edcb9e61038f');
        mem.query();
        if (mem.next()) {
            return true;
        } else {

            return false;
        }
    },
    type: 'CheckuserGroup'
});


Attaching screenshot for reference.
 
Rohit99_0-1725014284078.png

 

 

Rohit99_1-1725014345735.png

 

Rohit99_2-1725014543447.png

 

 

 

Please mark my response as correct and helpful if it helped solved your question.

 

Thanks,

Rohit Suryawanshi