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

Hi Ali its working for software and security but its not working for Hardware this is the coide below

 

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: " + subcategory);

var subcategories = [];

switch (category) {
case 'soft':
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_catagory');

// 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_catagory', value, label);
}

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

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

Amit Gujarathi
Giga Sage
Giga Sage

Hi @Yougander patel ,
I trust you are doing great.
Please make a note of below points which on the script

  1. Ensure that the variable names for Category and Subcategory are correct in the code.

  2. Make sure that the client script is set to run on the change of the Category field.

  3. You can improve the performance of the client script by checking if the new Category value is different from the old one before executing the 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 if there is any
  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]);
  }
}

 

Please mark the answer correct if its helpful.

Regards,

Amit Gujarathi


Was this answer helpful?


Please consider marking it correct or helpful.


Your feedback helps us improve!


Thank you!


Regards,


Amit Gujrathi



showing error in JavaScript browser console