Error on Purchase Order

Hi,
I am encountering a strange error on Purchase Order.

This happens when I open up an old record in Quotaion,then i close it, then i try to add a new record on the Purchase Order form, everthing is fine until i try to add a new row of my custom table, then a pop-up msg " field XXX not found", XXX is a custom field on Quotation.

But this does not happen if i working on Purchase Order form directly on a freshed page without open the old record in Quotation before this.

I have do a little experiment, I added field XXX in Purchase Order form, same error occurs but the pop-up msg changed to " field YYY not found", which YYY is another custom field on Quotation form.

For Further inspection, I have found that both custom field XXX and YYY has custom script in Quotation and both are taking data from the custom table which i tried to add new row on Purchase Order form by using " frm.{child_table_name}.forEach" function.

I am taking a guess,
When i open up the Quotation form, the broswer loaded the Quotation custom scripts, but when i closed the Quotation form and open up the Purchase Order form, the Quotation custom scripts still remained at browser and it is keep running, so the scripts try to set a field value even it is in Purchase Order form, so it informed me that this is not a such field?

looks to me as if you need to investigate your custom fields/scripts involved at first

can you share the script ?

it should be frm.doc.{child_table_name}.forEach

Thanks, Makarand

@vrms & @makarand_b, Thanks for the reply.

Below is my script:

// Calculate total row of items, then set print format
frappe.ui.form.on("Epicz Item", {
item_code: function(frm, cdt, cdn) {
var d = locals[cdt][cdn];
var pbcount =0;
var tolrow = 0;
frm.doc.epicz_item2.forEach(function(d) {
  tolrow +=1;
  if (pbcount == 2) { d.page_break = 1;
                      pbcount =0; 
                      refresh_field("epicz_item2");
                     }
   pbcount += 1; });
frm.set_value("print_row", tolrow);
  }
})

“Epicz Item” is child table;
“epicz_item2” is the field name of the child table;
“print_row” is the name of the custom field;
“page_brake” is a field in the child table.

This custom script is used to count the number of rows of the custom table and the apply suitable print format.

@Terry

Try this

frappe.ui.form.on("Epicz Item", 
item_code: function(frm, cdt, cdn) {
var d = locals[cdt][cdn];
var pbcount =0;
var tolrow = 0;
frm.doc.epicz_item2.forEach(function(d) {
  tolrow +=1;
  if (pbcount == 2) { d.page_break = 1;
                      pbcount =0; 
                      refresh_field("epicz_item2");
                     }
   pbcount += 1; });
frm.set_value("print_row", tolrow);
  });

@Nick

Actually, the code works fine, it functions well for my purpose on Quotation form, but the problem is that it also working at Purchase Order form even tho I haven’t put this script in Purchase Order.
So i think the problem switch to how can we stop running the scripts from other froms without reload the page as it opened once

Anyway, what is the difference between the code you edited and mine?