How use a client script to check which fields were modified?

EJ13
Tera Contributor

I am writing an onSubmit client script on the Incident table. The goal that it will perform a certain action if the form was modified, except in the case that only "assigned to" and/or "assignment group" are modified. I am currently using g_form.modified to check if anything was modified at all, but I am not sure how to verify that only those two fields are changing. Is there a way to determine which fields are changing without checking each one individually?

 

Thank you!

6 REPLIES 6

@EJ13 

Hope you are doing good.

Did my reply answer your question?

💡 If my response helped, please mark it as correct and close the thread 🔒— this helps future readers find the solution faster! 🙏

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

Deepak Shaerma
Mega Sage

hi @EJ13 

g_form.modified property is a binary switch—it only tells you that something changed, not what,  to solve this without hardcoding every field, you need a "Snapshot Strategy."

Since g_form doesn't store previous values for you to access in an onSubmit script, you need two scripts
onLoad and onSubmit

function onLoad() {

    g_form.initialValues = {};

    var fields = g_form.getFieldNames();

    for (var i = 0; i < fields.length; i++) {
        var fieldName = fields[i];
        g_form.initialValues[fieldName] = g_form.getValue(fieldName);
    }
}


onsubmit

function onSubmit() {

    if (!g_form.modified) {
        return;
    }
    var ignoreFields = ['assigned_to', 'assignment_group'];
    
    var meaningfulChange = false;

    var fields = g_form.getFieldNames();

    for (var i = 0; i < fields.length; i++) {
        var fieldName = fields[i];
        if (ignoreFields.indexOf(fieldName) > -1) {
            continue;
        }

        var currentValue = g_form.getValue(fieldName);
        var originalValue = g_form.initialValues[fieldName];
        if (currentValue != originalValue) {
            meaningfulChange = true;
            break;
        }
    }

    // 5. Execute your Action
    if (meaningfulChange) {
        // YOUR CUSTOM CODE HERE
        // alert("Significant fields were modified. Executing logic...");
    }
}

Happy to help! ‌‌
To help others in the community find this solution, kindly mark this response as the Correct Answer ‌‌ and Helpful‌‌.
Warm Regards,
Deepak Sharma
Community Rising Star 2025