Calculations in Child Table's Fields

Is it possible to do calculations on child-table’s field customization level ??

Let say I have 3 fields in child table-> Field 1 (LinkType), Field 2(Float Type), and Field 3(Float Type).
I selected a value in field 1 and fetched a value of that Link, let say 4 . What I want is when i enter value in field 2, field 3 automatically gets value as ==>[ 4(from field 1’s link) + field’s 2 value ].
For example I selected any value in field and it fetched 4…and in field 2 i inserted 8…so on field 3…it automatically inserts 4+8 =12 …

Note all this is happening in child table… How to approach for so using field customization level. (Fetch_from is helping me to get the value of link field but how to get the value inserted in field 2 at that time and perform addition)

1 Like

This code should work:

frappe.ui.form.on('Child Table Doctype Name', {
    field2: function (frm, cdt, cdn) {
        var row = locals[cdt][cdn];
        if(row.field2){
            frappe.model.set_value(cdt, cdn, 'field3', row.field1+row.field2);
        }
    }
});
2 Likes

Thanks

I have the same scenario with Sales Order Item
I have added three new fields by customize the form for sales order item and then I have added the following custom script for “sales order item” doc_type and it is not working.

frappe.ui.form.on(‘Sales Order Item’, {
board_length: function (frm, cdt, cdn) {
var row = locals[cdt][cdn];
if(row.board_length){
frappe.model.set_value(cdt, cdn, ‘curve’, flt(row.board_count * row.board_length));
}
}
});

any help will be highly appreciated

Hello @arefaii,
Try, row.curve=row.board_count*board_length; cur_frm.refresh_fields("items");

Thanks for replying @ROHAN_JAIN1 but unfortunately it didn’t work.

Can any body help on this code please I have lost 4 days on this issue and yet it is not solved
this is the script I’m using

frappe.ui.form.on(“Sales Order Item”, {
board_length: function(frm, cdt, cdn)
board_count: function(frm, cdt, cdn){
let d = locals[cdt][cdn];
let q = flt(d.board_length * d.board_count);
d.qty=d.count*d.length;
cur_frm.refresh_fields(“items”);

}

});

I have added the following statement to check if the code is running or not

frappe.msgprint(__(“Checking Message”));

and this did not show !!!

Hello @arefaii,
Which doctype do you use for your custom script?
I usually use the “Sales Order” doctype. Your code seems ok.

This code should be in the parent doctype js file
please check and share screen shot

Thank you @OmarJaber
Actully after trying a lot I find out that when you create the custom script you should create it using the doc-type of the parent document first then in the code we should depend on the child document in the code like I create the custom script on the sales order doc-type but in the code I used the child table name

frappe.ui.form.on(“Sales Order Item”, { …

Thank you again, I think the documentation on this issue is very poor I wish if it can be updated.

Every child table triggers depends on their doctype linking with the child table name, and child table files keep with no edit on it
Here is an example:
https://frappe.io/docs/user/en/guides/app-development/trigger-event-on-deletion-of-grid-row

And you can ask anything here in this community