Logic to Show/ Hide select box choices based on the requester Department

ALi64
Giga Contributor

Hello,

Is there anyone manged to achieve this requirement, i need to build logic to validate the requester Department so once user open portal services, i need to bale to show/hide select box choices based on his department, i came to know that this is applicable by create catalog client scrip and script include, please advise how to design this logic.

 

Thanks in advance.

1 ACCEPTED SOLUTION

miked_jones
Giga Expert

How you are populating your select box with choices and whether you are willing to add a department field to the form will somewhat dictate what your best approach would be. 

You could do this by adding a field to the form (does not need to be visible to the user) and setting the default value using gs.getUser().getDepartmentID() which would get the department of the logged in user. If you don't want to add a field to the form, then you may need to write a GlideAjax call to return the correct value into a variable. 

Once you have the value (either in field, or in pure variable) the next part depends on how you populate your select box. 

If the values are hard-coded on the variable itself, then you will need to write a client-script to remove all of the options with g_form.clearOptions() and then add the options you want back with g_form.addOption() based on the value of your field, or your GlideAjax return.  

If the values are pulled from a table (lookup select box) you could add a department field to the table and then adjust your reference qualifier to only return values where the department matches gs.getUser().getDepartmentID().

Hope that helps. 

 

 

View solution in original post

4 REPLIES 4

Shubham Kakade
Giga Guru

Hello Ali,

 

You can do this with the help of a client script also as mentioned below:

 

Write an on change Client Script on the Department field and write the below script:

 

 Stud code example for onchange of Category field.  //Change this code according to your requirements and implement the following logic

function onChange(control, oldValue, newValue, isLoading) {

 

  if (isLoading || newValue == '') {

 

  return;

 

  }

 

  var cat = g_form.getValue('Category');

 

  if(newValue == 'New Category Value')           // Replace the New Category Value with the value you want to have the validation

 

  {

 

  g_form.clearOptions('Field Name');

 

  g_form.addOption('u_Subcategory', 'choiceValue', 'choiceLabel ');

 

  g_form.addOption('u_Subcategory', 'choiceValue', 'choiceLabel ');

 

    }

 

else if(newValue == 'New Category Value')

 

{

 

g_form.clearOptions('Field Name');

 

  g_form.addOption('u_Subcategory', 'choiceValue', 'choiceLabel ');

 

  g_form.addOption('u_Subcategory', 'choiceValue', 'choiceLabel ');

 

      }

 

 

 

 

 

Hope this helps.Mark the answer as correct/helpful if this helps you.

 

 Thank you

 

Regards,

Shubham

Hello Shubham,

 

There is not Department filed in the request, i need to get this information from the user profile which already stored in user table.

 

So once the requester login to service now and try to open portal to submit the service and after select the service the logic will check the user department from his profile in the user table then show the required choices.

 

Hope this clarify me question.

 

ALi64
Giga Contributor

Hello Shubham,

 

There is not Department filed in the request, i need to get this information from the user profile which already stored in user table.

 

So once the requester login to service now and try to open portal to submit the service and after select the service the logic will check the user department from his profile in the user table then show the required choices.

 

Hope this clarify me question.

miked_jones
Giga Expert

How you are populating your select box with choices and whether you are willing to add a department field to the form will somewhat dictate what your best approach would be. 

You could do this by adding a field to the form (does not need to be visible to the user) and setting the default value using gs.getUser().getDepartmentID() which would get the department of the logged in user. If you don't want to add a field to the form, then you may need to write a GlideAjax call to return the correct value into a variable. 

Once you have the value (either in field, or in pure variable) the next part depends on how you populate your select box. 

If the values are hard-coded on the variable itself, then you will need to write a client-script to remove all of the options with g_form.clearOptions() and then add the options you want back with g_form.addOption() based on the value of your field, or your GlideAjax return.  

If the values are pulled from a table (lookup select box) you could add a department field to the table and then adjust your reference qualifier to only return values where the department matches gs.getUser().getDepartmentID().

Hope that helps.