ERPNext Foundation ERPNext Cloud Chat Blog Discuss Frappé* Donate

Issue with Custom Script for setting value


#1

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!


#2

Still hoping for some assistance or suggestions here…

Thanks


#3

question, is this a child table field or not?


#4

Hi @johnskywalker

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


#5

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.


#6

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?


#7

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


#8

Hello,

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


#9

Hi @littlehera

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


#10

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);
});

#11

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…


#12

Hi all,

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

Many thanks