Hiding form section for users without role or if the record is not the user's

Wayne Richmond
Tera Guru

Hi. I'm trying to hide a form section on the user table unless the user has a role or the it's the users record. I've got the first part working with the following Client Script but I don't know how to configure it so the user can see the record if it's theirs. Can anyone else?

function onLoad() {

  if (g_user.hasRole('secret_qa_viewer')) {

  g_form.setSectionDisplay('security_questions', true);

  }

  else{

  g_form.setSectionDisplay('security_questions', false);

  }

}

1 ACCEPTED SOLUTION

Wayne Richmond
Tera Guru

Okay, I managed to get it working! Here's the code:



function onLoad() {



var id = (g_form.getUniqueValue());



  if (g_user.userID == id || g_user.hasRole('secret_qa_viewer')) {


  g_form.setSectionDisplay('security_questions', true);


  }



  else{


  g_form.setSectionDisplay('security_questions', false);


  }


}



Thanks everyone who replied.


View solution in original post

9 REPLIES 9

Rajesh Mushke
Mega Sage
Mega Sage

Hi Wayne,



It sounds like below threads, So Please Refer



Hiding Form Sections Based on User Group



Hide/Show Form Section Based on role


Show/Hide Form Section




Thanks,
Rajashekhar Mushke
Rising star : 2022 - 2024
Community Leader -2018
Connect me on LinkedIn : Rajashekhar Mushke

darius_koohmare
ServiceNow Employee
ServiceNow Employee

Please note that simply hiding a field via client script is not true security; if this is sensitive information I would ensure to also add a ACL record for read access to the actual fields, based on the same conditions you described (secret_qa_viewer role OR caller is javascript:gs.getUserID();)


Thanks, I do have ACLs in place. However, that doesn't stop the section being visible which is my requirement here.


sachin_namjoshi
Kilo Patron
Kilo Patron

Please update your client script like below


                                     


                            var user = g_form.getReference('assigned_to'); // modify to check record created by


  1. if (g_user.hasRole('secret_qa_viewer')) {  
  2.   g_form.setSectionDisplay('security_questions', true);  
  3.   }  

                                              if(g_user.userID == user.sys_id){



                                      g_form.setSectionDisplay('security_questions', true);  


                                      }


  1.   else{  
  2.   g_form.setSectionDisplay('security_questions', false);  
  3.   }