Updating of custom work notes for the entities

VinuvarshitSR
Giga Expert

Table name is u_cmdb_ci_device
i have 2 reference field and a reference list
u_location_id 
u_subnetwork
Usecase is whenever the field get updated it should tell old value and new value 
i want this usecase to make it as Dynamic so i created script include below is what i did now

var FieldChangeHandler = Class.create();
FieldChangeHandler.prototype = {
    initialize: function() {},

    processFieldChange: function(current, previous, fieldName, workNotesField) {
        // Ensure the field exists on the record and has changed
        if (current[fieldName] !== undefined && previous[fieldName] !== undefined && current.getValue(fieldName) !== previous.getValue(fieldName)) {
            // Get display values for old and new field values
            var oldDisplayValue = previous.getDisplayValue(fieldName) || "None";
            var newDisplayValue = current.getDisplayValue(fieldName) || "None";

            // Construct the update note
            var updateNote = fieldName + " updated from '" + oldDisplayValue + "' to '" + newDisplayValue + "'.";

            // Append the update note to the specified work notes field
            var existingNotes = current.getValue(workNotesField) || "";
            current.setValue(workNotesField, existingNotes + "\n" + updateNote);
        }
    },

    type: 'FieldChangeHandler'
};


originally i used BR

 if (current.u_location_id.changes()) {
        // Get the display values for the old and new u_location_id
        var oldLocationDisplay = previous.u_location_id.getDisplayValue();
        var newLocationDisplay = current.u_location_id.getDisplayValue();

        // Append a note to the work_notes field
        var updateNote = "Location updated from '" + oldLocationDisplay + "' to '" + newLocationDisplay + "'.";
        current.u_work_notes = (current.u_work_notes ? current.u_work_notes + "\n" : "") + updateNote;
    }


BR is working fine but my script include is failing somewhere need help Thank you
I am calling the function through BR

(function executeRule(current, previous /*null when async*/) {
    var handler = new FieldChangeHandler();

    // Handle field changes dynamically
    handler.processFieldChange(current, previous, "u_location_id", "u_work_notes");
    handler.processFieldChange(current, previous, "u_subnetwork", "u_work_notes");
})(current, previous);




1 ACCEPTED SOLUTION

Vishal Jaswal
Giga Sage

Hello @VinuvarshitSR 

Script Include:

var FieldChangeHandler = Class.create();
FieldChangeHandler.prototype = {
    initialize: function() {},
    processFieldChange: function(current, previous, fieldsToMonitor, workNotesField) {
        try {
            gs.log("VJ4: Script Include executed");
            var updateNotes = [];
            fieldsToMonitor.forEach(function(fieldName) {
                var oldSysId = previous ? previous.getValue(fieldName) : null;
                var newSysId = current.getValue(fieldName);
                var oldDisplayValue = previous ? previous.getDisplayValue(fieldName) || "None" : "None";
                var newDisplayValue = current.getDisplayValue(fieldName) || "None";
                if (oldSysId !== newSysId) {
                    var updateNote = fieldName + " updated from '" + oldDisplayValue + "' to '" + newDisplayValue + "'.";
                    gs.log("VJ4: Script Include: Adding to work notes - " + updateNote);
                    updateNotes.push(updateNote);
                }
            });
            if (updateNotes.length > 0) {
                current.setValue(workNotesField, existingNotes + "\n" + updateNotes.join("\n")); // Append new notes
                gs.log("VJ4: Script Include: Work notes updated successfully.");
            } else {
                gs.log("VJ4: Script Include: No changes detected.");
            }
        } catch (ex) {
            gs.log("VJ4: Script Include: Error processing field change - " + ex.message);
        }
    },
    type: "FieldChangeHandler",
};


Before Update Business Rule:

(function executeRule(current, previous) {
   var fieldsToMonitor = ['u_location_id', 'u_subnetwork']; // Reference fields
   var workNotesField = 'u_work_notes';
   var fieldHandler = new FieldChangeHandler();
   fieldHandler.processFieldChange(current, previous, fieldsToMonitor, workNotesField);
})(current, previous);


Results (syslog.LIST):

VishalJaswal_0-1743010654215.png

 




Hope that helps!

View solution in original post

1 REPLY 1

Vishal Jaswal
Giga Sage

Hello @VinuvarshitSR 

Script Include:

var FieldChangeHandler = Class.create();
FieldChangeHandler.prototype = {
    initialize: function() {},
    processFieldChange: function(current, previous, fieldsToMonitor, workNotesField) {
        try {
            gs.log("VJ4: Script Include executed");
            var updateNotes = [];
            fieldsToMonitor.forEach(function(fieldName) {
                var oldSysId = previous ? previous.getValue(fieldName) : null;
                var newSysId = current.getValue(fieldName);
                var oldDisplayValue = previous ? previous.getDisplayValue(fieldName) || "None" : "None";
                var newDisplayValue = current.getDisplayValue(fieldName) || "None";
                if (oldSysId !== newSysId) {
                    var updateNote = fieldName + " updated from '" + oldDisplayValue + "' to '" + newDisplayValue + "'.";
                    gs.log("VJ4: Script Include: Adding to work notes - " + updateNote);
                    updateNotes.push(updateNote);
                }
            });
            if (updateNotes.length > 0) {
                current.setValue(workNotesField, existingNotes + "\n" + updateNotes.join("\n")); // Append new notes
                gs.log("VJ4: Script Include: Work notes updated successfully.");
            } else {
                gs.log("VJ4: Script Include: No changes detected.");
            }
        } catch (ex) {
            gs.log("VJ4: Script Include: Error processing field change - " + ex.message);
        }
    },
    type: "FieldChangeHandler",
};


Before Update Business Rule:

(function executeRule(current, previous) {
   var fieldsToMonitor = ['u_location_id', 'u_subnetwork']; // Reference fields
   var workNotesField = 'u_work_notes';
   var fieldHandler = new FieldChangeHandler();
   fieldHandler.processFieldChange(current, previous, fieldsToMonitor, workNotesField);
})(current, previous);


Results (syslog.LIST):

VishalJaswal_0-1743010654215.png

 




Hope that helps!