Client script to set category sub category

avinashdubey103
Tera Guru

Hi , I have a requirement to  have only specific values on dropdown of subcategory when a specific category is selected

i have created a client script for that :

 

But when i am selecting for the first time it is working fine but for the second time , it is giving me first selection values and latest selection values both in subcategory dropdown.

 

 

 

 

function onChange(control, oldValue, newValue, isLoading) {
if (isLoading || newValue == '') {

return;
}
if (newValue == 'IAM') {
g_form.addOption('subcategory', 'Add permission', 'Add permission');
g_form.addOption('subcategory', 'Create new policy', 'Create new policy');
g_form.addOption('subcategory', 'Roles', 'Roles');
g_form.addOption('subcategory', 'Policies', 'Policies');
g_form.addOption('subcategory', 'Assume role', 'Assume role');
g_form.addOption('subcategory', 'Other', 'Other');

}

if (newValue == 'VM Instances') {
g_form.addOption('subcategory', 'Login issue', 'Login issue');
g_form.addOption('subcategory', 'Permissions ', 'Permissions ');
g_form.addOption('subcategory', 'Images', 'Images');
g_form.addOption('subcategory', 'Manage Instance group (MIG) statfull', 'Manage Instance group (MIG) statfull');
g_form.addOption('subcategory', 'Manage Instance group (MIG) statless', 'Manage Instance group (MIG) statless');
g_form.addOption('subcategory', 'Unmanage Instance group (UMIG)', 'Unmanage Instance group (UMIG)');
g_form.addOption('subcategory', 'Images', 'Images');
g_form.addOption('subcategory', 'Filestore', 'Filestore');
g_form.addOption('subcategory', 'Reserved static IP', 'Reserved static IP');
g_form.addOption('subcategory', 'Instances', 'Instances');
g_form.addOption('subcategory', 'Load Balancers', 'Load Balancers');
g_form.addOption('subcategory', 'Firewall rules', 'Firewall rules');
g_form.addOption('subcategory', 'Snapshots ', 'Snapshots ');
g_form.addOption('subcategory', 'disks', 'disks');
g_form.addOption('subcategory', 'Agents', 'Agents');
g_form.addOption('subcategory', 'Machine type', 'Machine type');
g_form.addOption('subcategory', 'Tags', 'Tags');
g_form.addOption('subcategory', 'Status', 'Status');
g_form.addOption('subcategory', 'Other ', 'Other ');


}

if (newValue == 'GKE') {
g_form.addOption('subcategory', 'Permissions ', 'Permissions ');
g_form.addOption('subcategory', 'Other ', 'Other ');
}

if (newValue == 'Cloud Storage') {
g_form.addOption('subcategory', 'Permissions ', 'Permissions ');
g_form.addOption('subcategory', 'Bucket', 'Bucket');
g_form.addOption('subcategory', 'Other ', 'Other ');

}

if (newValue == 'Cloud DNS') {
g_form.addOption('subcategory', 'Permissions ', 'Permissions ');
g_form.addOption('subcategory', 'Firewall rules', 'Firewall rules');
g_form.addOption('subcategory', 'Other ', 'Other ');

}

if (newValue == 'Cloud Function') {
g_form.addOption('subcategory', 'Add permission', 'Add permission');

g_form.addOption('subcategory', 'Other ', 'Other ');
}

if (newValue == 'VPC') {
g_form.addOption('subcategory', 'Create a new VPC', 'Create a new VPC');

g_form.addOption('subcategory', 'Add subnets', 'Add subnets');
g_form.addOption('subcategory', 'Other ', 'Other ');

}

if (newValue == 'logging & Monitoring') {
g_form.addOption('subcategory', 'Alarms ', 'Alarms ');
g_form.addOption('subcategory', 'Other ', 'Other ');
}

if (newValue == 'Other') {
g_form.addOption('subcategory', 'Permissions ', 'Permissions ');
g_form.addOption('subcategory', 'Other ', 'Other ');
}

//Type appropriate comment here, and begin script below


}

 

 

avinashdubey103_0-1678094309133.png

 

1 ACCEPTED SOLUTION

Sails
Kilo Sage

Hi @avinashdubey103 
The easiest way to get this done is
Create dependency between category & subcategory

Go to the dictionary entry of the 'subcategory' field, navigate to the choices, personalise list view to add the dependent value--> add the 'value' of Category
Let me know if this works and mark the answer helpful

 

Thanks

Sails

Sails
ServiceNow Consultant
United Kingdom

View solution in original post

3 REPLIES 3

Aavi
Mega Guru

Hi @avinashdubey103 

Please try the below script:

 

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

return;
}
if (newValue == 'IAM') {

g_form.clearValue('subcategory');
g_form.addOption('subcategory', 'Add permission', 'Add permission');
g_form.addOption('subcategory', 'Create new policy', 'Create new policy');
g_form.addOption('subcategory', 'Roles', 'Roles');
g_form.addOption('subcategory', 'Policies', 'Policies');
g_form.addOption('subcategory', 'Assume role', 'Assume role');
g_form.addOption('subcategory', 'Other', 'Other');

}

if (newValue == 'VM Instances') {

g_form.clearValue('subcategory');
g_form.addOption('subcategory', 'Login issue', 'Login issue');
g_form.addOption('subcategory', 'Permissions ', 'Permissions ');
g_form.addOption('subcategory', 'Images', 'Images');
g_form.addOption('subcategory', 'Manage Instance group (MIG) statfull', 'Manage Instance group (MIG) statfull');
g_form.addOption('subcategory', 'Manage Instance group (MIG) statless', 'Manage Instance group (MIG) statless');
g_form.addOption('subcategory', 'Unmanage Instance group (UMIG)', 'Unmanage Instance group (UMIG)');
g_form.addOption('subcategory', 'Images', 'Images');
g_form.addOption('subcategory', 'Filestore', 'Filestore');
g_form.addOption('subcategory', 'Reserved static IP', 'Reserved static IP');
g_form.addOption('subcategory', 'Instances', 'Instances');
g_form.addOption('subcategory', 'Load Balancers', 'Load Balancers');
g_form.addOption('subcategory', 'Firewall rules', 'Firewall rules');
g_form.addOption('subcategory', 'Snapshots ', 'Snapshots ');
g_form.addOption('subcategory', 'disks', 'disks');
g_form.addOption('subcategory', 'Agents', 'Agents');
g_form.addOption('subcategory', 'Machine type', 'Machine type');
g_form.addOption('subcategory', 'Tags', 'Tags');
g_form.addOption('subcategory', 'Status', 'Status');
g_form.addOption('subcategory', 'Other ', 'Other ');


}

if (newValue == 'GKE') {

g_form.clearValue('subcategory');
g_form.addOption('subcategory', 'Permissions ', 'Permissions ');
g_form.addOption('subcategory', 'Other ', 'Other ');
}

if (newValue == 'Cloud Storage') {
g_form.addOption('subcategory', 'Permissions ', 'Permissions ');
g_form.addOption('subcategory', 'Bucket', 'Bucket');
g_form.addOption('subcategory', 'Other ', 'Other ');

}

if (newValue == 'Cloud DNS') {
g_form.addOption('subcategory', 'Permissions ', 'Permissions ');
g_form.addOption('subcategory', 'Firewall rules', 'Firewall rules');
g_form.addOption('subcategory', 'Other ', 'Other ');

}

if (newValue == 'Cloud Function') {

g_form.clearValue('subcategory');
g_form.addOption('subcategory', 'Add permission', 'Add permission');

g_form.addOption('subcategory', 'Other ', 'Other ');
}

if (newValue == 'VPC') {

g_form.clearValue('subcategory');
g_form.addOption('subcategory', 'Create a new VPC', 'Create a new VPC');

g_form.addOption('subcategory', 'Add subnets', 'Add subnets');
g_form.addOption('subcategory', 'Other ', 'Other ');

}

if (newValue == 'logging & Monitoring') {
g_form.addOption('subcategory', 'Alarms ', 'Alarms ');
g_form.addOption('subcategory', 'Other ', 'Other ');
}

if (newValue == 'Other') {

g_form.clearValue('subcategory');
g_form.addOption('subcategory', 'Permissions ', 'Permissions ');
g_form.addOption('subcategory', 'Other ', 'Other ');
}

//Type appropriate comment here, and begin script below


}

hi , still facing the same issue after updating the code

Sails
Kilo Sage

Hi @avinashdubey103 
The easiest way to get this done is
Create dependency between category & subcategory

Go to the dictionary entry of the 'subcategory' field, navigate to the choices, personalise list view to add the dependent value--> add the 'value' of Category
Let me know if this works and mark the answer helpful

 

Thanks

Sails

Sails
ServiceNow Consultant
United Kingdom