Category and Subcategory in Record Producer

Yougander patel
Tera Contributor

i have a variable called category with choices software ,hardware and Security and i have another variable called subcategory with choices email, phone, iPad, keyboard, monitor, Spam so here when i select category = software then in subcategory choices should visible only email and phone ,when i select category= hardware then subcategory choices should only visible iPad, keyboard, monitor and when i select category =security then i should select only spam this can be done by using Script Include and On change client script.

 

Can please review my code it not working and can i get correct code for script include and On change client script

 

script include:

var CategoryUtils = Class.create();

CategoryUtils.prototype = {
initialize: function() {},

getSubcategories: function(category) {
var subcategories = [];

switch (category) {
case 'software':
subcategories = ['email', 'phone'];
break;
case 'hardware':
subcategories = ['iPad', 'keyboard', 'monitor'];
break;
case 'security':
subcategories = ['Spam'];
break;
default:
subcategories = [];
}

return subcategories;
},

type: 'CategoryUtils'
};

 

 

Onchange Client Script :

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

var category = g_form.getValue('category');
var subcategory = g_form.getValue('subcategory');

// Get the subcategories for the selected category
var categoryUtils = new CategoryUtils();
var subcategories = categoryUtils.getSubcategories(category);

// Clear the existing subcategory choices
g_form.clearOptions('subcategory');

// Add the new subcategory choices
for (var i = 0; i < subcategories.length; i++) {
var value = subcategories[i];
var label = subcategories[i];

g_form.addOption('subcategory', value, label);
}

// Set the subcategory to the first option if it's no longer valid
if (subcategories.indexOf(subcategory) === -1) {
g_form.setValue('subcategory', subcategories[0]);
}
}

1 ACCEPTED SOLUTION

code is correct. check backend value of hardware choice value. What are you getting in alert when you select hardware category? also check if the choice value has any white spaces.

If I could help you with your Query then, please hit the Thumb Icon and mark my answer as Correct!!

Thank you,
Ali

View solution in original post

13 REPLIES 13

Ahmmed Ali
Mega Sage

Hello @Yougander patel 

 

Try below client script:

 

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

var category = g_form.getValue('category');
var subcategory = g_form.getValue('subcategory');

 

var subcategories = [];

switch (category) {
case 'software':
subcategories = ['email', 'phone'];
break;
case 'hardware':
subcategories = ['iPad', 'keyboard', 'monitor'];
break;
case 'security':
subcategories = ['Spam'];
break;
default:
subcategories = [];

 

// Clear the existing subcategory choices
g_form.clearOptions('subcategory');

// Add the new subcategory choices
for (var i = 0; i < subcategories.length; i++) {
var value = subcategories[i];
var label = subcategories[i];

g_form.addOption('subcategory', value, label);
}

// Set the subcategory to the first option if it's no longer valid
if (subcategories.indexOf(subcategory) === -1) {
g_form.setValue('subcategory', subcategories[0]);
}
}

 

 

 

 

Issues with your scripts:

1. Script include is not client callable

2. You need to user GlideAjax to call server side script from client script

 

 

 

 

 

If I could help you with your Query then, please hit the Thumb Icon and mark my answer as Correct!!

Thank you,
Ali

The given code above is not working Ali can you please check my code given below as took on change client script and my variable "what_are_you_reporting_th_isssue_with" and i used below script but its not working

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

var category = g_form.getValue('what_are_you_reporting_the_issue_with');
var subcategory = g_form.getValue('sub_catagory');

var subcategories = [];

switch (category) {
case 'software':
subcategories = ['email', 'eway', 'password reset', 'proclaim', 'visual files'];
break;
case 'hardware':
subcategories = ['iPad', 'keyboard', 'monitor', 'headset', 'laptop', 'mobile phone', 'mouse', 'monitor', 'pc', 'printer', 'phone', 'scanner', 'thin client', 'WiFi'];
break;
case 'security':
subcategories = ['spam', 'phishing', 'emailquarantinerelease', 'other'];
break;
default:
subcategories = [];
}

// Clear the existing subcategory choices
g_form.clearOptions('sub_category');

// Add the new subcategory choices
for (var i = 0; i < subcategories.length; i++) {
var value = subcategories[i];
var label = subcategories[i];

g_form.addOption('sub_category', value, label);
}

// Set the subcategory to the first option if it's no longer valid
if (subcategories.indexOf(subcategory) === -1) {
g_form.setValue('sub_category', subcategories[0]);
}
}

Hello @Yougander patel 

 

Are the category and subcategory backend values are correct? scripts should use backend values, not labels for choice values.

 

Can you update code as below highlighted with alerts and let me know what alerts you are getting:

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

var category = g_form.getValue('what_are_you_reporting_the_issue_with');

alert("Selected category: " + category);
var subcategory = g_form.getValue('sub_catagory');

alert("Selected subcategory: " + category);

var subcategories = [];

switch (category) {
case 'software':
subcategories = ['email', 'eway', 'password reset', 'proclaim', 'visual files'];
break;
case 'hardware':
subcategories = ['iPad', 'keyboard', 'monitor', 'headset', 'laptop', 'mobile phone', 'mouse', 'monitor', 'pc', 'printer', 'phone', 'scanner', 'thin client', 'WiFi'];
break;
case 'security':
subcategories = ['spam', 'phishing', 'emailquarantinerelease', 'other'];
break;
default:
subcategories = [];
}

 

alert("Sub categories computed: " + subcategories.join(","));

 

// Clear the existing subcategory choices
g_form.clearOptions('sub_category');

// Add the new subcategory choices
for (var i = 0; i < subcategories.length; i++) {
var value = subcategories[i];
var label = subcategories[i];

g_form.addOption('sub_category', value, label);
}

// Set the subcategory to the first option if it's no longer valid
if (subcategories.indexOf(subcategory) === -1) {
g_form.setValue('sub_category', subcategories[0]);
}
}

If I could help you with your Query then, please hit the Thumb Icon and mark my answer as Correct!!

Thank you,
Ali

if i select software ; alert's are Selected category: soft,Selected subcategory: soft ,Sub categories computed: 

and there no change in subcategory when i select software ,iam seeing all the choices still