How to get the value of g_form.addOption stored in a variable and then alert value on the screen

Esteban_Neri
Tera Contributor

Hello everyone,

 

My team created a select box with dynamic options using a onChange Client Script. I would like to be able to store the selected value in a variable and use it to auto-populate other field. I'm struggling to achieve this, any help provided will be greatly appreciated.

 

AddOption client script:

 

function onChange(control, oldValue, newValue, isLoading) {
    if (isLoading || newValue == '') {
        g_form.clearOptions('sub_category');
        g_form.addOption('sub_category', '', '-- None --');
        g_form.setVisible('sub_category', false);
        return;
    }

    //Type appropriate comment here, and begin script below
    var cat = newValue;
    g_form.clearOptions('sub_category');
    g_form.setVisible('sub_category', true);
    g_form.addOption('sub_category', '', '-- None --');
    g_form.setMandatory('sub_category', true);
   
    if (cat === 'Remediation') {
        g_form.addOption('sub_category', 'Metrics', 'Metrics');
        g_form.addOption('sub_category', 'Training', 'Training');
        g_form.addOption('sub_category', 'Governance', 'Governance');
        g_form.addOption('sub_category', 'Other', 'Other');
    } else if (cat === 'Quality') {
        g_form.addOption('sub_category', 'Management', 'Management');
        g_form.addOption('sub_category', 'Quality', 'Quality');
        g_form.addOption('sub_category', 'Other', 'Other');
    }
}
1 ACCEPTED SOLUTION

Since kb_article is a reference variable in the Catalog Item, drop the third argument

 

if (scat == 'metadata_management') {
        g_form.setValue('kb_article', 'c8b6a62413f81300004b32228144b06c');
}

 

If it's still not working as expected, alert on newValue/scat to confirm the value you are using in the if condition is exactly correct.

There's not really a point in assigning newValue to another script variable, so your script could just be

function onChange(control, oldValue, newValue, isLoading) {
    if (isLoading || newValue == '') {
        g_form.setVisible('kb_article', false);
        return;
    }

    g_form.setVisible('kb_article', true);

    if (newValue == 'metadata_management') {
        g_form.setValue('kb_article', 'c8b6a62413f81300004b32228144b06c');
     }
}

 

View solution in original post

6 REPLIES 6

Brian Lancaster
Tera Sage

Why are you using the triple equals sign in your if statement? Note sure if that is the problem but I would start there as the rest of your code looks fine.

 

Edit: Why are you using a script on subcategory? You can just make it depending on category and fill in the depending value field on the subcategory choice list.

Quality control checks enforced requested triple equal sign on If statements.

 

The subcategory list is going to be a lot bigger, (way more categories and subcategories) and must be populated based on "Category" field. That is why we did not created the options directly in the Select Box Variable.

Brad Bowman
Kilo Patron
Kilo Patron

I don't think I'm following.  Is this script working to dynamically add options to the subcategory select box?  The selected value would be stored in the subcategory variable, so why do you need to store it elsewhere?  If you want to populate another variable based on the selected subcategory value, then create an onChange Catalog Client Script when subcategory changes. 

I tried to create a new OnChange Client Script for getting the subcategory value and use it to dynamically display a KB number in a reference field. The first step is get the sub_category value to use it on an If statement. However this is not working.

 

The new OnChange script is as follows:

 

function onChange(control, oldValue, newValue, isLoading) {
    if (isLoading || newValue == '') {
        g_form.setVisible('kb_article', false);
        return;
    }

    //Type appropriate comment here, and begin script below

    var scat = newValue;    
    g_form.setVisible('kb_article', true);

    if (scat == 'metadata_management') {
        g_form.setValue('kb_article', 'c8b6a62413f81300004b32228144b06c', 'KB0010001');
        //'kb_article' is a referenced field.
    }

}