How to Map Custom Fields in Child Tables

Hi ,

I added a custom link field in the Sales Order Items table and added same field (with same name) in the Sales Invoice Items table. However, when I create a Sales Invoice from Sales Order, the field doesn’t get mapped! I thought this should happen automatically?

How can I achieve this mapping via custom script? I’ve tried some of the suggestions I found on the forum but they don’t seem to work

Any assistance will be much appreciated

Thanks

share what you have tried so far,

Is field name same in both Sales Order Item and Sales Invoice Item ?

Hi @neerajvkn

Pls find below:

frappe.ui.form.on("Sales Invoice", {
    "onload": function(frm) {
        frappe.model.with_doc("Sales Order", frm.doc.onload, function() {
            var tabletransfer= frappe.model.get_doc("Sales Order", frm.doc.onload)
            $.each(tabletransfer.items, function(index, row){
                var d = frm.add_child("items");
                d.business_unit = row.business_unit;
                frm.refresh_field("items");
            });
        });
    }
});

Hi @sanjay

Yes. This is shown in code above

Thanks

have you tried using a custom trigger, say like a custom button to trigger the code and see if the code actually works as intended? from seeing the code, it should have worked, maybe its not getting triggered the right way,

& i think on line 3, its with_doctype , not just with_doc, and frm,doc.onload, not needed i think,

similar fetching items from one table to sales invoice , using with_doctype, and no trigger there, this can be defenitely optimised and cut short to fit your needs

relevenat parts of the code is below

var get_doc = function(mydocname){
	var sinv;
	return new Promise(function(resolve) {
		frappe.call({
			"method": "frappe.client.get",
			"args": {
				"doctype": "My custom Doc",
				"name": mydocname
			},
			"callback": function(response) {
				sinv = response.message.items;   
				resolve(sinv);
			}
		});
	});
} 

    cur_frm.add_custom_button(__(button_name_var), function(frm) {
        get_doc(cur_frm.docname).then(
            function(result) { 
                frappe.model.with_doctype('Sales Invoice', function() {
                    var si = frappe.model.get_new_doc('Sales Invoice');
                    si.customer= cur_frm.doc.customer;
                    result.forEach(function(item) {
                        var si_item = frappe.model.add_child(si, 'items');
                        si_item.item_code = item.item_code;
                    });
                    frappe.set_route('Form', 'Sales Invoice', si.name);
                });
            }
        );
    });

Thanks @neerajvkn

Will try to adapt this for my use case

Kind regards,

Interestingly, when I try creating other fields with the same name on both docs, they get mapped! The only difference that comes to mind right now is that the field not getting populated is an accounting dimension so it was automatically created… I’m not sure if this could have anything to do with the issue

Kind regards

I can confirm now that the issue is caused by enabling Accounting Dimension. Is there a reason for this behaviour or is it a bug??