diff --git a/Client-Side Components/Catalog Client Script/catalog Draft/Readm.md b/Client-Side Components/Catalog Client Script/catalog Draft/Readm.md new file mode 100644 index 0000000000..a4ef78f42e --- /dev/null +++ b/Client-Side Components/Catalog Client Script/catalog Draft/Readm.md @@ -0,0 +1,9 @@ +This project implements an Auto Save Draft feature for ServiceNow Catalog Items. It automatically saves the user’s progress (form variables) every few minutes to prevent data loss if the session times out or the browser closes. it Prevents data loss during long form filling. + + + +features +Auto-save catalog form data every 2 minutes. + Stores draft data in a custom table. +Restores saved data when the user reopens the catalog item. + Works in Service Portal diff --git a/Client-Side Components/Catalog Client Script/catalog Draft/Script include.JS b/Client-Side Components/Catalog Client Script/catalog Draft/Script include.JS new file mode 100644 index 0000000000..f8687b4754 --- /dev/null +++ b/Client-Side Components/Catalog Client Script/catalog Draft/Script include.JS @@ -0,0 +1,27 @@ +var CatalogDraftUtils = Class.create(); +CatalogDraftUtils.prototype = Object.extendsObject(AbstractAjaxProcessor, { + saveDraft: function() { + var userId = gs.getUserID(); + var catalogItem = this.getParameter('sysparm_catalog_item'); + var draftData = this.getParameter('sysparm_draft_data'); + + var gr = new GlideRecord('u_catalog_draft'); + gr.addQuery('user', userId); + gr.addQuery('catalog_item', catalogItem); + gr.query(); + if (gr.next()) { + gr.variables_json = draftData; + gr.last_saved = new GlideDateTime(); + gr.update(); + } else { + gr.initialize(); + gr.user = userId; + gr.catalog_item = catalogItem; + gr.variables_json = draftData; + gr.last_saved = new GlideDateTime(); + gr.insert(); + } + return 'Draft saved successfully'; + }, + type: 'CatalogDraftUtils' +}); diff --git a/Client-Side Components/Catalog Client Script/catalog Draft/client script.js b/Client-Side Components/Catalog Client Script/catalog Draft/client script.js new file mode 100644 index 0000000000..7b908008cf --- /dev/null +++ b/Client-Side Components/Catalog Client Script/catalog Draft/client script.js @@ -0,0 +1,19 @@ +function onLoad() { + console.log('Auto Save Draft initialized'); + + setInterval(function() { + var draftData = { + hardware_name: g_form.getValue('hardware_name'), + quantity: g_form.getValue('quantity') + }; + + var ga = new GlideAjax('CatalogDraftUtils'); + ga.addParam('sysparm_name', 'saveDraft'); + ga.addParam('sysparm_catalog_item', g_form.getValue('sys_id')); // Catalog item sys_id + ga.addParam('sysparm_draft_data', JSON.stringify(draftData)); + + ga.getXMLAnswer(function(response) { + console.log('Draft saved: ' + response); + }); + }, 120000); // Every 2 minutes +}