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

Adrian Ubeda
Mega Sage

Hello @EJ13

 

I have done something similar, but I had to use g_scratchpad in the onSubmit client script. I had control all changes in a onChange script separately and then unified values in the onSubmit client script

Here's some link which was useful for me: https://www.servicenow.com/community/developer-forum/g-scratchpad/m-p/2225478

If it was helpful, please give positive feedback! ✔
☆ Community Rising Star 22, 23 & 24 ☆

yashkamde
Tera Guru

Hello @EJ13 ,

There is one OOTB Table 'sys_audit' which track changes to each single change in any servicenow table,

So when I change the assignment group from Network -> Hardware & assigned to from empty -> Bow ruggeri
Screenshot 2026-01-15 223433.png


It's got captured in this table as record in form of new value and old value
Screenshot 2026-01-15 223419.png

 

So in this way you can keep the track of the changes you had made..

If my response help, mark as helpful and accept the solution..

adityahubli
Tera Guru

Hello @EJ13 ,

 

I tried your use case , i created script include and on submit client script for it . I simple show addInfoMessage onsubmiting form but it is showing pnly for while . You can refer this code and modify that as per your requirnment :

 

Script include : Glideajax callable 

 

var showModifiedFields = Class.create();
showModifiedFields.prototype = Object.extendsObject(AbstractAjaxProcessor, {
    type: 'showModifiedFields',

    getModifiedFields: function() {

        var fields = [];
        var id = this.getParameter('sysparm_id');

        var twoMinutesAgo = new GlideDateTime();
        twoMinutesAgo.addMinutesUTC(-2);

        var gr = new GlideRecord('sys_audit');
        gr.addQuery('documentkey', id);
        // gr.addQuery('sys_created_on', '>=', twoMinutesAgo);
        gr.query();

        while (gr.next()) {
            if (
                gr.fieldname != 'assigned_to' &&
                gr.fieldname != 'assignment_group' &&
                fields.indexOf(gr.fieldname.toString()) === -1
            ) {
                fields.push(gr.fieldname.toString());
            }
        }
        gs.error('fileds mod: ' + fields);
        return JSON.stringify(fields);
    }
});

 

 Onsubmit client script : 

 

var isAjaxComplete = false;

function onSubmit() {

    // Allow submit after Ajax is done
    if (isAjaxComplete) {
        return true;
    }

    var ga = new GlideAjax('showModifiedFields');
    ga.addParam('sysparm_name', 'getModifiedFields');
    ga.addParam('sysparm_id', g_form.getUniqueValue());

    ga.getXML(function(response) {

        var res = response.responseXML.documentElement.getAttribute('answer');

        // SAFETY CHECK
        if (res) {
            var fieldsArr = JSON.parse(res);

            if (fieldsArr.length > 0) {
                g_form.addInfoMessage('Modified Fields are : ' + fieldsArr.join(', '));
            }
        }

        isAjaxComplete = true;
        g_form.submit();
    });

    return false; // stop default submit
}

 

adityahubli_0-1768539903854.png

 

adityahubli_1-1768539939771.png

 

If this helps you then mark it as helpful and accept as solution.

Regards,

Aditya,

Technical Consultant

 

 

Ankur Bawiskar
Tera Patron

@EJ13 

try this and it should work

function onSubmit() {

    var assignedToChanged = g_form.getControl('assigned_to').changed;
    var assignmentGroupChanged = g_form.getControl('assignment_group').changed;
    
    // If form modified AND it's NOT just assignment fields
    if(g_form.modified && !(assignedToChanged && !assignmentGroupChanged) && 
       !(assignmentGroupChanged && !assignedToChanged) && 
       !(assignedToChanged && assignmentGroupChanged)) {
        
        // Your custom action here
        g_form.addErrorMessage('Other fields modified - custom validation required');
        return false;
    }
}

💡 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