Catalog client script not working as expected to hide a variable value

Rhonda9
Tera Expert

Hello ,

 

I need assistance with a catalog client script .  It is partially working. I want to hide "Training Only" variable value from the Make Selection select box if Oracle is selected from the Select Subject Matter select box.   When I select Oracle, it hides "Training Only" but it also hides it when I select any of the other select box variable value choices.   

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

    if (newValue == 'Oracle') {
        //g_form.clearOptions('select_training_subject'); 
        g_form.removeOption('training_type_of_training_services', 'Training Only');
       
    }else{
        //params: variable name, choice value, choice label
        g_form.addOption('training_type_of_training_services', 'Training Only');
       
    }
   
}
 
 
1 ACCEPTED SOLUTION

Community Alums
Not applicable

Hi @Rhonda9 ,

 

The issue you're experiencing is due to the fact that once the option is removed, it's not being added back properly for other selections.

Additionally, the addOption method in your code lacks the required parameters (choice value and choice label).

Here is the updated code please try-

 

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

    if (newValue == 'Oracle') {
        g_form.removeOption('training_type_of_training_services', 'Training Only');
    } else {
        // Ensure 'Training Only' option is added back if not already present
        if (!isOptionPresent('training_type_of_training_services', 'Training Only')) {
            g_form.addOption('training_type_of_training_services', 'Training Only', 'Training Only');
        }
    }
}

// function to check if an option is already present in the select box
function isOptionPresent(variableName, optionValue) {
    var options = g_form.getOption(variableName);
    for (var i = 0; i < options.length; i++) {
        if (options[i].value == optionValue) {
            return true;
        }
    }
    return false;
}

 

 

If my response has resolved your query, please consider giving it a thumbs up ‌‌ and marking it as the correct answer‌‌!


Thanks & Regards,

Sanjay Kumar

View solution in original post

4 REPLIES 4

AnirudhKumar
Mega Sage
Mega Sage

Hey Rhonda,

The best way to handle such requirements is to place the clearOptions() at the top... let it execute first and wipe all the options. And then, conditionally add your desired options using addOption()

removeOption() always causes too much heartache 😉

Community Alums
Not applicable

Hi @Rhonda9 ,

 

The issue you're experiencing is due to the fact that once the option is removed, it's not being added back properly for other selections.

Additionally, the addOption method in your code lacks the required parameters (choice value and choice label).

Here is the updated code please try-

 

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

    if (newValue == 'Oracle') {
        g_form.removeOption('training_type_of_training_services', 'Training Only');
    } else {
        // Ensure 'Training Only' option is added back if not already present
        if (!isOptionPresent('training_type_of_training_services', 'Training Only')) {
            g_form.addOption('training_type_of_training_services', 'Training Only', 'Training Only');
        }
    }
}

// function to check if an option is already present in the select box
function isOptionPresent(variableName, optionValue) {
    var options = g_form.getOption(variableName);
    for (var i = 0; i < options.length; i++) {
        if (options[i].value == optionValue) {
            return true;
        }
    }
    return false;
}

 

 

If my response has resolved your query, please consider giving it a thumbs up ‌‌ and marking it as the correct answer‌‌!


Thanks & Regards,

Sanjay Kumar

Thank you , this works !

Dhruv Chandan
Giga Guru

Hi,

 

"select box" usually captures sys_id value, could you try the following instead 

//Correct code
if(newValue == "<sys_id_of_oracler_record")

// instead of below - incorrect code
//if(newValue == "Oracle")

 

Hope this helps.

 

Thanks,
Dhruv