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.

How to create auto save functionality in the incident form

Amol Pawar
Tera Guru

Hi Experts,

 

I need to create an Auto-save functionality in the incident form. I read many posts regarding this, but I didn't understand. It would be great if anyone could navigate me to the correct post or give me any insights on this!

 

When someone opens a form and updates any details, it should auto-save the details ignoring the mandatory fields if they are yet to be filled.

 

Thanks in advance,

Amol

10 REPLIES 10

Hi @Runjay Patel,

Thank you for your response. I tried your answer but still not working for me. Let me know if there are any changes needed:

 

Script include:

var autoSaveUtilsSecond = Class.create();
autoSaveUtilsSecond.prototype = Object.extendsObject(global.AbstractAjaxProcessor, {

    autoSave: function() {
        var sysId = this.getParameter('sysparm_sys_id');
        var fields = JSON.parse(this.getParameter('sysparm_fields'));

        if (!sysId || !fields) {
            return JSON.stringify({
                status: 'error',
                error: 'Invalid parameters'
            });
        }

        try {
            var gr = new GlideRecord('x_infte_incident_incident');
            if (gr.get(sysId)) {

                for (var key in fields) {
                    gs.log('Amol' + key[key], 'Pawar');
                    gr.setValue(key, fields[key]);

                }

                gr.autoSysFields(false); // Prevent system fields like updated_by from being updated
                gr.update();
                return JSON.stringify({
                    status: 'success'
                });
            } else {
                return JSON.stringify({
                    status: 'error',
                    error: 'Incident not found'
                });
            }
        } catch (error) {
            return JSON.stringify({
                status: 'error',
                error: error.message
            });
        }
    },
    type: 'autoSaveUtilsSecond'
});
 
Client Script: 
function onLoad() {
    //Type appropriate comment here, and begin script below

    function onLoad() {
        //Type appropriate comment here, and begin script below
        var autoSaveInterval = 10000; // Auto-save interval in milliseconds (10 seconds)
        var autoSaveTimer;
        var tempArr = ['incident_description'];


        function startAutoSave() {
            // Clear any existing timer
            if (autoSaveTimer) clearInterval(autoSaveTimer);

            // Set a new timer
            autoSaveTimer = setInterval(function() {
                autoSaveForm();
            }, autoSaveInterval);
        }

        function autoSaveForm() {

            var arr = {};

            tempArr.forEach(function(field) {
                var fieldValue = g_form.getValue(field); // Get the value of the field
                arr[field] = fieldValue;
            });


            console.log(arr);
            var ga = new GlideAjax('autoSaveUtilsSecond');
            ga.addParam('sysparm_name', 'autoSave');
            ga.addParam('sysparm_sys_id', g_form.getUniqueValue());
            ga.addParam('sysparm_fields', JSON.stringify(arr)); // Capture changed fields
            ga.getXMLAnswer(function(response) {
                var result = JSON.parse(response);
                if (result.status === 'success') {
                    console.log('Auto-save successful');
                } else {
                    console.error('Auto-save failed: ' + result.error);
                }
            });
        }

        // Start auto-save on form load
        startAutoSave();
    }
}
 
I tried to create the script include in both global and custom scope.
Thanks in advance,
Amol