Custom code to add a line item on Quotation

Hello all

I’m trying to add a line item on a Quotation with custom code.
I manage to do so but the frappe.model.set_value() function seems to replace the description and rate.
Furthermore, it correctly triggers the calculation of both the amount and total, but it uses the standard selling rate rather than the rate provided by the custom code.

frappe.ui.form.on("Quotation", {
	onload: function(frm) {
		var g = frm.get_field('items').grid;
		g.add_custom_button("Add Fee", function() {
			var new_row = g.add_new_row();
			var row_name = new_row.name;
			frappe.model.set_value(g.doctype, row_name, "item_code", "item3");
			frappe.model.set_value(g.doctype, row_name, "item_name", "Item 3");
			frappe.model.set_value(g.doctype, row_name, "description", "Fee"); //Could be different per Quotation
			frappe.model.set_value(g.doctype, row_name, "qty", 2);
			frappe.model.set_value(g.doctype, row_name, "uom", "Nos");
			frappe.model.set_value(g.doctype, row_name, "rate", 2.00);
			frappe.model.set_value(g.doctype, row_name, "amount", 1.00);
			frm.refresh_field("items");
		});
	}
});

How can I add a line item with custom code and still trigger the calculation of amount and total using the rate provided by the custom code?

In child_table, set_value might not work in the way you expected as it triggers various fetches related to the respective fields. To overcome this and still trigger the auto calculation you can use the below customization

frappe.ui.form.on("Quotation", {
	onload: function(frm) {
		var g = frm.get_field('items').grid;
		g.add_custom_button("Add Fee", function() {
			var new_row = frm.add_child("items");
			new_row.item_code = "item3";
			new_row.item_name = "Item 3";
			new_row.description = "Fee";
			new_row.qty = 2;
			new_row.uom = "Nos";
			new_row.rate = 2.00;
			new_row.amount = 1.00;
			frm.refresh_field("items");
            //Triggers toals and tax calculation
            frm.cscript.calculate_taxes_and_totals(); 
		});
	}
});
1 Like

@manasan you’re a star. Thanks so much.