How to find the child table index of a form?

I’ve created a doctype and added a child doctype in that. Now, I want to do some calculation based on the child doctype field values. I’m attaching a screenshot of the child doctype.

I’m attaching the screenshot of main doctype.

So, I want to do the calculation for the amount field in this above screenshot and for that I’m using the following client script.

frappe.ui.form.on('Invoice', {
	period_of_deployment(frm) {
		frm.set_value("amount", Math.round((frm.doc.rate / frm.doc.days) * frm.doc.period_of_deployment))
	}
})

So, what is the syntax to get the child table field index ?

@Rahul_Singh what exactly you trying to calculate ?

@bahaou I want to calculate the amount of a product (Service Product) which depends on Number of Days in a month and Period of Deployment. So, the product is a Security Guard Service which is deployed in a company. So, I want to calculate the the amount based on the days and for that I’ve created a doctype where the user can select the company and month and I’ve added a service doctype which is a child table. So, user can add multiple rows of services for example Security Guard, Cleaner, Cook etc. and based on their working days user can calculate the final amount.

Use this

frappe.ui.form.on("Invoice", "refresh", function(frm) {
	for (let i in frm.doc.service_info) {
		frm.doc.service_info[i].amount = Math.round((frm.doc.service_info[i].rate / frm.doc.service_info[i].days) * frm.doc.service_info[i].period_of_deployment)
	}
	frm.refresh_field("service_info");
});
1 Like

@mohitchechani Thanks, it is working for me. I want to set the amount field value while entering the period of deployment. But right now the calculation is happening after saving the form which is also ok for me. So, thankyou for this solution.