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

Thank you for sharing the image for the reference and that's exactly will done if you use my client script which I shared you in first place.

Have you tried it?

Hi @Aniket Chavan Sorry for mis Understanding the code. The code is working based on role, if I want based of group means how can I achieve please provide me solution for group also

 

 

Thanks for solution

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




 

Gillerla Rajesh
Tera Contributor

IMG_20231211_211927.png

 

I nee to show like this based on role or group, again don't suggest me

 g_form.set Value('description',,'•••••');  like this silly ways to achieve