Onchange client script

GBS
Tera Contributor

I have written the below onchange client script to make Subcategory mandatory when the Category is Software but it is not working.

 

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

    var category = g_form.getValue('category');

    if (category === 'software') {
        g_form.setMandatory('subcategory', true);
    } else {
        g_form.setMandatory('subcategory', false);
    }

}
8 REPLIES 8

Aniket Chavan
Tera Sage
Tera Sage

Hello @GBS 

Can you please verify once your category choice value i.e. is it "software" or "Software".
and then use the same script which you are using and I also have tested it from my end on incident table and its working perfectly fine.

 

 

function onChange(control, oldValue, newValue, isLoading, isTemplate) {
    // Check if the form is still loading or if the field is empty
    if (isLoading || newValue === '') {
        return;
    }

    // Get the value of the 'category' field
    var category = g_form.getValue('category');
    g_form.addInfoMessage("Category - " + category);

    // Check if the category is 'Software'
    if (category == 'Software') {
        g_form.addInfoMessage("Inside if ");
        g_form.setMandatory('subcategory', true);
    } else {
        g_form.addInfoMessage("Inside else");
        g_form.setMandatory('subcategory', false);
    }
}

 

 

 

Let me know your views on this and Mark Correct if this solves your query and also mark 👍Helpful if you find my response worthy based on the impact.


Regards,
Aniket

@Aniket Chavan choice value is 'software'

Hello @GBS 

Then please check if there is any existing logic present or not? like UI policy or existing client script, you can navigate to configure<all and verify there once.

AniketChavan_0-1702999471393.png


Please let me know your views on this.

Sainath N
Mega Sage
Mega Sage

@GBS : Assuming that you have created the onChange client script in the category field, you can directly use newValue to check the value. Please also check the backend name of subcategory is correct or not.

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

    // var category = g_form.getValue('category');

    if (newValue == 'software') {
        g_form.setMandatory('subcategory', true);
    } else {
        g_form.setMandatory('subcategory', false);
    }
}

 

If this still doesn't work, check if there are existing logics in client scripts with a higher order than the current one and also UI policies, as they get the highest precedence. 

 

Please mark this as correct answer and helpful if it resolved, or mark this helpful if this help you to reach towards solution.
 
Regards,
Sainath N