How to set child table field value based on other field in same doctype

frm.set_value("project_reference", r['project_name']);

it’s not working:

Hi @Murugan,

I can’t really see the subtable mentioned in the title, but are you looking for this:

frappe.model.set_value(DOCTYPE OF SUBTABLE, SUBTABLE ENTRY NAME, SUBTABLE FIELD NAME, VALUE)

This will set the VALUE into the field SUBTABLE FIELD NAME of the instance with name SUBTABLE ENTRY NAME of DoctType DOCTYPE OF SUBTABLE (sorry for the upper-case, I hope it helps readability). E.g.

frappe.model.set_value(cur_frm.doc.items[0].doctype, cur_frm.doc.items[0].name, "description", "Hello World");

Hope this helps.

2 Likes

this my code


So it is not really a subtable/child table but a related DocType? Have you tried with

cur_frm.set_value('field_name', 'value');

Is the variable directly in the response object or maybe in r.message (i.e. what is console.log(r['project_name'])?

console.log(r[“project_name”]) ----> undefine

Check the structure of your response object. I guess it would be

console.log(r.message['project_name'])

or

console.log(r.message.project_name)

both are same
console.log(r.message[‘project_name’])----> undefine
console.log(r.message.project_name) —> undefine

From your screenshot in the initial query it seems that r.message is an arry of dict, try to access the first element of the array

r.message[0].project_name

otherwise, you’re gonna have to share your Python code at return… :wink:

1 Like

Thanks for everyone finally i got o/p

You’re welcome :wink:

i have another small problem. here if i select ART7 it’s contain multiple value but i got single value what i change code or fieldtype (i want multiple value). now project-reference datatype field in data

If your server-side code returns multiple projects, loop through the array and append into the output field (type data), e.g. like this:

var projects = r.message[0].project_name;
for (i = 1; i < r.message.length; i++) {
    text += ", " + r.message[i].project_name;
} 
cur_frm.set_value('project_reference', projects);

Hope this helps.

Hello Sir,
Sir can you help me with real example for this
frappe.model.set_value(cur_frm.doc.items[0].doctype, cur_frm.doc.items[0].name, “description”, “Hello World”);

Please help me sir.
Thanks in advance

Hi @vivek22793,
sure, why don’t you open a new thread with your real example? Then this is also useful for others… Happy to support you on that!

To the code snippet: The parameter values are the doctype of the child item (e.g. “Quotation Item”), the name of the child item (typically something like “85c56973b1”), the field name of the child item and the value to write into that field… Normally, this comes from the loop where you go through the items…

Thanks For your valuable reply sir.
Sir in my case i have one doctype chagres fines and loans for employee which consist two table field.
1)employee table
2)Types of loans fines and charges- this table consist 3 field
1) actual_amount
2) initial_payment
3) pending_amount
so sir on the basis of actual_amount and initial_payment i want to set the value of pending_amount

sir my code is

frappe.ui.form.on("Charge And Fines And Loan Of Employee", "validate",  function(frm, dt, dn) {
	var row = locals[dt][dn];
	for(i= 0; i<row.fines_charges_and_loan.length; i++)
		{
			if(row.fines_charges_and_loan[i]["total_amount"] > 0){
				if(row.fines_charges_and_loan[i]["initial_payment"] >= row.fines_charges_and_loan[i]["total_amount"]){
					msgprint("Initial payment can not be grater than or equal to Amount");
					validated = false;
					break;
					console.log("after break:= ", i )	
				}
				if(row.fines_charges_and_loan[i]["initial_payment"] < row.fines_charges_and_loan[i]["total_amount"]){
					//row.fines_charges_and_loan[i]["pending_amount"]= row.fines_charges_and_loan[i]["total_amount"] - row.fines_charges_and_loan[i]["initial_payment"]
					frappe.model.set_value(fines_charges_and_loan[i].doctype, cur_frm.doc.fines_charges_and_loan[i].name , "pending_amount", row.fines_charges_and_loan[i]["total_amount"] - row.fines_charges_and_loan[i]["initial_payment"])
					
				}
			}
				
		}
 });

The code looks fine as far as I can see, what is the problem with it?

It might be a bit easier to read if you use a forEach instead of the [i] loops:

frappe.ui.form.on("Charge And Fines And Loan Of Employee", "validate",  function(frm) {
	frm.doc.fines_charges_and_loan.forEach( function(row) {
		if(row.total_amount > 0) {
			if(row.initial_payment >= row.total_amount) {
				msgprint("Initial payment can not be grater than or equal to Amount");
				validated = false;
				break;
			}
			if(row.initial_payment < row.total_amount) {
				var pending_amount = row.total_amount - row.initial_payment;
				frappe.model.set_value(row.doctype, row.name , "pending_amount", pending_amount);				
			}
		}
	});
});

Sir the above code is not working can you help me.
code is not setting the value to pending_amount.

Thank You so much sir its working now.
Thanks a lot.:smile:

1 Like