Try ERPNext Buy Support Partners Foundation

Issue with Custom Script for setting value

Hi ,

I’m trying to set a field’s value using the custom script below:

frappe.ui.form.on("Sales Invoice", "outstanding_amount", function(frm, dt, dn) {
	var d = locals[dt][dn];
	frappe.model.set_value(dt, dn, "outstanding_tranches", (d.outstanding_amount / d.tranch_amount));
});

The problem is:

  1. The script doesn’t work unless I use the “refresh” trigger but I want it to run whenever the value in the ‘outstanding_amount’ field changes

  2. When I use the “refresh” trigger, the field get’s updated on Saving BUT the status of the Sales Invoice form remains as ‘Not Saved’. I need to click ‘Save’ a second time before the status is properly updated and the Submit button appears

Any help here would be much appreciated

Thanks!

Still hoping for some assistance or suggestions here…

Thanks

question, is this a child table field or not?

Hi @johnskywalker

Thanks for responding. No, all fields are on the main (parent) doc…

not sure if this will make any difference, try cur_frm.set_value(“your_field”,“some_value”);

also pls check your console, there might be errors.

Hi @johnskywalker

Still the same issues highlighted in the original post… it only works with refresh trigger and even then needs to be saved twice

Any other thoughts?

Setting the trigger as a field with values that are manually selected seems to work normally except for when the document is initially created

I have a feeling it may just be something very small I’m missing here…

Thanks

Hello,

Have you tried adding a frm.refresh_field("name_of_field_that_was_updated") after the set_value() function?

Hi @littlehera

Thanks for your response. Yes I did but it was still the same!

Oof. :cry:

The fields aren’t in a child table right? If that’s so, you might want to change the frappe.ui.form on to pass just frm (no need for dt, dn since those are for child tables), and using frm.doc.outstanding_amount, frm.doc.outstanding_tranches to access the values.

It’ll look like this:

frappe.ui.form.on("Sales Invoice", "outstanding_amount", function(frm){
	var temp_var = frm.doc.outstanding_amount/frm.doc.tranch_amount;
	frm.set_value("outstanding_tranches", temp_var);
});

Hi @littlehera

Still the same :pensive:

Refresh is still the only trigger that works and it doesn’t update the status field. I have to still manually click on Save (if the doc is in Draft) or Update (if the doc is Submitted) before the status of the doc gets updated accordingly

It’s as though the set_value method requires a manual trigger or something…

Hi all,

Can anyone proffer a solution to this or should I assume it’s a bug?

Many thanks

Still need some assistance with this…

It seems that the field trigger doesn’t work unless the field is manually updated. Not sure if there’s a reason for that

I don’t mind using the “refresh” trigger but I need to get around the ‘Not Saved’ issue which always requires the user to manually click on the Save button a second time (or Update button if the document is in a Submitted state)

Would appreciate any suggestions

Thanks

Hi,facing the same issue i am trying to set value in js after document submitted value is also set but problem is i have to click on the update button…i need to update this via code is it possible? any idea