Dependent mandatory field on catalog form

_bhishek
Tera Guru

Hi ,

I have one catalog item.In first field ,I am selecting application .I have 2 other fields purpose and RTO . if purpose is selected as 'business application' in catalog form and 'RTO' value present in service now application table for the selected server is 1 hour then RTO field on catalog form should be mandatory else it should not be .I have written below on change catalog client script and not sure what is wrong with this.could you please help here.

On change field :purpose

function onChange(control, oldValue, newValue, isLoading) {
    if (isLoading || newValue == '') {
        return;
    }
    g_form.getReference('business_application_name', function(gr) { //business_application_name is reference field available for Applications.
        if (gr) {
            if (newValue == 'Business Application' && gr.u_rto == '1 Hour') { //u_rto column name of the field available on the service now application table
                g_form.setMandatory('rto', true); //rto is name of the firld available on catalog form
            } else {

                g_form.setMandatory('rto', false);
            }
            //Type appropriate comment here, and begin script below
        }
    });
}

 

1 ACCEPTED SOLUTION

Ankur Bawiskar
Tera Patron
Tera Patron

@_bhishek 

try this

function onChange(control, oldValue, newValue, isLoading) {
    if (isLoading || newValue == '') {
        return;
    }
    // Debug: Check newValue
    console.log('Purpose newValue:', newValue);

    g_form.getReference('business_application_name', function(gr) {
        if (gr) {
            // Debug: Check RTO value
            console.log('Application RTO:', gr.u_rto);

            // Adjust the value comparison as per your actual stored values
            if (newValue == 'business_application' && gr.u_rto == '1 Hour') {
                g_form.setMandatory('rto', true);
            } else {
                g_form.setMandatory('rto', false);
            }
        }
    });
}

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

View solution in original post

2 REPLIES 2

Ankur Bawiskar
Tera Patron
Tera Patron

@_bhishek 

try this

function onChange(control, oldValue, newValue, isLoading) {
    if (isLoading || newValue == '') {
        return;
    }
    // Debug: Check newValue
    console.log('Purpose newValue:', newValue);

    g_form.getReference('business_application_name', function(gr) {
        if (gr) {
            // Debug: Check RTO value
            console.log('Application RTO:', gr.u_rto);

            // Adjust the value comparison as per your actual stored values
            if (newValue == 'business_application' && gr.u_rto == '1 Hour') {
                g_form.setMandatory('rto', true);
            } else {
                g_form.setMandatory('rto', false);
            }
        }
    });
}

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

@_bhishek 

Hope you are doing good.

Did my reply answer your question?

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