Make check box variables Mandatory depending on select box choices

satya30
Tera Contributor

Hi All,

Thank you for taking your time and helping me out.

Please help me out, I am working for long time on below requirement.

I have a requirement where 

 

We have 6 check box variables(next to each other like options variable):

If choice 1 is selected then all 6 checkboxes should be available for checking but out of particular 3 checkbox variables one should definitely be selected

If choice 2 is selected then all 6 checkboxes should be available out of which 3 should definitely be selected

If choice 3 is selected then 3 check box variables should definitely be selected and out of other 3 check boxes any of one should be selected

ie. For example I have a select box variable SBV with choices A, B, C and the Checkboxes D1, D2, D3, D4, D5, D6

 

If A is selected we need 1 of (D1, D2, and D3) to be required

If B is selected, we need ALL the (D4, D5, D6) and add a note that says “If B is selected all three choices should definitely be selected”

If Cis selected we need  all the (D4, D5, D6) to be required  and one of (D1, D2, and D3) is required.

 

 

Below is the code that I tried working on:

 

Onchange of variable SBV

 

function onChange(control, oldValue, newValue, isLoading) {
    if (isLoading || newValue == '') {
       
    g_form.clearMessages();
    g_form.setMandatory('D1’, false);
    g_form.setMandatory(D2, false);
    g_form.setMandatory(D3, false);
    g_form.setMandatory(D4, false);
    g_form.setMandatory(D5, false);
    g_form.setMandatory(D6, false);

    }
     
    if (newValue != oldValue){
    g_form.clearMessages();
    g_form.setMandatory('D1’, false);
    g_form.setMandatory(D2, false);
    g_form.setMandatory(D3, false);
    g_form.setMandatory(D4, false);
    g_form.setMandatory(D5, false);
    g_form.setMandatory(D6, false);


g_form.setVisible('D1’, false);
g_form.setVisible(D2, false);
g_form.setVisible(D3, false);
g_form.setVisible(D4, false);
g_form.setVisible(D5, false);
g_form.setVisible(D6, false);
   
    // Check the selection in the "SBV" field
    switch (newValue) {
        case 'A':
            // Make one of the mobility amenities required
            g_form.clearMessages();
            g_form.setVisible('D1’, true);
            g_form.setVisible(D2, true);
            g_form.setVisible(D3,, true);
            g_form.setVisible(D4,, false);
            g_form.setVisible(D5, false);
            g_form.setVisible(D6, false);

            g_form.setMandatory('D1’, true);
            g_form.setMandatory(D2, true);
            g_form.setMandatory(D3,, true);

           
            // Show hearing amenities if any mobility amenity is selected
            if (g_form.getValue('D1’) == 'true' ||
                g_form.getValue(D2) == 'true' ||
                g_form.getValue(D3) == 'true') {
                   
                g_form.setVisible('D4’, true);
                g_form.setVisible(D5, true);
                g_form.setVisible(D6, true);
            }
            break;

        case 'B':
            g_form.clearMessages();
            g_form.setVisible('D1’, true);
            g_form.setVisible(D2, true);
            g_form.setVisible(D3, true);
            g_form.setVisible(D4, true);
            g_form.setVisible(D5, true);
            g_form.setVisible(D6, true);

            // Make all hearing amenities required
            g_form.setMandatory(D4, true);
            g_form.setMandatory(D5, true);
            g_form.setMandatory(D6, true);

            // Set the hearing amenities to true (check them)
            g_form.setValue(D4, true);  
            g_form.setValue(D5, true);  
            g_form.setValue(D6, true);  

            break;

        case 'C':
            g_form.clearMessages();
            g_form.setVisible('D1’, true);
            g_form.setVisible(D2, true);
            g_form.setVisible(D3, true);
            g_form.setVisible(D4, true);
            g_form.setVisible(D5, true);
            g_form.setVisible(D6, true);

            // Make all hearing amenities required
            g_form.setMandatory(D4, true);
            g_form.setMandatory(D5, true);
            g_form.setMandatory(D6, true);

            // Set the hearing amenities to true (check them)
            g_form.setValue(D4, true);  
            g_form.setValue(D5, true);  
            g_form.setValue(D6, true);  

            // Make one mobility amenity required
            g_form.setMandatory('D1’, true);
            g_form.setMandatory(D2, true);
            g_form.setMandatory(D3, true);

            break;

        default:
            break;
    }
}
}

Please help me. Thanks in advance.

9 REPLIES 9

Ankur Bawiskar
Tera Patron
Tera Patron

@satya30 

try this

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

    // Clear previous messages and reset mandatory fields
    g_form.clearMessages();
    g_form.setMandatory('D1', false);
    g_form.setMandatory('D2', false);
    g_form.setMandatory('D3', false);
    g_form.setMandatory('D4', false);
    g_form.setMandatory('D5', false);
    g_form.setMandatory('D6', false);

    g_form.setVisible('D1', true);
    g_form.setVisible('D2', true);
    g_form.setVisible('D3', true);
    g_form.setVisible('D4', true);
    g_form.setVisible('D5', true);
    g_form.setVisible('D6', true);

    switch (newValue) {
        case 'A':
            // Make one of D1, D2, D3 required
            g_form.setMandatory('D1', true);
            g_form.setMandatory('D2', true);
            g_form.setMandatory('D3', true);
            g_form.addInfoMessage('Please select at least one of D1, D2, or D3.');
            break;

        case 'B':
            // Make D4, D5, D6 required
            g_form.setMandatory('D4', true);
            g_form.setMandatory('D5', true);
            g_form.setMandatory('D6', true);
            g_form.addInfoMessage('If B is selected, all three choices (D4, D5, D6) should definitely be selected.');
            break;

        case 'C':
            // Make D4, D5, D6 required and one of D1, D2, D3 required
            g_form.setMandatory('D4', true);
            g_form.setMandatory('D5', true);
            g_form.setMandatory('D6', true);
            g_form.setMandatory('D1', true);
            g_form.setMandatory('D2', true);
            g_form.setMandatory('D3', true);
            g_form.addInfoMessage('If C is selected, all three choices (D4, D5, D6) should be selected and one of D1, D2, or D3 should be selected.');
            break;

        default:
            break;
    }
}

If my response helped please mark it correct and close the thread so that it benefits future readers.

Regards,
Ankur
Certified Technical Architect  ||  9x ServiceNow MVP  ||  ServiceNow Community Leader

No, here functionality is missing. will you be able to please check the other response provided by Rajesh and the issues that I am facing and help me out.

Thank you

@satya30 

Please share what script you are using now and what's the challenge

you can always enhance the code.

If my response helped please mark it correct and close the thread so that it benefits future readers.

Regards,
Ankur
Certified Technical Architect  ||  9x ServiceNow MVP  ||  ServiceNow Community Leader

function onChange(control, oldValue, newValue, isLoading) {
    // If the form is loading or the new value is empty, exit
    if (isLoading) {
        return;
    }

    // Show an info message indicating that the value is being processed
   /* g_form.addInfoMessage('Processing the selected value... Please wait.');

    // Get the selected value in the lookup field (this will be the Sys ID)
    var selectedOption = g_form.getValue('look_up');

    // Log the selected option to see what value is returned from the lookup field
    g_form.addInfoMessage('Selected Option: ' + selectedOption);

    // If a valid option is selected, fetch the corresponding record and get u_code
    if (selectedOption) {
        // Log to confirm we're entering the block of code
        g_form.addInfoMessage('Processing selected option...');

        // Query the reference record using GlideRecord
        var gr = new GlideRecord('table'); // Your reference table
        if (gr.get(selectedOption)) { 
            // Log the u_code value for debugging
            g_form.addInfoMessage('Retrieved u_code: ' + gr.u_code);

            // Set the value of uCode field
            g_form.setValue('uCode', gr.u_code);

            // Confirm that the field was updated
            g_form.addInfoMessage('uCode has been set to: ' + gr.u_code);
        } else {
            // If no matching record was found, log this as well
            g_form.addInfoMessage('No matching record found for Sys ID: ' + selectedOption);
        }
    } else {
        // If no valid selection was made, log this as well
        g_form.addInfoMessage('No option selected.');
    }
}
*/

       
        var name = g_form.getValue('look_up');
        var Val = name.split('|')[0];
        g_form.setValue('uCode', Val);
            g_form.addInfoMessage('No option selected.'+Val);
        }