Custom script not updating field unless I save first

Team,

I have a custom field of type Data called “costing” in Quotation, and I want it to be updated once the grand_total standard field in Quotation is updated. I wrote the below custom script which updates but only if I save the document. How can I make it so that the costing field is updated always the same way the grand_total field is being updated, without the need to save.

frappe.ui.form.on("Quotation", "validate", function(frm) {
    frm.set_value("costing", frm.doc.grand_total * 0.5);
});

Regards,
Ahmed

1 Like

@ahmed

validate run only when you save the form.

frappe.ui.form.on("Quotation", "refresh", function(frm) { frm.set_value("costing", frm.doc.grand_total * 0.5); });

1 Like

Hi @ahmed,

Try below code

frappe.ui.form.on("Quotation", "grand_total", function(frm) {
    frm.set_value("costing", frm.doc.grand_total * 0.5);
});

Thanks, Rohit

2 Likes

Dear @Sangram

refresh has the same effect as validate. Didn’t sort out the issue. Any other ideas?

Regards,
Ahmed

Hi @rohit_w, this didn’t work at all. Not even updating on saving.

Hi @ahmed,

Ok, try below one

frappe.ui.form.on("Quotation", "before_save", function(frm) {
    frm.set_value("costing", frm.doc.grand_total * 0.5);
});

Thanks

3 Likes
frappe.ui.form.on("Quotation", "grand_total", function(frm) {
    frm.set_value("costing", parseFloat(frm.doc.grand_total)  * 0.5);
});

Please check field names if you have further issues.
Validate is a way to check BEFORE saving - not live in the app. For this use solution which @rohit_w posted.

1 Like

Hello @rohit_w, before_save is also not updating until you save!. How is the grand_total field updating easily like this? Will the field type has an effect? Mine is Data, with all default settings for a Data type.

Thanks @Marcin_Walkow, parsefloat didn’t help too. I had the same understanding about validate, but couldn’t find anywhere guidance of how I can get the updates Live in the app.

@ahmed

You can set values live by:

frm.set_value("field_to_update", value);

Events are handled like this:

frappe.ui.form.on("doctype_name", "field_or_event", function(frm) {
   your_code_here
});

Not sure why it is not working though.

1 Like

Hi @ahmed,

Check how did you updating grand total value?
@Marcin_Walkow is right, if you used frm.set_value(‘grand_total’, value) to update the grand_total then below code will work

frappe.ui.form.on("Quotation", "grand_total", function(frm) {
    frm.set_value("costing", frm.doc.grand_total * 0.5);
});

@rohit_w, I am actually not updating grand_total by code, but rather by adding items in Quotation Items, or adding Taxes and Charges, or Additional Discounts. So I see the value of grand_total changing in front of me, but not the costing value.

@ahmed,

You need to write function to calculate costing and call the function on trigger of qty, rate, price list rate, which is the only customisable solution for it.
OR
Add one more custom field with data type button(“calculate_costing”) and on click of button calculate the costing

frappe.ui.form.on("Quotation", "calculate_costing", function(frm) {
    frm.set_value("costing", frm.doc.grand_total * 0.5);
});

Thanks, Rohit

@ahmed

Please use debugging tools chrome/firefox to check what is the current value of
frm.doc.grand_total

in the event call (maybe there is race condition - you try to change value but have old one when assigning)

1 Like

@rohit_w, these 2 options sounds doable, but I am really starting my way around coding (even these simple things). Would you be so kind by giving a complete example of how to write the function (simple formula of grand_total * 0.5), and how to call it on trigger on rate and discount_amount?

Also, for the button where will I write the code? and how to?

I know that this might be a little too much, but your help will be much appreciated as we advance out use of ERPnext.

Regards,
Ahmed

Make custom script for quotation and add below code

frappe.ui.form.on("Quotation", "calculate_costing", function(frm) {
    frm.set_value("costing", frm.doc.grand_total * 0.5);
});

Here you need to add Calculate Costing custom field whose field type is Button after costing field. After preparing quotation, click on button which show you the costing amount before save

Thanks, Rohit

@rohit, so the button idea works fine. Thanks!

what about the function and triggers idea? for my own development.