Hide/Show a variable

dev_K
Tera Contributor

Hi All,

 

 

In my catalog item I have a list collector, where one of the options is 'other'. I would like to show (and make mandatory) a string field 'other consumer' when this option is selected.

 

Currently I have a client script, but when I select 'other' the field 'other consumer' does not show:

 

 

dev_K_3-1723621187587.png

 

 

 

 

 

Here is the condition:

 

dev_K_2-1723621085009.png

 

1 ACCEPTED SOLUTION

Rajesh Chopade1
Mega Sage

Hi @dev_K 

use following script, which is working fine:

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

    // Get the values from the list collector
    var selectedValues = g_form.getValue('consumer_list');
    
    // Check if 'Other' is selected
    if (selectedValues.indexOf('Other') !== -1) {
        // Show and make mandatory the 'other_consumer' field
        g_form.setDisplay('other_consumer', true);
        g_form.setMandatory('other_consumer', true);
    } else {
        // Hide and make not mandatory the 'other_consumer' field
        g_form.setDisplay('other_consumer', false);
        g_form.setMandatory('other_consumer', false);
    }
}

If you want to check your script, please share here to verify.

I hope my answer helps you to resolve your issue, if yes please mark my answer helpful and correct.

Thank you

rajesh.

View solution in original post

6 REPLIES 6

Community Alums
Not applicable

Hi @dev_K ,

Can you share the client script which you are using?

 

Rajesh Chopade1
Mega Sage

Hi @dev_K 

use following script, which is working fine:

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

    // Get the values from the list collector
    var selectedValues = g_form.getValue('consumer_list');
    
    // Check if 'Other' is selected
    if (selectedValues.indexOf('Other') !== -1) {
        // Show and make mandatory the 'other_consumer' field
        g_form.setDisplay('other_consumer', true);
        g_form.setMandatory('other_consumer', true);
    } else {
        // Hide and make not mandatory the 'other_consumer' field
        g_form.setDisplay('other_consumer', false);
        g_form.setMandatory('other_consumer', false);
    }
}

If you want to check your script, please share here to verify.

I hope my answer helps you to resolve your issue, if yes please mark my answer helpful and correct.

Thank you

rajesh.

dev_K
Tera Contributor

Hi @Rajesh Chopade1 

 

Actually I used this script, and replaced the names of the variables but it is not working:

 

dev_K_0-1723625396587.png

 

 

Is getValue() a right method of retrieving values from the list collector??

Hi @dev_K 

Yes, getValue() is working fine with list collector, you can verify with following code:

// Get the sys_id string of selected items in the list collector
    var selectedValues = g_form.getValue('list_collector_variable_name');
    
    // Convert the comma-separated string into an array
    var selectedArray = selectedValues.split(',');

    // Example of how to iterate over the array
    for (var i = 0; i < selectedArray.length; i++) {
        console.log('Selected sys_id:', selectedArray[i]);
    }

 

i hope this clarifies, if any issue facing let me know.

thank you