The CreatorCon Call for Content is officially open! Get started here.

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

Thanks but this doesn't work I'm afraid. Are you sure the user.sys_id bit is correct? I can't find any other example of it.


Sharique Azim
Mega Sage

Hi Wayne,


A few tip here, although the requirement is not clearly understood.



if( g_user.hasRoles() ){


can simply check if the user has ANY role     (as per your words in the question)


}



you can use hasRoleFromList instead. http://wiki.servicenow.com/index.php?title=GlideUser_(g_user)#gsc.tab=0




But,please clarify on what   basis are you filtering that "the ticket   is of the user's" ? are you using assigned to for checking?


you can simply use



var usr= g_form.getValue('assigned_to');


if( g_user.hasRoleFromList('role') || g_form.userID==usr   ){


}


Hi sharique, thanks for the reply. I should have said "unless the user has a specific role", namely the 'secret_qa_viewer' role. This bit is working just fine, it's the latter part I can't get to work, namely when the user is viewing their own record. I'll explain it as clearly as I can.



I am the user Wayne Richmond. I have no roles, I am just an ess user. I open my own user record. I want to be able to see the section 'Secret Questions' because it's my record. I don't want anyone else to see this unless they have the security role 'secret_qa_viewer'.



I assumed I could achieve this by something like what sachin.namjoshi provided (where the current user's sys_id matches the current record's sys_id):



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


g_form.setSectionDisplay('security_questions', true);


}



Unfortunately it doesn't work.


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.


Ahh, i absolutely missed that you are using the user table here. My bad.