Built something you're proud of? Tell the story. A quick G2 review of App Engine or Build Agent helps other developers see what's possible on ServiceNow. Share your experience.

Logic for dependent fields and user criteria

Ashutosh4
Tera Expert

Hello all, 

I have a requirement where i have to create two fields, 
Category and sub category 

Where we have dependency , example If category is having options a, b, c and sub category has values a1,a2,a3,b1,b2,b3.....


If a is selected a1,a2,a3 should be visible and so on.

This is taken care.

But now there is also an ask where i have to restrict the drop downs based on if a user is present in a group (HR ops as example )or not, or if the company name matches with = 'xyz' then it should limit few options.

Like if I am part of the group HR ops, 

Then I should be able to see a1 if a is selected, and if a user is there from other company again with using logic he should see only a2, if a user qualifies both criteria he should see a1 a2 and a3 , something like this.


Any help is highly appreciated.


3 REPLIES 3

UmesH77
Tera Guru

Hello @Ashutosh4 , 

You can do this , 

Clear sub-category options
• Re-add only allowed values based on:

  • Selected category

  • User group

  • User company

 

you can use Onchange  client script for achiving this , 

as In provided some , use it as your requirement

 

 

function onChange(control, oldValue, newValue, isLoading) {
if (isLoading || !newValue) {
return;
}

// Clear existing sub-category options
g_form.clearOptions('sub_category');

var category = newValue;

// User conditions
var isHROps = g_user.hasRole('hr_ops'); // replace with group logic if needed
var isXYZCompany = (g_user.company == 'xyz');

if (category == 'a') {

// HR Ops users
if (isHROps) {
g_form.addOption('sub_category', 'a1', 'A1');
}

// Company xyz users
if (isXYZCompany) {
g_form.addOption('sub_category', 'a2', 'A2');
}

// Users matching both conditions
if (isHROps && isXYZCompany) {
g_form.addOption('sub_category', 'a1', 'A3');
g_form.addOption('sub_category', 'a2', 'A2');
g_form.addOption('sub_category', 'a3', 'A3');
}
}
}

 

if my response is helpfull 🙏, please mark it as helpfull  and accept it as solution.

 

Regards , 

Umesh

hey, I tried but its failing, is it due to MY variable type?

My variable type is lookup select box.

And then taking reference from sys_choice and filtering it.

Tanushree Maiti
Kilo Patron

Hi @Ashutosh4 ,

 

If you want subcategory population based on Category selection, a. under subcategory dictionary, you need to mark it is dependent on Catgory and b. in the sys_choice table properly define the dependent value (category)

 

e.g you needs this change in incident table.

Go to the incident form open the config dictionary of sub category . 

1. Do this

 

 

2) under choice tab ,You can see list of choices . mention the dependent field like below from category. 

 

 

TanushreeMaiti_0-1772015897568.png

 

 

Please mark this response as Helpful & Accept it as solution if it assisted you with your question.
Regards
Tanushree Maiti
ServiceNow Technical Architect
Linkedin: