How to add two field values and store it in the other field in same child doctype

I wanted to add two field values of child doctype and save the resulted value in another field in the same child doctype

Can anybody please help me out how to do it ?

This may help You, @bnmahesh0017

I should code this in main doctype custom script right.

yes, the DocType which the child table linked.

This code is not working

can you explain your process briefly? @bnmahesh0017
I’m not good at coding, I have tried this method, and its working, :upside_down_face:

frappe.ui.form.on(“child”, {
a: function(frm,cdt,cdn) {
var d = locals[cdt][cdn];
frappe.model.set_value(cdt, cdn, “cc”, (d.a + d.b));
}
});

This is the code I have given, in which “child” is the child doctype name
a is the child field name to get the function triggered
a is field1
b is field2
c is fieldtotal

and field type which I have given is float

I should add a’s and b’s value and store it in c

please help me out with this

*sorry fieldtotal is cc


Here is my code

let grand_total = 0;
frappe.ui.form.on("child doctype name", {
    quantity: function(frm,cdt,cdn) {
    var d = locals[cdt][cdn];
    console.log(d);
    frappe.model.set_value(cdt, cdn, 'amount', (d.quantity * d.price));
    grand_total = grand_total + (d.quantity * d.price);
    cur_frm.set_value("grand_total",grand_total);
     }
});

You can try this!.

bro like I want to add value of column “a” and column “b” and store the values in column “c”
how to do this ?

Use below script

frappe.ui.form.on("Child Table Name", {
	field1 function(frm,cdt, cdn){
		destination_field(frm, cdt, cdn);
	},
	field2: function(frm, cdt, cdn){
		destination_field(frm, cdt, cdn);
	}
});
var destination_field = function(frm, cdt, cdn) {
	var child = locals[cdt][cdn];
	frappe.model.set_value(cdt, cdn, "destination_field", child.field1 + child.field2);
};

Library_Member.py

class LibraryMember(Document):
    #this method will run every time a document is saved
    def before_save(self):
        self.full_name = f'{self.first_name} {self.last_name or ""}'

NOTE

If the above snippet doesn’t work for you , make sure server side scripts are enabled, and then restart bench

bench --site <your_site> set-config server_script_enabled true

frappe.ui.form.on(‘Customer’, {
refresh(frm)
{

}
});

frappe.ui.form.on('Daily Meals Child', {
         refresh(frm)
         {
           frappe.msgprint("Im I n Child Script ")  
         },
        
        before_save(frm)
        {
          frappe.trigger('quantity');
        },
        
        quantity: function (frm, cdt, cdn) {
            var bill = locals[cdt][cdn];
            console.log(bill);

            frappe.model.set_value(cdt, cdn, 'total_price', (d.quantity * d.base_price));
            meal_bill += (bill.quantity * bill.base_price);
            cur_frm.set_value("meal_bill", meal_bill);
         }
    });

I Try this code but not working can u tell me what’s incorrect ?

You can use server script for this

def before_save(self):
    for item in self.items():
        meal_bill = item.quantity * item.base_price
        item.total_price += meal_bill

I’m multiplying the quantity by the base price and adding it to the total price in the child table.

in child table server script or in parent ?

where can i put this code i didn’t find it