Custom script not working for onchange in Stock Entry Detail table

I am facing an issue where the Custom Script is not working for the onchange of the Barcode field in Stock Entry Detail table. The same script works in the Sales Order Item table but does not work here.

Following is the code for Sales Order Item: The code strips the first 16 digits of the scanned barcode.

frappe.ui.form.on(“Sales Order Item”, {
barcode: function(frm, cdt, cdn) {
var d = locals[cdt][cdn];
var str = d.barcode;
if(str>16){
var res = str.substring(0, 16);
d.barcode = res;
}
},
});

Following is the code for Stock Entry Detail:

frappe.ui.form.on(“Stock Entry Detail”, {
qty: function(frm, cdt, cdn) {
var d = locals[cdt][cdn];
if (d.barcode) {
frappe.call({
method: “myapp_erpnext.hooks_call.stock_entry.get_item_group”,
args: {“barcode”: d.barcode },
callback: function(r) {
if (!r.exe){
frappe.model.set_value(cdt, cdn, “item_group”, r.message);
}
}
});
}
},
barcode: function(frm, cdt, cdn) {
var d = locals[cdt][cdn];
var str = d.barcode;
if(str>16){
var res = str.substring(0, 16);
d.barcode = res;
}
},
});

Just do a double check, did u write it in Stock Entry js? Do u have any console error?

Hello @JoEz

We have the code written in the Custom Script for the Stock Entry doctype.

Here is the console error:
Traceback (most recent call last):
File “/home/frappe/frappe-bench/apps/frappe/frappe/app.py”, line 55, in application
response = frappe.handler.handle()
File “/home/frappe/frappe-bench/apps/frappe/frappe/handler.py”, line 21, in handle
data = execute_cmd(cmd)
File “/home/frappe/frappe-bench/apps/frappe/frappe/handler.py”, line 52, in execute_cmd
return frappe.call(method, **frappe.form_dict)
File “/home/frappe/frappe-bench/apps/frappe/frappe/init.py”, line 907, in call
return fn(*args, **newargs)
File “/home/frappe/frappe-bench/apps/erpnext/erpnext/stock/get_item_details.py”, line 121, in get_item_code
frappe.throw(_(“No Item with Barcode {0}”).format(barcode))
File “/home/frappe/frappe-bench/apps/frappe/frappe/init.py”, line 316, in throw
msgprint(msg, raise_exception=exc, title=title, indicator=‘red’)
File “/home/frappe/frappe-bench/apps/frappe/frappe/init.py”, line 306, in msgprint
_raise_exception()
File “/home/frappe/frappe-bench/apps/frappe/frappe/init.py”, line 279, in _raise_exception
raise raise_exception, encode(msg)
ValidationError: No Item with Barcode 01089032332114150000000000

The logic we have implemented is as follows:

  1. Item has a 26 digit barcode printed on it. (01089032332114150000000000)
  2. ERPNext has only the first 16 digits stored in the Barcode field of the item. (0108903233211415)
  3. When the barcode is scanned we want the first 16 digits and remove the rest so that it matches with the barcode stored for the Item in ERPNext and can fetch the item code automatically in the Stock Entry Detail table based on the barcode.

This code is working fine in Sales Order and there is no console error there but the same code in the Stock Entry Custom Script throws up this console error.

Our understanding is that the onchange should get called before the validate which seems to be happening in the case of Sales Order but in Stock Entry the validate is getting called before onchange.

Better hire a developer :grin: :wink: …have a look at ERPNext: Free and Open Source Cloud ERP Software

It’s not about hiring a developer. The question is that what works in one doctype does not work in another.
There could be an issue in the code inside the frappe framework which needs to be looked into and fixed.

If this is the exact code you have used there are some problems.

Can’t compare string to number

Should be !r.exc

This won’t change the form value. Use frm.set_value

as @netchampfaris pointed out, you’ve error in your code …it’s not a frappe bug, so it’s about hiring someone who have some more experience …

Thanks @netchampfaris for pointing out the errors in the code.
My bad that the code had some issues, but then I wonder why it was working in Sales Order

We have made the changes as suggested as follows:

barcode: function(frm, cdt, cdn) {
var d = locals[cdt][cdn];
var str = d.barcode;
if(str.length>16){
var res = str.substring(0, 16);
frappe.model.set_value(d.doctype,d.name,“barcode”,res)
}
},

Though now the barcode is getting truncated to 16 digits, we are getting an additional alert message which does not come up in the Sales Order.

Still unable to understand why this message is coming in the Stock Entry but not in Sales Order?

Rectified the error in the code, but I am wondering how the code was working in Sales Order :confused:

Try console.log (res) and check what’s the value …

Might be a problem with the Field Type.

Can you check the Sales Order Barcode Field and compare it with the Stock Entry Barcode field?

I think the Sales Order “barcode” field isn’t linked to the Barcode doctype but Stock Entry Detail’s barcode field is a linked field to Barcode. :slight_smile:

Both the fields seem to be the same @littlehera

It shows the truncated value of 16 digits (0108903233211415)

@keshav
OnChange event is not trigger on quick entry form. I think that was the reason that you can’t see console. Try this on full edit form. But if you want to call this event on quick entry form then check this link.