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

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');
     }
}

 

I replicated the scripts on my personal instance and it works, Not sure why is not working in my company's DEV instance. Probably some Knowledge DB access or ACL.

 

Thanks for the advise. I really appreciate your support