Show/Hide choices on field depending on which group the user is member of

Floridalife
Giga Expert

I am trying to hide some of the choice in a reference type field depending on which group the user is part of, at first i was trying to filter it based on what the u_assignment_group field was set to on the form, this is the client script i was   using but it didnt work (even tried SysID for the group), if this is the recommended way can you advise what looks wrong in the code?

function onChange() {

var sta = g_form.getValue('u_assignment_group');//get the value of state  

//var cat = g_form.getValue('category');//get the value of category  

//var com = g_form.getValue('company');//get the value of company  

  if (u_assignment_group == "8dd9bdaa37c45e009325dcc773990e25" || u_assignment_group == "SA" || u_assignment_group == "INF")/*if state is resolved AND category is Inventory AND company is HQ, then:*/   {  

      //now populate the choices:  

      g_form.addOption('call_type', 'Option 1', 'Option 1', 1);  

      //g_form.addOption('call_type', 'Option 2', 'Option 2', 2);  

      //g_form.addOption('call_type', 'Option 3', 'Option 3', 3);//etc...  

  }   }

Then i started playing with the dependent field but we have a handful of teams and i was forced to duplicate the choice for each team and use the sysid, very time consuming and messy, is there a better way ?

2016-06-08 16_40_09-ServiceNow.png

1 ACCEPTED SOLUTION

Pradeep Sharma
ServiceNow Employee
ServiceNow Employee

HI Mathieu,




I see that you are not fetching the value of assignment group field i.e you should use


var assignment = g_form.getValue('u_assignment_group');


now in your if loop replace compare it based on assignment and also I see that other loops you have used assignment group display value and it should be sys_id.



var assignment = g_form.getValue('u_assignment_group'); //will return sys_id


var assignment = g_form.getDisplayBox('u_assignment_group').value; //will return display value


View solution in original post

26 REPLIES 26

Hi Mathieu,



Still there is error.


Is the field custom and if not then it should be assignment_group


Also make it consistent that is use sys_id everywhere in if loop.


You need to use group for all instances of u_assignment_group in your if statement.


And you are still using names for the value of the group.   Assignment group values are sys_ids.   Nothing else.


Almost there thanks guys !



The script is removing the choice fine now but doesnt add it when the group sys_ID match in the field u_assignment_group



Anything else missing?



function onChange() {


  var group = g_form.getValue('u_assignment_group');//get the value of state


  //var cat = g_form.getValue('category');//get the value of category


  //var com = g_form.getValue('company');//get the value of company


  if (group == "8dd9bdaa37c45e009325dcc773990e25" || group == "8dd9bdaa37c45e009325dcc773990e25" || group == "8dd9bdaa37c45e009325dcc773990e25")/*if group is SA OR ISS OR INF, then:*/   {


  //now populate the choices:


  g_form.addOption('call_type', 'sc_request', 'Request', 1);


  //g_form.addOption('call_type', 'Option 2', 'Option 2', 2);


  //g_form.addOption('call_type', 'Option 3', 'Option 3', 3);//etc...


  return;


  }


  g_form.removeOption('call_type', 'sc_request');


}



Although i would prefer to use group name, is this how getdisplaybox should be used?



function onChange() {


var group = g_form.getDisplayBox('u_assignment_group');//get the value of group


//var cat = g_form.getValue('category');//get the value of category  


//var com = g_form.getValue('company');//get the value of company  


  if (group == "ISS" || group == "SA" || group == "INF")/*if group is SA OR ISS OR INF, then:*/   {  


      //now populate the choices:  


      g_form.addOption('call_type', 'sc_request', 'Request', 1);  


      //g_form.addOption('call_type', 'Option 2', 'Option 2', 2);  


      //g_form.addOption('call_type', 'Option 3', 'Option 3', 3);//etc...  


return;


  }


  g_form.removeOption('call_type', 'sc_request');


}


You are doing onChange of what?   assignment group?   If that is the case, all you have to do is:



function onChange() {


var group = g_form.getDisplayBox('u_assignment_group');//get the value of group


//var cat = g_form.getValue('category');//get the value of category


//var com = g_form.getValue('company');//get the value of company


  if (group == "ISS" || group == "SA" || group == "INF")/*if group is SA OR ISS OR INF, then:*/   {


      //now populate the choices:


      g_form.addOption('call_type', 'sc_request', 'Request', 1);


      //g_form.addOption('call_type', 'Option 2', 'Option 2', 2);


      //g_form.addOption('call_type', 'Option 3', 'Option 3', 3);//etc...


return;


  }else{


            g_form.removeOption('call_type', 'sc_request');


        }


}