Trigger recalculation after change in customfield

Hi ,

I have created my three variables, aaa, bbb, & ccc in my sales order header and placed this script on the sales document but nothing is happening. I have refreshed my cache. Can anyone advise on what I’m missing?

function on_change_aaa_bbb(doc, cdt, cdn){
if (doc.aaa && doc.bbb){
doc.ccc = doc.aaa*doc.bbb;
cur_frm.refresh()
}
}

cur_frm.custom_aaa = on_change_aaa_bbb;
cur_frm.custom_bbb = on_change_aaa_bbb;

You have to write three function:

frappe.ui.form.on("Sales Order", "bbb", function(frm) {
       msgprint(frm.doc.bbb);
});
frappe.ui.form.on("Sales Order", "aaa", function(frm) {
       msgprint(frm.doc.aaa);
});
frappe.ui.form.on("Sales Order", "ccc", function(frm) {
       //your logic
       msgprint(frm.doc.ccc);
});
1 Like

@kolate_sambhaji

Thank you for the prompt response

I have placed this simple logic where you indicated to place logic:
function on_change_aaa_bbb(doc, cdt, cdn){
if (doc.aaa && doc.bbb){
doc.ccc = doc.aaa*doc.bbb;
cur_frm.refresh()
The messages come up when I input values in fields aaa and bbb.

How come the computation does not automatically occur in field ccc?

user cur_frm.set_value(“field_name”, value)

1 Like

@saurabh6790 : Thank you, I’ve tried the below and still no joy, Can you point out what I’ve missed? Thanks in advance:
frappe.ui.form.on(“Quotation”, “bbb”, function(frm) {
msgprint(frm.doc.bbb);
});
frappe.ui.form.on(“Quotation”, “aaa”, function(frm) {
msgprint(frm.doc.aaa);
});
frappe.ui.form.on(“Quotation”, “ccc”, function(frm) {

    var qty = doc.aaa*doc.bbb;
   cur_frm.set_value("doc.ccc", qty);

   msgprint(frm.doc.ccc);

});

Use frm.doc.aaa * frm.doc.bbb

just use ccc instead of doc.ccc

ie, cur_frm.set_value("ccc", (frm.doc.aaa * frm.doc.bbb));

@saurabh6790

Herewith what the script looks like and yet it does nothing after values are entered in aaa & bbb

frappe.ui.form.on(“Quotation”, “bbb”, function(frm) {
msgprint(frm.doc.bbb);
});
frappe.ui.form.on(“Quotation”, “aaa”, function(frm) {
msgprint(frm.doc.aaa);
});
frappe.ui.form.on(“Quotation”, “ccc”, function(frm) {

cur_frm.set_value("ccc", (frm.doc.aaa * frm.doc.bbb));

   msgprint(frm.doc.ccc);

});

add code on aaa and bbb trigger.

@saurabh6790:

I’ve tried this code , but still nothing:

function on_change_aaa_bbb(doc, cdt, cdn){
if (doc.aaa && doc.bbb){
cur_frm.set_value(“ccc”, (frm.doc.aaa * frm.doc.bbb));
cur_frm.refresh()
}
msgprint(frm.doc.ccc);
}

@saurabh6790 - Thanks for your patience. I really appreciate your assistance

What is output of, [quote=“Eli, post:9, topic:7394”]
msgprint(frm.doc.ccc
[/quote]

@saurabh6790 - There is no message shown

what the type of field aaa and field bbb?

I have them as float

frappe.ui.form.on("Sales Order", "bbb", function(frm) {
       cur_frm.set_value("ccc", (frm.doc.aaa * frm.doc.bbb));
       msgprint(frm.doc.bbb);
});
frappe.ui.form.on("Sales Order", "aaa", function(frm) {
       cur_frm.set_value("ccc", (frm.doc.aaa * frm.doc.bbb));
       msgprint(frm.doc.aaa);
});
frappe.ui.form.on("Sales Order", "ccc", function(frm) {
       cur_frm.set_value("ccc", (frm.doc.aaa * frm.doc.bbb));
       msgprint(frm.doc.ccc);
});

try this

1 Like

@kolate_sambhaji

THANK YOU , THANK YOU , VERY VERY MUCH!

It works!

I really appreciate the help.

@saurabh6790

-Thanks for bearing with me and assisting me as well. It’s gentleman like yourself and @kolate_sambhaji that make the ERPNext journey enjoyable

@kolate_sambhaji - I’ve been testing it and I see the value ccc is only updated when value bbb changes . When value aaa changes, the calculation remember only the original value entered and doesn’t work with the new value that’s been input.

For example bbb starts as 10 ,and aaa as 5, ccc is calculated as 50, if I then change aaa to 10, the value in ccc remains as 50, However if I change bbb to 20, then ccc shows 100 even though aaa has been chnaged to 10 and the output in ccc should be now 200 .

Whats the work around to address this?

1 Like

You need to understand frappe doc event and custom script for this

https://manual.erpnext.com/contents/customize-erpnext/custom-scripts
https://frappe.io/help/hooks [doc event]

frappe.ui.form.on("Sales Order", "<field_name>", function(frm) {

in above example, sales order is doctype on which you have written custom script
ccc,aaa,bbb fieldname, if value of field ccc changes then your script run
you can also write event_name instead of fieldname,
Event name can be validate, refresh, onload

If you write validate, then script will run before save
frappe.ui.form.on(“Sales Order”, “validate”, function(frm) {

1 Like

@kolate_sambhaji -

Wonderful!, Its now working as anticipated.

You sir are a true gentleman.