Batch creation custom script not working

Could someone help me out with this? I’m getting no errors to point me in the right direction, but this script’s not working and it won’t allow the document to save.

This script was working in V7, but has stopped in V11

frappe.ui.form.on("Purchase Order", {
   validate: function(frm) {
   	frm.doc.items.forEach(function(d) {
   			doc = {
   				"doctype": "Batch",
   				"batch_id": (frm.doc.name + " (" + d.item_code + ")"),
   				"item": d.item_code
   			};
   			frappe.call({
   				"method": "frappe.client.insert",
   				"args": {
   					"doc": doc
   				}
   			});
   		});
   }
});

I use this code to create batch.

frappe.ui.form.on('Purchase Receipt Item', 'width', function(frm, cdt, cdn) {
    const batch = frappe.model.get_new_doc('Batch');
    batch.item = 'SAC';
    batch.width = 999;
    batch.batch_id = 'Insert3';
    frappe.db.insert(batch);

    frappe.model.set_value(cdt, cdn, "batch_no", batch.batch_id);
});