Join the #BuildWithBuildAgent Challenge! Get recognized, earn exclusive swag, and inspire the ServiceNow Community with what you can build using Build Agent.  Join the Challenge.

OnChange for multiple fields

Akshay Bhaskar
Kilo Sage

Hi Everyone, 

The problem record in my instance have the fields - Assignment Group, Priority and Review Date. My requirement is to make the comments mandatory when one of the above value changes. I've written three onChange client scripts for and the script in general is - 

function onChange(control, oldValue, newValue, isLoading, isTemplate) {
    if (isLoading || newValue === '') {
        return;
    }
    //Type appropriate comment here, and begin script below
    if ((newValue != oldValue) && (oldValue != '')) {
        g_form.setMandatory('comments', true);
        g_form.addInfoMessage("Please justify within comments, the update made to the priority.");
    } else{
        g_form.setMandatory('comments', false);
      }
}


The use case scenario that has failed is - 
1. Assume assignment group's original value = GroupABC. User has updated it to GroupXYZ (Result - mandatory comments)
2. Assume priority is 4-Low and is updated to 3-Medium (Result - mandatory comments)
3. If the user updates the assignment group back to GroupABC, the comments become non-mandatory and allows the form to be saved without any justification for the change in priority. 

Are there any workarounds to address this issue? 


Regards,
Akshay

1 ACCEPTED SOLUTION

@Akshay Bhaskar 

you can know which field got changed based on that group.Changed flag and based on that show respective messages using multiple if else statements.

I hope you will be able to incorporate those changes.

Regards
Ankur

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

View solution in original post

6 REPLIES 6

Hi Ankur, 

You're correct. I made use of multiple if-else statements to fulfil my requirements. Thank you so much for your guidance.

For the benefit of future readers, I'm also adding the script that I've used - 

function onSubmit() {
    //Type appropriate comment here, and begin script below
    var arr = [];
    var group = g_form.getControl('assignment_group');
    var priority = g_form.getControl('priority');
    var date = g_form.getControl('due_date');

    if (group.changed || priority.changed || date.changed) {
        if (date.changed) {
            arr.push(g_form.getLabelOf('due_date'));
        }
        if (group.changed) {
            arr.push(g_form.getLabelOf('assignment_group'));
        }
        if (priority.changed) {
            arr.push(g_form.getLabelOf('priority'));
        }
        var string = arr.join(", ");
        g_form.addInfoMessage(string + " has been updated. Please provide a justification within comments");
        if (g_form.getValue('comments') == '') {
            g_form.setMandatory('comments', true);
            return false;

        }
    }
}



Regards,
Akshay

John Dahl
Kilo Sage

The issue is that the "previous" value doesn't get updated when the value is changed, it is loaded when the page loads and remains until the form is submitted... The onChange is fired when you set the value back to the original value, so it will match the "previous" value and set the mandatory flag to false.

Rather than having each onChange script try to directly take action (i.e. making notes mandatory), have a UI Script with the logic that checks the three fields and sets the mandatory flag when any of the three fields is changed... then you only need to call the function from the onChange scripts.