How to show a Form Field when member belongs to a specific Group?

tech_tc
Mega Contributor

How to show a Form Field when member belongs to a specific Group? - in Eureka


I'm using Eureka now and would like to only reveal 2 special fields on a form to users that are in a specific group.

e.g.   Users belonging to a 'BackupOperatorGrp' group have 2 mandatory fields on a form to fill in. example field names are 'Backup Type' and 'Backup Start Time' . The backup operators will enter random text as a string in the fields. Anyone else not in the 'BackupOperatorGrp' group will not even see these fields when the form launches.



I gather that a UI policy maybe the way to go with but i can't select 'group' from the Conditions: drop-down and can't see it in the Show Related Fields either. Therefore i'm assuming i need to put a script in-place. I've laid out the UI Policy as below. Any help help creating this script would be appreciated.


UI Plicy for Showing Fields for Backup Operators Only - (example only)

Table: B_Operators_Update

Short Desc: Show Fields To Backup Operators Only

Global: ticked

Reverse if False: ticked

On load: ticked

Inherit: unticked

Run scripts: ticked

Execute if true: script



1 ACCEPTED SOLUTION

try this one.   The section of code with answer is probably misleading you.


It needs to be calculated and you are not doing so



this code should work on just the users group membership


It will add an info message at the top of the page and then make fields visible / non visible


function onLoad() {



      //if(g_user.isMemberOf('BackupOperatorGrp'))


      //return;


      g_form.addInfoMessage('Scratchpad : ' + g_scratchpad.isMember);



      //if(g_user.isMemberOf('BackupOperatorGrp') && answer < -60 )


      if (g_scratchpad.isMember == true)


      {        


              g_form.setVisible('u_backup_type',true);


              g_form.setValue('u_backup_type','');


              g_form.setMandatory('u_backup_type',true);


              g_form.setVisible('u_backup_start_time',true);


              g_form.setValue('u_backup_start_time','');


              g_form.setMandatory('u_backup_start_time',true);


      }


      else                


      {        


              g_form.setVisible('u_backup_type',false);


              g_form.setMandatory('u_backup_type',false);


              g_form.setVisible('u_backup_start_time',false);


              g_form.setMandatory('u_backup_start_time',false);


      }





}  


View solution in original post

16 REPLIES 16

poyntzj
Kilo Sage

client script that checks the group membership onLoad and then hides the fields if they are not a member and makes them visible and mandatory if they are.



we do something similar with different fields, but for those we add an extra role to the user and check for that.


tech_tc
Mega Contributor

I'm trying to avoid using a Role as this has a cost attached to it I believe. If i have to i will though.


Do you have an example script to get me started?


sure if you want to do the br.. it is fairly easy we set this in a display br.. replace current assignment group with the sid of the group you are testing for...



g_scratchpad.isMember = gs.getUser().isMemberOf(current.assignment_group);



then in a client script you can use this...



if (g_scratchpad.isMember )




an alternative would be to simply feed all the groups into an array and thumb through them in the task... but up to you which way you go... i prefer the view business rule as it doesn't require the client script to make a server call.


This works.   you can change the "current.assignment_group" to be the name of a specific group


so for your purpose "BackupOperatorGrp"



the g_scratchpad.isMember is then either a true or false



This is a client script that was used.


It was to determine if a time Worked field was to be visible or not and indeed mandatory.


it checks to see if the ticket is closed, It uses reads the sys_updated_on field, compares against the current date / time (so if the ticket was updated a minute ago it does not need to be filled in again)   and looks to see the role is allocated to the user.



you can change it to read the scratchpad and make fields visible mandatory based on that




var lsu = g_form.getValue('sys_updated_on');


g_form.setVisible('sys_updated_on',false);



var state = g_form.getValue('state');


if (state !=7)


{      



      var ajax = new GlideAjax('MyDateTimeAjax');


      ajax.addParam('sysparm_name','nowDateTime');


      ajax.getXMLWait();      


      var rightnow = ajax.getAnswer()


      //var rightnow = getDateFromFormat(ajax.getAnswer(), g_user_date_time_format);


     


             


      var ajaxdc = new GlideAjax('ClientDateTimeUtils');


      ajaxdc.addParam('sysparm_name','getNowDateTimeDiff');


      ajaxdc.addParam('sysparm_fdt',lsu);


      ajaxdc.addParam('sysparm_difftype','second');      


      ajaxdc.getXMLWait();


      var answer = ajaxdc.getAnswer();


      //g_form.addInfoMessage('rightnow : ' + rightnow);


      //g_form.addInfoMessage('lsu : ' + lsu);


      //g_form.addInfoMessage('answer : ' + answer);


                                                 


     


     


     


     


      if(g_user.hasRole('TimeMandatory') && answer < -60 )


      {      


              g_form.setVisible('u_time_worked',true);


              g_form.setValue('u_time_worked','');


              g_form.setMandatory('u_time_worked',true);


      }


      else if(g_user.hasRole('TimeMandatory') && answer > -60)


      {      


              g_form.setVisible('u_time_worked',true);


              g_form.setValue('u_time_worked','');


              g_form.setMandatory('u_time_worked',false);


      }


      else              


      {      


              g_form.setVisible('u_time_worked',false);


            g_form.setMandatory('u_time_worked',false);


      }



}