Join the #BuildWithBuildAgent Challenge! Get recognized, earn exclusive swag, and inspire the ServiceNow Community with what you can build using Build Agent.  Join the Challenge.

Hiding Form Sections Based on User Group

allison_holt
Giga Expert

I am trying to hide a form section based on a users group, which is not listed on the form itself.

 

I tried the following client script and it's doesn't hide the tab.

 

function on Load(){

var me = g_user.userID();

if (me.isMemberOf('7efcb8956fa21100dd3253eabb3ee40c'){

var sections = g_form.getSections();

sections[3].style.display = 'block';

} else {

    sections[3].style.display = 'none';

    }

}

 

Any help as to what I can do to get this to work would be appreciated!

31 REPLIES 31

Hi,



Try not to use alerts in between Glide record.



if(ir.next()){


//use your alert here and try.


  g_form.setValue('u_comments', "hi shikha");// use single quotes here


  sections[1].style.display = 'block';


  sections[2].style.display = 'block';



  }



Do you have any mandatory field in these two sections?



Thanks,


Vinitha.K


Hi,



Yes i had a UI policy on comments field in this section, i have deactivated it for now, But still there is some problem.



I have 2 sections on form:



find_real_file.png



First section gets visible as per below script:


function onLoad() {



  var sections = g_form.getSections();


  var approvalstatus=g_form.getValue("u_approval_status");


  if (g_form.isNewRecord())


    {


{sections[1].style.display = 'none';}


{sections[2].style.display = 'none';}


  }


else{


var manager=g_form.getValue('u_manager');


  var user = g_user.getUserID();


  //alert(user);


  //alert(manager);


if (approvalstatus=='Pending Approval' && user!=manager)


  {


  sections[1].style.display = 'none';


  sections[2].style.display = 'none';


  }


else


  {


  sections[1].style.display = 'block';


  sections[2].style.display = 'none';


  }


}


}



For second section i was using the below code in this same script after second else: but it was not working.


alert('checking user is member of T&E team');


if (approvalstatus=='Approved' || approvalstatus=='Limit Changed Addressed' || approvalstatus=='Limit Change Finalized' && (person.isMemberOf('T&E Credit Card Team')));


  {


  {sections[1].style.display = 'block';}


  {sections[2].style.display = 'block';}


  alert('checking done');


}



Now i used your code as per your suggestion:


function onLoad()


{


  if (!g_form.isNewRecord())


  {


  var sections = g_form.getSections();


  //var approvalstatus=g_form.getValue("u_approval_status");


  var person = g_user.getUserID();


  alert(person);


  var ir = new GlideRecord('sys_user_grmember');


ir.addQuery('group', '64ca1638db6e3680130a50a4ce961922');


ir.addQuery('user', person);


  ir.query();


  if(ir.next()){


  g_form.setValue('u_comments', "hi shikha");


  sections[1].style.display = 'block';


  sections[2].style.display = 'block';


  }}}



But it is also not working now.


First section gets visible as per below script:


function onLoad() {



  var sections = g_form.getSections();


  var approvalstatus=g_form.getValue("u_approval_status");


  if (g_form.isNewRecord())


    {


{sections[1].style.display = 'none';}


{sections[2].style.display = 'none';}


  }


else{


var manager=g_form.getValue('u_manager');


  var user = g_user.getUserID();


  //alert(user);


  //alert(manager);


if (approvalstatus=='Pending Approval' && user!=manager)


  {


  sections[1].style.display = 'none';


  sections[2].style.display = 'none';


  }


else


  {


sections[1].style.display = 'block';


  sections[2].style.display = 'none';


  }


}


}



Section 2 will be visible if you change it to "block".. TRy and let me know.


Hi Vinitham,



What you have mentioned is already running.



Problem is in below condition:



For second section i was using the below code in this same script after second else: but it was not working.


alert('checking user is member of T&E team');


if (approvalstatus=='Approved' || approvalstatus=='Limit Changed Addressed' || approvalstatus=='Limit Change Finalized' && (person.isMemberOf('T&E Credit Card Team')));


  {


  {sections[1].style.display = 'block';}


  {sections[2].style.display = 'block';}


  alert('checking done');


}



I want to make section 2 visible only after manager has approved the request : that is why i have given condition as f (approvalstatus=='Approved' || approvalstatus=='Limit Changed Addressed' || approvalstatus=='Limit Change Finalized'


Hi,



approvalstatus=='Approved' || approvalstatus=='Limit Changed Addressed' || approvalstatus=='Limit Change Finalized' - Here, please check whether you use values or display values to check the approval status.



Also, declare and set person variable before the if,else part and check.



Thanks,


VInitha.K