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

Sumit Pandey3
Tera Contributor

If you change impact from 1 to 3 and then 3 to 1 without submitting the form that means No changes have been made according to your clientScript. As the newValue will become OldValue it means that the specific Onchange condition is not meeting. The Onchange Client script will execute when oldValue is not equal to newValue.

Hi @Sumit Pandey3 - how to fix the above code ?

Hi @Pooja Khatri , The Above code is fine. Try to understand the OnChange client script as a concept. I can share with you a solution if you'd like more about the requirements and what you're trying to achieve.

PrashantLearnIT
Giga Sage

Hi @Pooja Khatri ,

 

Please try to understand what @Sumit Pandey3 has mentioned, your script is fine please test it correctly, oldvalue should not be same as newvalue to make work notes mandatory.

 

 

********************************************************************************************************
Please appreciate the efforts of community contributors by marking the appropriate response as the correct answer and helpful. This may help other community users to follow the correct solution in the future.

********************************************************************************************************
Cheers,
Prashant Kumar
ServiceNow Technical Architect


Community Profile LinkedIn YouTube Medium TopMate
********************************************************************************************************