I have custom fields in the Purchase Receipt Item that I want to be entered in to the associated Batch when it is created. I tried to do this by giving the DocTypes fields in each the same name and type but the information is not transferred from PR Item to Batch.
I have two custom fields in the Purchase Receipt Item, not the Purchase Receipt DocType.
manufacturers_lot_no, a Data field
manufacturers_expiry_date, a Date field
When the Purchase Receipt is Submitted and new Batch No assigned to theitem, how can this accompanying information be added to corresponding fields in the Batch No DocType?
You can use The fetch from feature
In batch doctype add a new 2 field , and in fetch from add this { field contains purchase receipt id in batch doctype } . { Field name in purchase receipt }
batch no manually created via batch no link field, ensure the referenced (to-be copied field filled before create batch no), please create the following client script
for item in doc.items:
if item.batch_no and item.manufacturer_part:
batch_doc = frappe.get_doc('Batch', item.batch_no)
if batch_doc.reference_doctype == doc.doctype and batch_doc.reference_name == doc.name:
batch_doc.manufacturer_part = item.manufacturer_part
batch_doc.save()
for both scripts, please adapt the field name accordingly.
Thanks @szufisher , that script works brilliantly for v14.
Unfortunately I’m using the develop branch and it doesn’t work with the new Serial and Batch Bundle approach (batch_no is no longer used). However, it does give me a good direction to work on
Been a while but I think I have working version to share that works on v15 with the Serial and Batch Bundle feature.
This is a server script for the Purchase Receipt, set to trigger on DocType Event After Submit
I have two custom fields setup in the Purchase Receipt Item:
custom_m_lot_number for the manufacturers’ lot/batch number
custom_m_expiry_date for the manufacturers’ expiry date
In turn I have two corresponding fields in the Batch DocType
custom_supplier_identifier to store the value for custom_m_lot_number
custom_supplier_expiry to store the value for custom_m_expiry_date
NB this assumes that you either completely accept the items on a line item ORcompletely reject them. It will not work, as is, for a Purchase Receipt Item that contains a quantity for both accepted and rejected fields. I use an additional Client Script to enforce this.
# Get Purchase Receipt Name
parent_receipt = doc.name
# Get the Purchase Receipt document
purchase_receipt = frappe.get_doc("Purchase Receipt", parent_receipt)
# Iterate through Purchase Receipt Item child table
for item in purchase_receipt.items:
# Get the custom_m, custom_d fields
custom_m = item.get("custom_m_lot_number")
custom_d = item.get("custom_m_expiry_date")
serial_and_batch_bundle = item.get("serial_and_batch_bundle")
# If serial_and_batch_bundle field is empty, find the SABB id from the rejected_serial_and_batch_bundle field instead
if serial_and_batch_bundle is None:
serial_and_batch_bundle = item.get("rejected_serial_and_batch_bundle")
# Search for the batch number using the serial_and_batch_bundle field
batch_number = frappe.db.get_value("Serial and Batch Entry",
{"parent": serial_and_batch_bundle},
"batch_no")
# Set custom field values in relevant Batch no
frappe.db.set_value('Batch', batch_number, {
'custom_supplier_identifier': custom_m,
'custom_supplier_expiry': custom_d
})