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

tech_tc
Mega Contributor

Hi Julian



This has been hugely helpful thanks.


I have this working in a fashion. I've got the Client Script working by its own only using Roles, as i got Function Declaration errors when trying to use the g_scratchpad.isMember in the Client script. I've obviously gone wrong somewhere.


Ideally i'd like to convert what i have to be driven by a Group, rather than Roles.



\\I understand my business rule should be as follows:



Name: Show Fields To Backup Operators Only (example only)


Table: B_Operators_Update


Active: ticked


Advanced: ticked


When: display


Order: 200


Delete: unticked


Query: unticked


When to run - Filter Conditions: not set


Actions - Set field values: not set


Inherit: unticked


Run scripts: ticked


Advanced - Script: g_scratchpad.isMember = gs.getUser().isMemberOf(BackupOperatorGrp);






\\Here is my working Client Script to Restrict Fields on Form by a specific Role. The setMandatory does not appear to work but the 2 fields are only shown to those that have the 'BackUpRole' Role.



function onLoad() {


  if (g_user.hasRole('BackUpRole'))


  return;


         


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


      {          


              g_form.setVisible('Backup_Type',true);  


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


              g_form.setMandatory('Backup_Type',true);


  g_form.setVisible('Backup_Start_Time',true);  


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


              g_form.setMandatory('Backup_Start_Time',true);


      }  


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


      {          


              g_form.setVisible('Backup_Type',true);  


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


              g_form.setMandatory('Backup_Type',false);  


  g_form.setVisible('Backup_Start_Time',true);  


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


              g_form.setMandatory('Backup_Start_Time',false);


      }  


      else                  


      {          


              g_form.setVisible('Backup_Type',false);  


            g_form.setMandatory('Backup_Type',false);  


        g_form.setVisible('Backup_Start_Time',false);  


            g_form.setMandatory('Backup_Start_Time',false);


      }  


 


}  




Just need some help with 3 things.


1). Check my Display Business Rule is configured correctly so it calls my client script.


2). Where to add the entry for the if (g_scratchpad.isMember ) in my client script


3). Do i replace the if(g_user.hasRole('BackUpRole') with if(g_user.isMemberOf('BackupOperatorGrp') ?



Thanks in advance for your help.


I would check the line


Advanced - Script: g_scratchpad.isMember = gs.getUser().isMemberOf(BackupOperatorGrp);


is this


Advanced - Script: g_scratchpad.isMember = gs.getUser().isMemberOf('BackupOperatorGrp');


just incase the quotes are missing as it will throw an error.



You then change the code to be


if (g_scratchpad.isMember == true)


(if that is still not working, you can add the following line above the if statement


g_form.addInfoMessage('Scratchpad ; ' + g_scratchpad.isMember) - should show you true or false at the top of the page when the script loads.



setMandatory works for me.   I wonder if you have another UI Policy that is kicking in


also, check the field names.   Are you sure it is Backup_Time and not u_Backup_Time


They look like the labels to me and not the actual field names.



as a test, you can run these in a Background Script


gs.print(gs.getUser().isMemberOf('BackupOperatorGrp'))


gs.print(gs.getUser().isMemberOf('BackupOperatorGrp2'))



If you are a member it will show true and then false


If you are not, it will be false and false.



Just gives peace of mind that that part works.


tech_tc
Mega Contributor

Julian


I've done all those tweaks, but still have issue with if (g_scratchpad.isMember == true) bit. I get the error WARNING at line 2: Function declarations should not be placed in blocks. Use a function expression or move the statement to the top of the outer function.



if (g_scratchpad.isMember == true)


function onLoad() {


  if(g_user.isMemberOf('BackupOperatorGrp'))


  return;


         


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


      {          


              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 if(g_user.isMemberOf('BackupOperatorGrp') && answer > -60)  


      {          


              g_form.setVisible('u_backup_type',true);  


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


              g_form.setMandatory('u_backup_type',false);  


  g_form.setVisible('u_backup_start_time',true);  


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


              g_form.setMandatory('u_backup_start_time',false);


      }  


      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);


      }  


 


}  


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);


      }





}  


tech_tc
Mega Contributor

That worked a treat! Thanks.


I've noticed the setMandatory is now working too!


One strange thing i notice though. When the form loads for users that don't have the group, i see a very quick flash of the 2 fields before they disappear. Is this how it is? i thought the Business rule ran before the form is displayed and would present the form minus the 2 fields?