Not able to update value in child table [Solved]

Hi,
I am using the below code but I am not able to update the value of a field costom_code_ref in child table.
Below is my code:

frappe.ui.form.on("Sales Order", "fill_items", function(frm, cdt, cdn){

var d = locals[cdt][cdn];

 for (var i in frm.doc.items)
    {     
	
	 var d1 =  { "item_code":  frm.doc.items[i].item_code
                   };      
frappe.call({
                method: "library_management.item_script.generate_custom_codes",
                args: d1,
                callback: function(r) { 
		cur_frm.set_value("po_no",r.message);
                frappe.model.set_value(cdt, cdn, "custom_code_ref",r.message);
              
		
               }
           
            });
   }
});

Any idea?

Regards
Ruchin Sharma

@ruchin78

go through following thread

But, I want to use it in Sales Order not in Sales Order Item like this.

https://frappe.github.io/frappe/user/en/tutorial/form-client-scripting

Edit: I tried what is mentioned in the above link but that is also not working for me.

Regards
Ruchin Sharma

@ruchin78

Try

adding var p = frm.doc;

replace it with for parent
frappe.model.set_value(p.doctype, p.name, "custom_code_ref",r.message);'
replace it with for child
frappe.model.set_value(d.doctype, d.name, "custom_code_ref",r.message);

@Nick
It is still not working.

Regards
Ruchin Sharma

@ruchin78

Did you try console.log(r); whether callback returns value. Also check whether field name custom_code_ref is correct.

@Nick
Yes, I did both the things and both are OK.

Regards
Ruchin Sharma

@ruchin78

Try this

frappe.ui.form.on("Sales Order Item", "fill_items", function(frm,cdt,cdn) {
	d = locals[cdt][cdn]
	frappe.call({
		method: "method_path",
		args: {args},
		callback: function(r) {
			d.custom_code_ref = r.message
		}
	})
});

fill_items and costom_code_ref both fields are in child table right ?

@Sangram
fill_items is in parent whereas custom_code_ref is in child, that is why I can’t use ur code.

Regards
Ruchin Sharma

@ruchin78

I assumed both fields are in child table itself.

Try this it will work.

frappe.ui.form.on("Sales Order", "fill_items", function(frm,cdt, cdn) {
	frappe.call({
		method: "method_path",
		args: {args},
		callback: function(r) {
			$.each(cur_frm.doc.items || [], function(i, item) {
				item.custom_code_ref = r.message
			});
			refresh_field("items");
		}
	})
});
3 Likes

@Sangram
Yep, it worked for me perfectly.
Thanks a lot for your help dear.

Regards
Ruchin Sharma