How to fix the OnChange Client Script

Pooja Khatri
Tera Contributor

Hi All ,

 

I have written the below onchange client script for impact field , 

the requirement is .. whenever an Incident form is opened there is some default value for the impact field on the Incident form being setup .. so if the customer changes the default value of the Impact field before clicking on the SAVE button on the Incident during that time the worknotes should not be mandatory which is working perfectly fine with the below code .

 

function onChange(control, oldValue, newValue, isLoading, isTemplate) {
    if (isLoading || newValue === '' || newValue == oldValue) {
        if (newValue == oldValue) {
            g_form.setMandatory('work_notes', false);
        }
        return;
    }
    if (!g_form.isNewRecord()) {
        g_form.setMandatory('work_notes', true);
    }
}
 
But now after saving the form the impact field is changed to some another value work notes should be mandatory which is also happening with my above script .. 
 
but if the customer plays or changes the impact value like for example if the default value for impact = 1 they change from 1 to 4 work notes becomes mandatory then if from 4 to 3 during that time too work notes is mandatory but if they change from 3 to 1 before clicking on save the field is not mandatory .. ideally the field should be mandatory if its changed from 3 to 1 .
 
The same issue is happening with Urgency field and the same script is used in my Urgency field too . 
 
Where I am wrong with my OnChange Client script that it is causing an issue ?
5 REPLIES 5

Sathish Kumar S
Tera Expert

Hi @Pooja Khatri 

 

Pls try below script and confirm if this fulfils your requirement.

 

function onChange(control, oldValue, newValue, isLoading, isTemplate) {

    if (isLoading || newValue === '') {

        return;

    }

 

    // If the value has changed and the record is not new

    if (newValue != oldValue && !g_form.isNewRecord()) {

        g_form.setMandatory('work_notes', true);

    }

}