forked from ServiceNowDevProgram/code-snippets
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathbasic_implementation.js
More file actions
58 lines (49 loc) · 1.78 KB
/
basic_implementation.js
File metadata and controls
58 lines (49 loc) · 1.78 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
/**
* Basic Auto-save Draft Implementation
* This version provides core functionality for auto-saving catalog item form data
*/
function onLoad() {
var autosaveInterval = 60000; // 1 minute
// Try to restore previous draft
restoreLastDraft();
// Set up auto-save interval
setInterval(function() {
if (g_form.isModified()) {
saveDraft();
}
}, autosaveInterval);
}
function saveDraft() {
try {
var draftData = {};
g_form.serialize(draftData);
var draftKey = 'catalogDraft_' + g_form.getUniqueValue();
sessionStorage.setItem(draftKey, JSON.stringify({
timestamp: new Date().getTime(),
data: draftData
}));
g_form.addInfoMessage('Draft saved automatically');
} catch (e) {
console.error('Error saving draft: ' + e);
}
}
function restoreLastDraft() {
try {
var draftKey = 'catalogDraft_' + g_form.getUniqueValue();
var savedDraft = sessionStorage.getItem(draftKey);
if (savedDraft) {
var draftData = JSON.parse(savedDraft);
var timestamp = new Date(draftData.timestamp);
if (confirm('A draft from ' + timestamp.toLocaleString() + ' was found. Would you like to restore it?')) {
Object.keys(draftData.data).forEach(function(field) {
g_form.setValue(field, draftData.data[field]);
});
g_form.addInfoMessage('Draft restored from ' + timestamp.toLocaleString());
} else {
sessionStorage.removeItem(draftKey);
}
}
} catch (e) {
console.error('Error restoring draft: ' + e);
}
}