Help! Cur_frm.add_fetch not working for default field loaded

I wants to have expiry_date(Date type field) of Batch to be fetch to Sales Invoice’s field expiry_date(Date type field)
I added below script in Sales Invoice but not getting anything:

cur_frm.add_fetch(“batch_no”, “expiry_date”, “expiry_date”);

Please help!

Hi @mdwala

Confirm all your fields are correct and try cur_frm.add_fetch('batch_no', 'expiry_date', 'expiry_date')

Kind regards,

@wale
Thanks…

Came to know that my code is perfect only but main issue is:
By selecting Item, (Automatically) batch id is selecting because default warehouse is set, but now expiry is not coming (automatic) based on that. To get expiry date I have to remove batch id and then re-add, then only expiry date comes.

Probably I need to use

frappe.ui.form.on([DocType], {
[trigger]: function(frm) {
[function];
}
});

I will try it tomorrow.

Please suggest if I am wrong.

1 Like

That should work

Cheers!

cur_frm.add_fetch doesn’t work on child table.

Use
frappe.ui.form.on(“Sales Invoice Item”, “batch_no”, function (frm, cdt, cdn) {
var d = locals[cdt][cdn];

    	frappe.db.get_value("batch", {"name": d.batch_no}, "expiry_date", function(value) {
    		d.expiry_date = value.expiry_date;
    	})
    });

Hi. @jof2jc, code you provided giving “SyntaxError: Invalid or unexpected token” error.

Also @jof2jc @wale

Now I am much confused between 3 ways to fetch value!

Please explain which method should use,
Why and
What is the difference
.

(Searched everywhere but couldn’t find answer)

  1. New field "expiry_date as “Read Only” and in “Option” write “batch_no.expiry_date”

  1. cur_frm.add_fetch(“batch_no”, “expiry_date”, “expiry_date”); Which Is working but has to edit batch no field to fetch Expiry date
    A) You said:

cur_frm.add_fetch doesn’t work on child table.

B) Rushabh Mehta Said add_fetch does work for child tables!

C) He also said add_fetch won’t work with table type fields.


  1. Code you provided:

frappe.ui.form.on(“Sales Invoice Item”, “batch_no”, function (frm, cdt, cdn) {
var d = locals[cdt][cdn];

	frappe.db.get_value("batch", {"name": d.batch_no}, "expiry_date", function(value) {
		d.expiry_date = value.expiry_date;
	})
});

Thanks for support.

frappe.ui.form.on(“Sales Invoice Item”, “batch_no”, function (frm, cdt, cdn) {
var d = locals[cdt][cdn];

    	frappe.db.get_value("batch", {"name": d.batch_no}, "expiry_date", function(value) {
    		d.expiry_date = value.expiry_date;
    	})
});

Make sure to put it under custom script doctype Sales Invoice

remove this

Yes it is in custom script of Sales Invoice.

No there is nothing in option, I was just asking good explanation for difference between those.

Code give this error

SyntaxError: Invalid or unexpected token
at Class.setup (http://192.168.0.140:8001/assets/js/form.min.js?ver=1507184322.42:2616:18)
at _f.Frm.setup (http://192.168.0.140:8001/assets/js/form.min.js?ver=1507184322.42:172:22)
at _f.Frm.refresh (http://192.168.0.140:8001/assets/js/form.min.js?ver=1507184322.42:441:9)
at Class.load (http://192.168.0.140:8001/assets/js/form.min.js?ver=1507184322.42:87:33)
at http://192.168.0.140:8001/assets/js/form.min.js?ver=1507184322.42:73:10
at Object.callback (http://192.168.0.140:8001/assets/js/desk.min.js?ver=1507184322.42:5497:6)
at Object.callback [as success_callback] (http://192.168.0.140:8001/assets/js/desk.min.js?ver=1507184322.42:1354:16)
at _ (http://192.168.0.140:8001/assets/js/desk.min.js?ver=1507184322.42:1378:34)
at Object. (http://192.168.0.140:8001/assets/js/desk.min.js?ver=1507184322.42:1478:5)
at i (http://192.168.0.140:8001/assets/frappe/js/lib/jquery/jquery.min.js:2:27151)

In short I require code to fetch Expiry Date in to Sales Invoice Item of selected batch_no.

Issue is default warehouse is selected for all items,
Thus adding item code fetch warehouse automatically, and batch id automatically.

And because of that Expiry date doesn’t fetch by using cur_frm.add_fetch in custom script

But it fetches when remove batch id and re-insert.

sorry wrong double quote…

frappe.ui.form.on("Sales Invoice Item", "batch_no", function(frm, cdt, cdn) {
	var d = locals[cdt][cdn];

    	frappe.db.get_value("Batch", {"name": d.batch_no}, "expiry_date", function(value) {
    		d.expiry_date = value.expiry_date;
    	});
});
2 Likes

Hi Thanks again for answering,

your code works but please see gif for my requirement.
Expiry Fetch

How do you did with this problem?