I know that his is probably asked quite a bit and through my searching and following advice I still do not have a solution. I am trying to populate an integer field that is the result of the difference between 2 dates within a child table. Everything I try gives me a calculation of 0. I have tried force refreshing after changing the script. Here is a copy of the script below… Any help would be greatly appreciated.
// Client Script for Doctype: Injury_Illness Report
// Triggers calculation ONLY when BOTH start_date and return_date are presentfrappe.ui.form.on(‘Days Away’, {
start_date: function(frm, cdt, cdn) {
trigger_calculation_if_ready(frm, cdt, cdn);}, return_date: function(frm, cdt, cdn) { trigger_calculation_if_ready(frm, cdt, cdn); }});
function trigger_calculation_if_ready(frm, cdt, cdn) {
let row = locals[cdt][cdn];// Only proceed if BOTH dates are filled //if (!row.start_date || !row.return_date) { // Optional: clear the field when incomplete (uncomment if desired) // frappe.model.set_value(cdt, cdn, 'days_away', 0); // return; ////} // Optional: prevent invalid range //if (row.return_date < row.start_date) { // frappe.msgprint(__("Return Date cannot be before Start Date")); // frappe.model.set_value(cdt, cdn, 'number_of_days_away', 0); // return;// }
// OSHA-compliant calculation // Returns calendar days: return_date - start_date // (does NOT count the return date itself) let days = frappe.datetime.get_diff(row.return_date, row.start_date); // Optional: OSHA allows capping at 180 days // Comment out the next 3 lines if you want to record actual values >180 if (days > 180) { days = 180; } frm.set_value(cdt, cdn, 'number_of_days_away', days); frm.refresh_field("number_of_days_away");}
