How can I make a "runtime" calculation?

I have these Grid on my system, how can I make the runtime calculation (#3 = #1 * #2)

My DocType Name: nota_entrada_itens

Tried these on nota_entrada_itens.js:

frappe.ui.form.on("Nota Entrada Itens", "vlr_unit", function(frm) {
	cur_frm.set_value("vlr_total", (flt(frm.doc.vlr_unit) * flt(frm.doc.qtde) ));
});

and these:

frappe.ui.form.on("Nota Entrada Itens", "vlr_unit", function(frm) {
	frm.set_value("vlr_total", (flt(frm.doc.vlr_unit) * flt(frm.doc.qtde) ));
});

but nothing happen…

Any thoughts?

Regards

If nothing happens and no errors reported please specify your assumptions and references that what you have done should work?

Try something like this.

frappe.ui.form.on('<< Child Table DocType Name >>', {
	<< Field name on child table #1 >>: function(frm, cdt, cdn) {
		let gridRow = frm.open_grid_row();
		if (!gridRow) {
			gridRow = frm.get_field('<< Field name of child table on main form >>').grid.get_row(cdn);
		}
		calAmount(gridRow);
	},
	<< Field name on child table #2 >>: function(frm, cdt, cdn) {
		let gridRow = frm.open_grid_row();
		if (!gridRow) {
			gridRow = frm.get_field('<< Field name of child table on main form >>').grid.get_row(cdn);
		}
		calAmount(gridRow);
	},
});


function calAmount(gridRow) {
	let qty = gridRow.on_grid_fields_dict.<< Field name on child table #1 >>.value;
	let rate = gridRow.on_grid_fields_dict.<< Field name on child table #2 >>.value;
	let amount = qty * rate;
	frappe.model.set_value(
		gridRow.doc.doctype,
		gridRow.doc.name,
		'<< Field name on child table #3 >>',
		amount
		);
}
3 Likes

Run your code on the event refresh and it should work

Hey @fellipeh,

As far as I could understand, you want to make changes a cell of the grid based on the changes you do in other cell of the table. For the that you have to first listen to the change being done in a particular cell. The code which you have pasted should work. Check the name of fields and put js debug points to check whether it reaches there… I will put a generic code here, may be reviewing it may help you to identify your problems. Generally the syntax should be as follows:

frappe.ui.form.on("<Child Table Name>", "<Fieldname>", function(frm, cdt, cdn) { 

    var item = locals[cdt][cdn]; // Fetching field from child table type and name
    var val = 1000 // Some value here
    item.result_field = val; // you may require to refresh 
}); 

To refresh try cur_frm.refresh() or cur_frm.dirty(); and see if it changes the table.

Regards,

Parth

Thanks everyone!!

Works fine… here is my final solution:

frappe.ui.form.on('Nota Entrada Itens', {
	"qtde": function(frm, cdt, cdn) {
		let gridRow = frm.open_grid_row();
		if (!gridRow) {
			gridRow = frm.get_field('itens').grid.get_row(cdn);
		}
		calAmount(gridRow);
	},
	"vlr_unit": function(frm, cdt, cdn) {
		let gridRow = frm.open_grid_row();
		if (!gridRow) {
			gridRow = frm.get_field('itens').grid.get_row(cdn);
		}
		calAmount(gridRow);
	},
});


function calAmount(gridRow) {
	let qtde = gridRow.on_grid_fields_dict.qtde.value;
	let vlr_unit = gridRow.on_grid_fields_dict.vlr_unit.value;
	let amount = qtde * vlr_unit;
	frappe.model.set_value(
		gridRow.doc.doctype,
		gridRow.doc.name,
		'vlr_total',
		amount
		);
}
1 Like

thank you sir.
is the greater than symbols part of the script(<< >>)?

I’m getting the this custom script error "
SyntaxError: In strict mode code, functions can only be declared at top level or inside a block
"
below is edited code
frappe.ui.form.on(‘Labour’, {
refresh(frm) {
qty :function(frm, cdt, cdn) {
let gridRow = frm.open_grid_row();
if(!gridRow){
gridRow = frm.grid_field(‘concreting’).grid.get_row(cdn);
}
calAmount (gridRow);
}

	rate: function(frm, cdt, cdn){
	    let gridRow = frm.open_grid_row();
	    if(!gridRow){
	        gridRow = frm.grid_field('concreting').grid.get_row(cdn);
	    }
	    calAmount (gridRow);
	}
	
	
}

});

function calAmount(gridRow){
        let qty = gridRow.on_grid_fields_dict.qty.value;
    	let rate = gridRow.on_grid_fields_dict.rate.value;
    	let amount = qty * rate;
    	frappe.model.set_value(
    		gridRow.doc.doctype,
    		gridRow.doc.name,
    		'amount',
    		amount
    		);

}

thank you sir. i works now. how would I sum up total Amount now