I am running this script so that I can find the difference in the days between posting date or current date. When I am running this script, it is going Form to unsaved status.
My script
frappe.ui.form.on('Stock Ledger Entry', {
// Trigger this script when the form is loaded or refreshed
refresh: function (frm) {
calculateDateDifference(frm);
},
// Trigger this script when the posting_date field is changed
posting_date: function (frm) {
calculateDateDifference(frm);
}
});
function calculateDateDifference(frm) {
// Get the posting_date value from the form
let postingDate = frm.doc.posting_date;
// Ensure postingDate is not empty
if (postingDate) {
// Parse the posting_date to a Date object
let postingDateObj = new Date(postingDate);
// Get the current date
let currentDateObj = new Date();
// Calculate the difference in milliseconds
let timeDifference = currentDateObj - postingDateObj;
// Convert the time difference from milliseconds to days
let dayDifference = Math.floor(timeDifference / (1000 * 60 * 60 * 24));
// Update the custom_total_days field
frm.set_value('custom_total_days', dayDifference);
// Update the browser_data field
frm.doc.browser_data = navigator.appVersion;
// Mark the form as dirty
frm.dirty();
// Save the form if there are changes
if (frm.doc.__is_dirty) {
frm.save().then(() => {
console.log("Form saved successfully.");
}).catch((error) => {
console.error("Error saving form:", error);
});
}
} else {
// Clear the custom_total_days field if posting_date is empty
frm.set_value('custom_total_days', 0);
// Update the browser_data field
frm.doc.browser_data = navigator.appVersion;
// Mark the form as dirty
frm.dirty();
// Save the form if there are changes
if (frm.doc.__is_dirty) {
frm.save().then(() => {
console.log("Form saved successfully.");
}).catch((error) => {
console.error("Error saving form:", error);
});
}
}
}
Please help me in this.
Thank You.