Populate Custom Field in Item to Delivery Note Item?

Hi,

How can I populate the data from a custom field in Item to the same custom field in the DeliveryNoteItem?

I tried making a custom script for the DeliveryNoteItem with the following line:

cur_frm.add_fetch(“item_code”,“custom_field”,“custom_field”);

But when I select an item in the DeliveryNote Form, the custom field in the DeliveryNoteItem Form does not get updated.

Btw, when we click Add New Row in the DeliveryNote Form, we get DeliveryNoteItem Form, right?

Hi @nanto_himawan,

You can write the custom script on Item Code field’s trigger to fetch the field value from Item then assign the value to new Delivery Note Item field.

e.g.

frappe.ui.form.on("Delivery Note Item", "item_code", function(frm, cdt, cdn){
    item = locals[cdt][cdn]
    frappe.call({
    	method: "frappe.client.get_value",
    	args: {
    		doctype: "Item",
    		filters: item.item_code,
    		fieldname: "your_fieldname"
    	},
    	callback: function(r){
    		if(r.message){
    			item.temp = r.message.your_fieldname || "";
    			frm.refresh_fields()
    		}
    	}
    });
});

Yes

Thanks, Makarand

1 Like

Hi Makarand,

Same thing, the custom field doesn’t get updated.

I think the Form that opens on clicking Add New Row is not Delivery Note Item Form. I tested using this only one line:

msgprint(“Test - Delivery Note Item Form”);

in the Delivery Note Item Custom Script, and I don’t get any dialog when I click on Add New Row.

Hi @nanto_himawan,

Can you please share your code ?

Hi @makarand_b,

Currently, for the Delivery Note Item custom script, I have only one line:

msgprint("Test - Delivery Note Item");

I just want to make sure that the script is ever executed. But it seems that the script is never executed. When I click Add New Row in the Delivery Note, nothing happens. It seems that the script is never executed. So I suspect that the form that open when we Click Add New Row is not Delivery Note Item form.

-nanto

you have to put event trigger for the code to be executed, therefore …

frappe.ui.form.on(“Delivery Note Item”, “item_code”, function(frm, cdt, cdn){
msgprint(“test”);

// other codes…
});

it will trigger item_code change

Hi @jof2jc,

Thank you, but still nothing happens. I think the one line code should work if the form is really a Delivery Note Item. I tried the same exact code in the Delivery Note Form. A dialog pops up as soon as I open a Delivery Note.

Thanx,
-nanto

Just make sure you put the script under Delivery Note doctype, not Delivery Note Item…

Perfect. I originally put the script under Delivery Note Item DocType. It works after moving it to Delivery Note DocType.

Thanx,
-nanto

When we use:

item.item_code gives the newly selected item_code. But item.rate is still the previously selected item_rate. How do I get the newly selected item_rate?

This seems like before_update event. Is there anything like after_update event that I can use?

Thanx,
-nanto

Hi,

use cur_frm.refresh_fields() then you will be fetch newly selected item_rate.

Thanks, Makarand

Hi @makarand_b,

No, it doesn’t work. Here’s my test code:

frappe.ui.form.on("Delivery Note Item", "item_code", function(frm, cdt, cdn){
    item = locals[cdt][cdn]

    msgprint(item.item_code);
    msgprint(item.price_list_rate);
    cur_frm.refresh_fields();
    cur_frm.cscript.recalculate();
    msgprint(item.price_list_rate);

});

After the 2 refresh lines, I am still getting price_list_rate of the previously selected item.

-nanto

Hi @nanto_himawan,

item.price_list_rate does not change on cur_frm.cscript.recalculate() method.
try item.rate field instead. You can find the field names in Customize Form

  • Go To Customize Form
  • Select the doctype in `Enter the Form Type* e.g. Delivery Note Item

Thanks, Makarand

Hi @makarand_b,

Thank you, but still doesn’t work.

I think price_list_rate should change because I am changing the item selection say from A to B. So I want to get price_list_rate_B, instead of price_list_rate_A. What I am getting now is price_list_rate_A with item_code_B, even after the refresh and recalculate.

-nanto

cur_frm.refresh_fields() should be called after recalculate also keep the msgprint() after refresh_fields(). Then only it will show the latest values for all the fields.

Hi @makarand_b,

Here’s the latest test code:

frappe.ui.form.on("Delivery Note Item", "item_code", function(frm, cdt, cdn){
    item = locals[cdt][cdn]

    msgprint(item.item_code);
    msgprint(item.rate);
    cur_frm.cscript.recalculate();
    cur_frm.refresh_fields();
    msgprint(item.rate);

}); 

Still doesn’t work. I am printing item.rate before and after refresh_fields().

Hi, Can you share the working code. I am also trying to populate custom field in Delivery Note Item from Custom Field in Item master.

Also, when I create a custom field in Delivery Note Item what are the things I need to check to enable this link up with Item master.

provide screenshots to understand in deap scenario.

Let us say you have a field in Item Doctype custom_field and you want to fetch this in Delivery Note Item.
In Delivery Note Item make a field with the same name custom_field, this does the trick most of the times, if the field names are same, ERPNext will fetch the value. If this is not working for some reason, go to Customize Form for Delivery Note Item, in Fetch from add item_code.custom_field and Save
This will fetch the value from Item Master to Delivery Note Item

Hope this helps

1 Like