How to Set Variables in a Server Script?

Probably an embarrassingly easy question but…

How do you set variables in a server script?

The documentation examples all seem to have them preset, but how do they get there in the first place?

I’m looking to use the After Insert option, so that when a new batch is created the script triggers to add some additional information from the related Purchase Receipt.

However, if I try to use i.e. variable1 = doc.batch_id it never seems to work and I get the error that variable1 is not defined.

Can anyone please point me in the right direction?

Thanks,
Paul

Share your code…

Certainly

 if doc.doctype == "Batch" and doc.reference_doctype == "Purchase Receipt":
        batch_no = doc.batch_id
        serial_batch_entry = frappe.db.get_value("Serial and Batch Entry", {"batch_no": batch_no}, ["parent"])
        
        if serial_batch_entry:
            bundle_entry = frappe.db.get_value("Serial and Batch Bundle", {"name": serial_batch_entry}, ["voucher_detail"], as_dict=True)
            
            if bundle_entry and bundle_entry.get("voucher_detail"):
                purchase_receipt_item = frappe.db.get_value("Purchase Receipt Item", {"name": bundle_entry["voucher_detail"]}, ["custom_m_lot_number", "custom_m_expiry_date"], as_dict=True)
                
                if purchase_receipt_item:
                    doc.supplier_identifier = purchase_receipt_item["custom_m_lot_number"]
                    doc.supplier_expiry = purchase_receipt_item["custom_m_expiry_date"]
                    doc.save()

What I want it to do:
When a new Batch is inserted via a Purchase Receipt (the Purchase Receipt Item child table will have an entry in the serial_and_batch_bundle field), use the Batch DocType information to find the associated Purchase Receipt Item child table entry and transfer two custom fields ( custom_m_lot_number and custom_m_expiry_date) to custom fields in the Batch entry (supplier_identifier and supplier_expiry)

So how do I make sure this only applies to new entries? Is the After Insert options sufficient?