How to create auto save functionality in the incident form
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
12-25-2024 01:10 AM - edited 12-25-2024 01:14 AM
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
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
12-25-2024 07:23 AM
The best way I could think of is separating the fields in different sections/stages to make them mandatory.
eg. when incident is in draft, category and sub category is not mandatory, but when state is changed to ready, you make category, sub category and assignment group mandatory. like wise to make state as in Progress, the assigned to field is mandatory.
By this approach, you will follow the best practices of ServiceNow and even for process, collect the necessary data from a user in logical way, and as UI policies works on different states, the performance will be much higher.
@Ankur Bawiskar @Dr Atul G- LNG _ Please suggest if there is anything to add.
---------------------------------------------------------------------------------------------------
Please mark my answer as helpful/correct if it resolves your query.
Thanks,
Nilesh Wahule
---------------------------------------------------------------------------------------------------
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
12-26-2024 05:38 AM
Hope you are doing well !!
Did my reply answer your question?
If my response helped, please mark it correct/helpful and close the thread so that it benefits future readers.
---------------------------------------------------------------------------------------------------
Thanks,
Nilesh Wahule
---------------------------------------------------------------------------------------------------
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
12-25-2024 05:39 AM
Hi @Amol Pawar
Practically it is not a valid requirement, the reason being it will increase the load on the system and every time a new version needs to be saved. While fetching system needs to see the last saved and get it back. So don't go for this one.
If my response proves useful, please indicate its helpfulness by selecting " Accept as Solution" and " Helpful." This action benefits both the community and me.
Regards
Dr. Atul G. - Learn N Grow Together
ServiceNow Techno - Functional Trainer
LinkedIn: https://www.linkedin.com/in/dratulgrover
YouTube: https://www.youtube.com/@LearnNGrowTogetherwithAtulG
Topmate: https://topmate.io/atul_grover_lng [ Connect for 1-1 Session]
****************************************************************************************************************
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
12-25-2024 01:02 PM
Hi @Amol Pawar,
Please re-visit this use case, the incident form already has Save and Update button for storing the data.
The form has various type of fields and applying auto-save feature is not feasible solution because there is no limit to determine the "updates any details", any single character change also consider change/update, this will keep form updating and referesh the page, which will cause the performance issue.
-Thanks,
AshishKM
Please mark this response as correct and helpful if it helps you can mark more that one reply as accepted solution
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
12-25-2024 10:45 PM
Hi @Amol Pawar ,
You can do this, follow below steps. I have tested it and working fine.
- Create one onload client script and use below code.
- Temp array should have all the fields which you want to auto save. Note: g_form.getEditableFilds() wont work here. you can have this fields in property and get it in client script.
function onLoad() {
//Type appropriate comment here, and begin script below
var autoSaveInterval = 10000; // Auto-save interval in milliseconds (10 seconds)
var autoSaveTimer;
var tempArr = ['short_description', 'state', 'category', '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('autoSaveUtils');
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();
}
- Now in script include, use below code.
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('incident');
if (gr.get(sysId)) {
for (var key in fields) {
gs.log('runjay' + key[key] , 'Runjay');
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
});
}
},
-------------------------------------------------------------------------
If you found my response helpful, please consider selecting "Accept as Solution" and marking it as "Helpful." This not only supports me but also benefits the community.
Regards
Runjay Patel - ServiceNow Solution Architect
YouTube: https://www.youtube.com/@RunjayP
LinkedIn: https://www.linkedin.com/in/runjay
-------------------------------------------------------------------------