How to create auto save functionality in the incident form

amolpawar
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

11 REPLIES 11

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

danzietlow
Tera Contributor

Everyone agrees that auto-saving unfinished forms to ServiceNow would create incomplete records and add unnecessary system load. However, ServiceNow is missing the point of this request - User's are losing their work because of a host of reasons their ServiceNow connection is lost or times out. And, this could be easily remedied by ServiceNow instead of always trying to explain the problem away.

ServiceNow already caches forms and data on a user's machine within the browser's cache to improve performance and reduce the time it takes to load the forms. The solution to this problem is to auto-save the form instance and it's data the user is working with in the user's browser's cache until the form is successfully submitted to ServiceNow, and then it is auto-deleted. This way, if a user loses their ServiceNow connection or times out, when the user reconnects to ServiceNow they still have all their work in progress saved in the form instance to continue where they left off. This is how my software development group solved this problem for our SaaS applications and users.

Thank you,
Dan