Set value in barcode text field

Hi,

How to set value in barcode text field (not on image field.)
i tried frm.set_value function but its setting value on barcode image field,
i am trying to fetch value of barcode from item master in custom child table.

This code may help you:

var selected_row = frm.get_field("CHILD_TABLE_NAME").grid.grid_rows_by_docname["NAME_OF_THE_ROW_DOC"];
selected_row.get_field("BARCODE_FIELD_NAME").set_value("VALUE")

The NAME_OF_THE_ROW_DOC is generally obtained in the child table events and it’s called cdn.
If you don’t have the name of the row, you can use the index:

var selected_row = frm.get_field("CHILD_TABLE_NAME").grid.grid_rows[INDEX_OF_ROW];
selected_row.get_field("BARCODE_FIELD_NAME").set_value("VALUE")
1 Like

@Nahuel_Nso thanks for your time buddy,

i tried

var selected_row = frm.get_field("barcode_items").grid.grid_rows[child.idx];
selected_row.get_field("barcode").set_value(r.barcode) 

Error in console:

fieldname barcode not found

but i’ve barcode field in my child table.

Child table

Parent Table

This is a known problem, and I don’t remember if i have or not a code solution. But the fastest and easiest solution is to add the barcode field to the list view (check inside the field). The problem is that the field “does not exists” if the inner form is not open.

@Nahuel_Nso i already have barcode field in list view, but still is not working while changing item from outside and inside of child table.

Can you check if the code is executed with a console.log?
This code works for inner form i think, but it only works if the form was previously opened:

selected_row.grid_form.fields_dict.FIELD_NAME.set_value("VALUE")

Tried but got below error.

TypeError: Cannot read property 'fields_dict' of undefined

Yes, that’s because the inner form wasn’t open. But what i mean was: Try the previous code, with a console log. The code with the child.idx that you post. Before and after that line put console.log("asd") and see if it is being executed.

Only first one is executing.

var selected_row = frm.get_field(“barcode_items”).grid.grid_rows[child.idx];
console.log(“asd”)
selected_row.get_field(“barcode”).set_value(r.barcode)
console.log(“asd 2”)

Try this, a “step by step” set_value:

selected_row.doc.FIELD_NAME = "VALUE";
selected_row.refresh();

I think that if you add this line before use the .set_value it works fine too:

selected_row.activate()

@Nahuel_Nso thanks but unfortunately none of the above worked, :frowning:

Does r.barcode have a valid value? Because i have this code working… Maybe you can provide the full code? Or do a console.log(r.barcode) console.log(child.idx) etc

r.barcode have value, i also tried with hardcoded value,

can you share your working full script? with doctype screenshots?

frappe.ui.form.on('Barcode Items', {
        item: function(frm,cdt, cdn){
            fun(frm, cdt, cdn);
        }
    });

var fun = function(frm, cdt, cdn) {
    var child = locals[cdt][cdn];
	if(child.item){
	frappe.call({
        "method": "frappe.client.get",
        "args": {
             "doctype": "Item",
             "name": child.item
        },
        "callback": function(response) {
             var r = response.message;
             if (r) {
                  var selected_row = frm.get_field("barcode_items").grid.grid_rows[child.idx];
		selected_row.activate()
		selected_row.get_field("barcode").set_value("123")
		selected_row.refresh();
             } else {
		frappe.msgprint("No barcode found.");
            }
        }
    });
}
}

This code works just fine, I did some modifications anyways.

frappe.ui.form.on('Barcode Items', {
        item: function(frm, cdt, cdn){
            fun(frm, cdt, cdn);
        }
    });

var fun = function(frm, cdt, cdn) {
    var selected_row = frm.get_field("barcode_items").grid.grid_rows_by_docname[cdn];
	if(selected_row.doc.item){
    	frappe.call({
            "method": "frappe.client.get",
            "args": {
                 "doctype": "Item",
                 "name": selected_row.doc.item
            },
            "callback": function(response) {
                 var r = response.message;
                 if (r) {
                	  selected_row.activate()
                	  selected_row.get_field("barcode").set_value("123")
                	  selected_row.refresh();
                } else {
                	  frappe.msgprint("No barcode found.");
                }
            }
        });
    }
}

But… For fetching straight forward you can use the Fetch From field in the field definition and set item.barcode.

Its woking but its setting value in barcode image field, i want to set value in barcode text field.

i am using v10, and it doesn’t have fetch from field.

I think that you can use Options field in the same way, just put item.barcode in the options field. Anyways, for the image/text part of the problem, I never used a Barcode field, so I don’t know.