Cur_frm.set_value not updating database even after save

Hi,
I have custom field sp_outstanding_amount.
I am updating it using custom script.
I found strange behavior.

  1. When we save document sp_outstanding_amount will be saved on UI but not updated in db
  2. when we save again, then it will be updated in database.
  3. this behavior leads to error in report.

Please let me know if someone has same issue.

custom script.

frappe.ui.form.on("Sales Invoice", "refresh", function(frm) {
    if (frm.doc.payments_made > frm.doc.total_commission){
         cur_frm.set_value("sp_outstanding_amount", 0);
         cur_frm.set_value("payments_made", 0);
         msgprint("Please check total commission.");
    } 
    else if(frm.doc.payments_made <= frm.doc.total_commission) {
        var comm = frm.doc.total_commission - frm.doc.payments_made;
        cur_frm.set_value("sp_outstanding_amount", comm);
    }
});

Refresh is called after save (?)

You should add this in validate event.

Yes, refresh is called after save.

We have payments_made and sp_outstanding_amount allow on submit field.
Outstanding commission amount is automatically calculated when we enter payments made detail.

In this case validate event not working, since we are updating document after submit.

I have solved this using,

frappe.ui.form.on("Sales Invoice", "payments_made", function(frm) {
    if (frm.doc.payments_made > frm.doc.total_commission){
         cur_frm.set_value("sp_outstanding_amount", 0);
         cur_frm.set_value("payments_made", 0);
         msgprint("Please check total commission.");
    } 
    else if(frm.doc.payments_made <= frm.doc.total_commission) {
        var comm = frm.doc.total_commission - frm.doc.payments_made;
        cur_frm.set_value("sp_outstanding_amount", comm);
    }
});

Where I find all doc events?

1 Like

On client side, validate, refresh, onload and onload_post_render are the main events/

3 Likes