Hide content in filed based on role or group

Gillerla Rajesh
Tera Contributor

Hi team ,

 

I need to hide field text based on role or group ,

For example : incident form have description filed, so based on role I will set encrypt or decrypt

 

Can any one provide me solution for this how to achieve

 

 

3 ACCEPTED SOLUTIONS

Aniket Chavan
Tera Sage
Tera Sage

Hello @Gillerla Rajesh 

As per your recent feedback you can refer the below OnLoad client script.

function onLoad() {
    // Check if the user has the specified role
    var hasRole = g_user.hasRole('admin');

    // Get the reference to the field
    var field = g_form.getControl('description');

    // Check if the user has the role and set the display accordingly
    if (hasRole) {
        // If the user has the role, show the decrypted value
        field.type = 'text'; // Use 'text' type to display the decrypted value
      
    } else {
        // If the user doesn't have the role, show the encrypted value
        field.type = 'password'; // Use 'password' type to display the encrypted value
    
    }
}


Please mark my answer as helpful and the solution accepted if it serves the purpose.

Regards,

Aniket.

 

View solution in original post

Hello @Gillerla Rajesh ,

 

Yes I know the difference between encrypt and decrypt and as per my  understanding only I have answered to your requirement,

 

Like you want to show field value is encrypted or decrypted based on the users role and this decrypted value means not the actual decrypt word you want to show it like it happens with password field with dot or star

 

So according to that only I have provided you the on load client script which will check the role and based on that it will mask the field valu if needed.

 

Correct me if I misunderstood anything.

 

View solution in original post

Hello @Gillerla Rajesh ,

No worries at all and you can refer to the code below in which I just have added group condition with the help of display business rule and client script, so either group or role will be required.

Display Business Rule:

(function executeRule(current, previous /*null when async*/) {

	// Add your code here
	g_scratchpad.grp = gs.getUser().isMemberOf('TM.ServiceNow'); 

})(current, previous);


On Change Client Script: 

 

function onLoad() {
    // Check if the user has the specified role
    var hasRole = g_user.hasRole('admin');

    // Check if the user is a member of the specified group
    // g_scratchpad.grp will be true if the user is a member
    var isMemberOfGroup = g_scratchpad.grp === true;

    // Get the reference to the field
    var field = g_form.getControl('short_description');

    // Check if the user has the role or is a member of the group and set the display accordingly
    if (hasRole || isMemberOfGroup) {
        g_form.addInfoMessage("inside if");
        // If the user has the role or is a member of the group, show the decrypted value
        field.type = 'text'; // Use 'text' type to display the decrypted value
    } else {
        g_form.addInfoMessage("inside else");
        // If the user doesn't have the role or is not a member of the group, show the encrypted value
        field.type = 'password'; // Use 'password' type to display the encrypted value
    }
}


Please let me know if you still face any issues.

Mark Correct if this solves your issue and also mark 👍Helpful if you find my response worthy based on the impact.

Regards,
Aniket




 

View solution in original post

13 REPLIES 13

palanikumar
Giga Sage
Giga Sage

You can achieve this requirement using simple client script.

Create a onload client script and add this line 

function onLoad() {

  if(g_user.hasRole('role_name')){ // Replace role_name with actual role name
    g_form.setValue('description','encrypt');
  }
  else{
    g_form.setValue('description','decrypt');
  }
}
Thank you,
Palani

Hi @palanikumar @Dr Atul G- LNG 

I don't need to set value encrypt or decrypt to the field , I need based on role based or group the description field information become hide I mens it will shown like encrypt format.

Example : if we choose filed type as password means it will encrypt format right like that I need to how can I achive this

 

 

Dr Atul G- LNG
Tera Patron
Tera Patron

Hi @Gillerla Rajesh 

 

This is not directly possible in UI policy and for this you can use the client script with Onload function. 

The script part is already given by my friend @palanikumar and it is good to go. 

*************************************************************************************************************
If my response proves useful, please indicate its helpfulness by selecting " Accept as Solution" and " Helpful." This action benefits both the community and me.

Regards
Dr. Atul G. - Learn N Grow Together
ServiceNow Techno - Functional Trainer
LinkedIn: https://www.linkedin.com/in/dratulgrover
YouTube: https://www.youtube.com/@LearnNGrowTogetherwithAtulG
Topmate: https://topmate.io/atul_grover_lng [ Connect for 1-1 Session]

****************************************************************************************************************

Ankur Bawiskar
Tera Patron
Tera Patron

@Gillerla Rajesh 

If your ask is to hide the field text, you can simply use field level READ ACL and show it for set of users you want.

I would recommend using field level READ ACL over Client script

If my response helped please mark it correct and close the thread so that it benefits future readers.

Regards,
Ankur
Certified Technical Architect  ||  9x ServiceNow MVP  ||  ServiceNow Community Leader