Catalog Client Script based on whether Label is Visible

Morgan House
Kilo Contributor

Hello all! I am newer to the scripting side of ServiceNow and I am making a form for users that has 32 checkbox variables available when a Label based on a UIPolicy is visible, but I need it so that users must select 2, but can not select more than that. Currently the error is "Error MessageonSubmit script error: TypeError: Cannot read properties of null (reading 'findTabIndexByName'): function () { [native code] }" I am almost positive it's lines 2 and 3, but unsure how to edit it to use the SectionVisible unless there is a better code. How can I fix this? Thank you so much in advance!

 

function onSubmit() {
var wkei = g_form.isSectionVisible('warlock_eldritch_invocations');
if (wkei == true){
var mandatoryVars = 'eldritch_invocations_agonizing_blast,eldritch_invocations_armor_shadows,eldritch_invocations_ascendant_step,eldritch_invocations_beast_speech,eldritch_invocations_beguiling_influence,eldritch_invocations_bewitching_whispers,eldritch_invocations_book_ancient_secrets,eldritch_invocations_chains_carceri,eldritch_invocations_devils_sight,eldritch_invocations_dreadful_word,eldritch_invocations_eldritch_sight,eldritch_invocations_eldritch_spear,eldritch_invocations_eyes_rune_keeper,eldritch_invocations_fiendish_vigor,eldritch_invocations_gaze_two_minds,eldritch_invocations_lifedrinker,eldritch_invocations_mask_many_faces,eldritch_invocations_master_myriad_forms,eldritch_invocations_minions_chaos,eldritch_invocations_mire_mind,eldritch_invocations_misty_visions,eldritch_invocations_one_shadows,eldritch_invocations_otherworldly_leap,eldritch_invocations_repelling_blast,eldritch_invocations_sculptor_flesh,eldritch_invocations_sign_ill_omen,eldritch_invocations_thief_five_fates,eldritch_invocations_thirsting_blade,eldritch_invocations_visions_distant_realms,eldritch_invocations_voice_chain_master,eldritch_invocations_whispers_grave,eldritch_invocations_witch_sight';
var mandatoryCount = 2;
var maxCount = 2;

var passed = forceMandatoryCheckboxes(mandatoryVars, mandatoryCount);
if (!passed) {
//Abort the submit
alert('Please select ' + '2 Eldritch Invocations.');
return false;
}
function forceMandatoryCheckboxes(mandatory, count) {
mandatory = mandatory.split(',');
var cnt = 0;
for (var i = 0; i < mandatory.length; i++) {
if (g_form.getValue(mandatory[i]) == 'true') {
cnt++;
if (cnt > count) {
return false;
}
}
}
return (cnt == count);
}
}
}

3 REPLIES 3

eumak
Tera Guru

Hello @Morgan House ,

2-3 lines are creating problem, other than that Code seems to be correct. Can you tell what type of variable is this "warlock_eldritch_invocations" ? Can you please tell what is your problem statement with the UI policy?

Via alert please check. What below line of code is returning?

var wkei = g_form.isSectionVisible('warlock_eldritch_invocations');
alert("the alert is -" + wkei);

This alert will check whether you are getting true or something else.

 
Suggestion - If you want to make checkboxes visible on the basis of the particular variable visible, include the checkbox into the same UI policy.

Cheers..!
Happy Learning:)
Tushar

Mark it as helpful or correct, If Applicable


Cheers..!

Happy Learning:)

Tushar

Thank you Tushar for your suggestions. I already have a UIPolicy regarding showing the Label and Checkboxes depending on other choices, but I need this script for the mandatory amounts. I am trying to use this script to be based on whether the Label is showing since otherwise, it won't allow the form to be submitted at all. The UIPolicy itself has no issue and it is only the Client Catalog Script basing it off the Label.

 

Thank you!

Hitoshi Ozawa
Giga Sage
Giga Sage

Hi Morgan,

As Tushar replied, the error is cause by "isSectionVisible()" step.

isSectionVisible() is used to check if the section is visible or not but there's no section in the form.

What to do when requiring users to select 2 items is to make the first checkbox mandatory. This will make at least checking one of the checkbox mandatory.

In the UI Policy, instead of making label mandatory or not, set the first checkbox mandatory or not and if required, hide and display the checkboxes.

Following is an example on how I would do this.

1. Variable definition. I've only declard 4 checkboxes. Field "Checkboxes mandatory" field is used to specify if the checkboxes are mandatory or not. This can be replaced with what's in the UI Policy condition.

find_real_file.png

2. onChange script on field checkboxes_mandatory that is used to display or hide the checkboxes as well as make the checkboxes mandatory or not.

function onChange(control, oldValue, newValue, isLoading) {
    if (newValue == '') {
        return;
    }
    var mandatoryVars = 'eldritch_invocations_agonizing_blast,eldritch_invocations_armor_shadows,eldritch_invocations_ascendant_step,eldritch_invocations_beast_speech';

// In this example, I only have 4 checkboxes so commenting out the rest    //eldritch_invocations_beguiling_influence,eldritch_invocations_bewitching_whispers,eldritch_invocations_book_ancient_secrets,eldritch_invocations_chains_carceri,eldritch_invocations_devils_sight,eldritch_invocations_dreadful_word,eldritch_invocations_eldritch_sight,eldritch_invocations_eldritch_spear,eldritch_invocations_eyes_rune_keeper,eldritch_invocations_fiendish_vigor,eldritch_invocations_gaze_two_minds,eldritch_invocations_lifedrinker,eldritch_invocations_mask_many_faces,eldritch_invocations_master_myriad_forms,eldritch_invocations_minions_chaos,eldritch_invocations_mire_mind,eldritch_invocations_misty_visions,eldritch_invocations_one_shadows,eldritch_invocations_otherworldly_leap,eldritch_invocations_repelling_blast,eldritch_invocations_sculptor_flesh,eldritch_invocations_sign_ill_omen,eldritch_invocations_thief_five_fates,eldritch_invocations_thirsting_blade,eldritch_invocations_visions_distant_realms,eldritch_invocations_voice_chain_master,eldritch_invocations_whispers_grave,eldritch_invocations_witch_sight';
	mandatoryVars = mandatoryVars.split(',');
    var setMandatory = (newValue == 'Yes');
    //alert(setMandatory);
    try {
        g_form.setMandatory(mandatoryVars[0], setMandatory); // setting checkboxes to be mandatory or not
        for (var i = 0; i < mandatoryVars.length; i++) {
            g_form.setDisplay(mandatoryVars[i], setMandatory);  // hide/display checkboxes
        }
    } catch (e) {
        alert(e.message);
    }
}

3. onSubmit script to make 2 selections mandatory.

function onSubmit() {
    var mandatoryVars = 'eldritch_invocations_agonizing_blast,eldritch_invocations_armor_shadows,eldritch_invocations_ascendant_step,eldritch_invocations_beast_speech';
    //eldritch_invocations_beguiling_influence,eldritch_invocations_bewitching_whispers,eldritch_invocations_book_ancient_secrets,eldritch_invocations_chains_carceri,eldritch_invocations_devils_sight,eldritch_invocations_dreadful_word,eldritch_invocations_eldritch_sight,eldritch_invocations_eldritch_spear,eldritch_invocations_eyes_rune_keeper,eldritch_invocations_fiendish_vigor,eldritch_invocations_gaze_two_minds,eldritch_invocations_lifedrinker,eldritch_invocations_mask_many_faces,eldritch_invocations_master_myriad_forms,eldritch_invocations_minions_chaos,eldritch_invocations_mire_mind,eldritch_invocations_misty_visions,eldritch_invocations_one_shadows,eldritch_invocations_otherworldly_leap,eldritch_invocations_repelling_blast,eldritch_invocations_sculptor_flesh,eldritch_invocations_sign_ill_omen,eldritch_invocations_thief_five_fates,eldritch_invocations_thirsting_blade,eldritch_invocations_visions_distant_realms,eldritch_invocations_voice_chain_master,eldritch_invocations_whispers_grave,eldritch_invocations_witch_sight';
    mandatoryVars = mandatoryVars.split(',');  // moved this here to stop splitting for each checkbox
    g_form.clearMessages();
    if (g_form.isMandatory(mandatoryVars[0])) {  // check if the checkboxes selection is mandatory or not (note that when checkboxes are not displayed, it's not mandatory)
        var mandatoryCount = 2;
        var maxCount = 2;

        var passed = forceMandatoryCheckboxes(mandatoryVars, mandatoryCount);
        if (!passed) {
            //Abort the submit
            g_form.addErrorMessage('Please select ' + '2 Eldritch Invocations.');
            return false;
        }

        function forceMandatoryCheckboxes(mandatory, count) {
            var cnt = 0;
            for (var i = 0; i < mandatory.length; i++) {
                if (g_form.getValue(mandatory[i]) == 'true') {
                    cnt++;
                    if (cnt > count) {
                        return false;
                    }
                }
            }
            return (cnt == count);
        }
    }
}

Execution:

Case 1:Checkboxes are not mandatory. Checkboxes are hidden

find_real_file.png

Case 2: Only 1 checkbox selected and submitted

find_real_file.png