A custom doctype that need to click save twice, how to fix it?

Hey guys. Yesterday I was creating my custom doctype and custom field.
And I put some custom script on my custom field.
My custom field that can make calculation and show the result.
After that I put “read-only” custom script.

The result is I need to click on save twice then the the status change to “save”

My question is, how to automate the calculation without clicking on save button and how to make the record save only for one “click” (save button)?

Share what you did so far. Paste your script here.

Thanks for your reply @Sangram

Here is my script:

frappe.ui.form.on("Employee Receipt", {
    refresh: function(frm) {
        // use the __islocal value of doc, to check if the doc is saved or not
        frm.set_value("balance_need_to_pay", (frm.doc.total_need_to_pay - frm.doc.payment));
	frm.set_df_property("balance_need_to_pay", "read_only", frm.doc.__islocal ? 0 : 1);
    }
});

do
frm.refresh_field('balance_need_to_pay')
after set_df_property

Thanks for your reply @netchampfaris

do you mean the code like this:

frappe.ui.form.on("Employee Receipt", {
    refresh: function(frm) {
        // use the __islocal value of doc, to check if the doc is saved or not
        frm.set_value("balance_need_to_pay", (frm.doc.total_need_to_pay - frm.doc.payment));
	frm.set_df_property("balance_need_to_pay", "read_only", frm.doc.__islocal ? 1 : 1);
	frm.refresh_field('balance_need_to_pay');
    }
});

Its okay guys.
I already fixed it.
I just add new trigger “validate” to validate the calculation.
Here is my code:

// make calculation on fields
frappe.ui.form.on("Employee Receipt", {
    validate: function(frm) {
        frm.set_value("balance_need_to_pay", (frm.doc.total_need_to_pay - frm.doc.payment));
        frm.refresh_field('balance_need_to_pay');
    }
});

// make a field read-only after saving
frappe.ui.form.on("Employee Receipt", {
    refresh: function(frm) {
        // use the __islocal value of doc, to check if the doc is saved or not
        frm.set_df_property("balance_need_to_pay", "read_only", frm.doc.__islocal ? 0 : 1);
    }
});

Thanks for all that help me :slight_smile: