The CreatorCon Call for Content is officially open! Get started here.

Client script getOptions() issue (field choices not resetting when changing onChange field)

Bhojraj Chauhan
Tera Contributor

I have create an onChange client script on 'type' field with the logic to display choices in choice field 'suspension_reason'. 

function onChange(control, oldValue, newValue, isLoading, isTemplate) {
    if (isLoading || newValue === '') {
        return;
    }
  
    //Type appropriate comment here, and begin script below
    var internal1 = g_scratchpad.internalreasons;
    var external1 = g_scratchpad.externalreasons;
    var type1 = g_form.getValue('type');

    internal1 = internal1.split(',');
    external1 = external1.split(',');
    // Add the appropriate options based on the selected type
    if (type1 == 'Internal Suspend') {
        g_form.setValue('your_choice_field', '');
        for (var j1 = 0; j1 < external1.length; j1++) {
            g_form.removeOption('suspension_reason', external1[j1]);
        }
        for (var k1 = 0; k1 < internal1.length; k1++) {
            g_form.addOption('suspension_reason', internal1[k1]);
        }
    } else if (type1 == 'External Suspend') {
        g_form.setValue('your_choice_field', '');
        for (var l1 = 0; l1 < internal1.length; l1++) {
            g_form.removeOption('suspension_reason', internal1[l1]);
        }
        for (var m1 = 0; m1 < external1.length; m1++) {
            g_form.addOption('suspension_reason', external1[m1]);
        }
    }
    g_form.clearValue('suspension_reason');
}


Here, 

When I select type as Internal Suspend, the choices in suspension_reason fields show correctly. But when I change the type from Internal Suspend to External Suspend, the choices show 'None'. 
Same happens vice versa, when I select External Suspend, the choices in suspension_reason show properly, but when I reselect the type field as Internal Suspend, the choices show 'None'. 
What changes can I make to make it work as expected.
I have tried adding  g_form.clearOptions('suspension_reason'); before if/else if conditions doesnt work. 
Please drop if you guys have any suggestions to make.


 

7 REPLIES 7

YaswanthKurre
Tera Guru

Hi @Bhojraj Chauhan ,

 

You're very close, but the issue is due to how you're managing the dynamic options for the suspension_reason field using g_form.removeOption() and g_form.addOption().

 

When switching between the "Internal Suspend" and "External Suspend" types, you're only removing the options from the other set — not clearing and fully re-adding all expected options cleanly.

 

  • You never fully clear all options before adding the new set, leading to stale or missing options.

  • You’re adding options without specifying a label or value if internal1 or external1 contain objects or values that aren't strings.

  • g_form.setValue('your_choice_field', '') should be replaced with the actual variable name: 'suspension_reason'.

Updated script:

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

    var internalList = g_scratchpad.internalreasons || '';
    var externalList = g_scratchpad.externalreasons || '';
    var type = g_form.getValue('type');
    var field = 'suspension_reason';

    // Ensure the values are arrays
    var internalArray = internalList.split(',');
    var externalArray = externalList.split(',');

    // Clear all existing options first
    g_form.clearOptions(field);

    // Add a default/placeholder option
    g_form.addOption(field, '', '-- None --');

    if (type === 'Internal Suspend') {
        for (var i = 0; i < internalArray.length; i++) {
            var value = internalArray[i].trim();
            g_form.addOption(field, value, value);
        }
    } else if (type === 'External Suspend') {
        for (var j = 0; j < externalArray.length; j++) {
            var value = externalArray[j].trim();
            g_form.addOption(field, value, value);
        }
    }

    // Always reset the field to blank to avoid selecting old value
    g_form.setValue(field, '');
}

 

Mark this as helpful and correct, if this helps you.

 

Thanks,

Yaswanth

Hi, thank you for your help
Its not displaying values for the first time using this script. The dropdown is not displaying any choices. 

  • g_form.setValue('your_choice_field', '') should be replaced with the actual variable name: 'suspension_reason'.

I had corrected this point but no progress too sadly.

 

Darris Cooper
Tera Contributor

Hello Bhojraj,

 

In your description, you mention two field names ('type', and 'supension_reason'), and of those two fields the onChange script is triggered based on the 'type' selection. I'm not sure if there is more happening with the 'your_choice_field' from lines 15 and 23, but the g_form.clearValues() on the last line would render the dropdown empty with every selection.

 

My suggestion would be to use the clearOptions() before the other logic, and remove the loops for removing the options. Something like this...

 

function onChange(control, oldValue, newValue, isLoading, isTemplate) {
    if (isLoading)
        return;

    g_form.clearOptions('suspension_reason');

    if (newValue === '' || newValue == oldValue)
        return;
  
    var internal1 = g_scratchpad.internalreasons;
    var external1 = g_scratchpad.externalreasons;
    var reasonList = (newValue == 'Internal Suspend') ? internal1 : external1;

    getOptions(reasonList);
}

function getOptions(list) {
    var options = list.split(',');
    var len = options.length;
    // Add the appropriate options based on the selected type
    for (var i=0; i<len; i++) {
        g_form.addOption('suspension_reason', options[i]);
    }
}

 

If my response helped, please mark it as the accepted solution so others can benefit as well.

 

Thanks,

Coop

Hi @Darris Cooper , clearOptions is not working for this form as it is clearing all the choices from the first iteration itself, please suggest some alternative in place of clearOptions() for removing the options and adding in a fresh set. this will fix the  script