Custom Script to create new batches for line items

The following script creates a new batch for each line item in a PO.

frappe.ui.form.on("Purchase Order", { //Creates new batches for line items.
    validate: function(frm, cdt, cdn) {
        var d = locals[cdt][cdn];
        var ic = [];
        frm.doc.items.forEach(function(d) {
            ic.push(d.item_code);
        });
        if (frm.doc.workflow_state == "Submitted") {
            for (var i = 0; i < ic.length; i++) {
                doc = {
                    "doctype": "Batch",
                    "batch_id": (ic[i] + frm.doc.name),
                    "item": ic[i],
                    "description": "created from" + frm.doc.name
                };
                frappe.call({
                    "method": "frappe.client.insert",
                    "args": {
                        "doc": doc
                    }
                });
            }
        }
    }
});

Can anyone tell me how to set the batch number in a field in the line item? Im fairly certain I have to use a callback, but Im not 100% sure how that works.

1 Like

@cpurbaugh In callback try,

frappe.model.set_value(cdt, cdn, fieldname, value);