I am running this java script for auto save but it is running even without any change in the form.
I want that when there is some change in the form then it should auto save otherwise it should not happen.
My-code
frappe.ui.form.on('Developer', {
refresh(frm) {
setInterval(() => {
cur_frm.save();
}, 3000); // 300000 is the interval in miliseconds
}
});
With this code I am able to auto save one field, but how to do it on the whole form, please guide me
Updated-code
frappe.ui.form.on('Developer', {
refresh: function(frm) {
// Track whether the form is currently being edited
let ischange = false;
// Function to handle auto-saving
function autoSave() {
if (ischange) {
frm.save(); // Save the form
ischange = false; // Reset dirty flag after saving
}
}
// Attach event listeners to detect changes in the form
frm.fields_dict['registration_no'].$input.on('input', function() {
ischange = true; // Set dirty flag when input changes
});
// Set interval to auto-save every 3 seconds
setInterval(autoSave, 3000);
}
});
This code is working but there is one problem,
Let us understand with an example, there is one or two mandatory fields in the whole form, until we do not fill the mandatory field, the document will not be saved, but the problem is that when we do not fill the requirement field, the script should run once, but the script is running continuously, I am attaching the screenshot below, please see
Missing field messages and these messages keep on coming continuously. I want the script to show that just like saving happens once, the missing field messages should also come only once and then stop.
There can be scenarios where we will have many mandatory fields 20-30 sometimes in our custom forms, so giving each mandatory field in the script sometimes will not be better practise we feel.
We are trying to Auto-Save the forms only when any of the field value changes and not other wise for eg google documents.
Also - if mandatory field is missing and the script starts working, then how to prevent mandatory validation messages showing up multiple times, it show up only once.
Maybe you should check if any mandatory field has no value … and save form just in that case.
frappe.ui.form.on('Developer, {
refresh(frm) {
let isAnyFieldRequired = false;
setInterval(() => {
console.log("check")
if (frm.is_dirty()){
for (let fieldname in frm.fields_dict) {
if (frm.fields_dict.hasOwnProperty(fieldname)) {
let field = cur_frm.fields_dict[fieldname];
if (field.df.reqd) {
if (!field.value && field.df.fieldname != '__newname') {
isAnyFieldRequired = true;
break;
}
}
}
}
}
if (isAnyFieldRequired && frm.is_dirty()){
frm.save();
}
}, 10000); // 300000 is the interval in miliseconds
}
});