Problem in creating serial and batch bundle while creating Stock entry via Server Script

While creating a Stock Entry via server script I’m getting this error.

  1. Stock Entry is being created Successfully.
  2. Serial and Batch Bundle for Outward is also created in draft.
  3. but when the code is going to create serial and batch bundle for Inward entry it is finding that the name it want to use is already used for another item’s outward serial and batch bundle.
    image


repairing_item_list = [repairing_item['name'] for repairing_item in repairing_items]

repairing_order_doc = frappe.get_list('Repair Order', filters={'name': ['in', repairing_item_list]}, fields=['name', 'workflow_state', 'repair_item', 'purchase_cost', 'repair_item_sr_no'])

company_doc = frappe.get_all('Company', ['name', 'abbr'])
company_name = company_doc[0].name
company_abbr = company_doc[0].abbr


stock_entry = {}
stock_entry['doctype'] = 'Stock Entry'
stock_entry['docstatus'] = 1
stock_entry['posting_time'] = frappe.utils.nowtime()
stock_entry['company'] = company_name
stock_entry['stock_entry_type'] = 'Material Transfer'
# stock_entry['from_warehouse'] = 'Store - ' + company_abbr

stock_entry_line_items = []

for repairing_order in repairing_order_doc:
    stock_entry_details = {}
    
    stock_entry_details['doctype'] = 'Stock Entry Detail'
    stock_entry_details['docstatus'] = 1
    
    s_warehouse_doc = frappe.get_doc('Warehouse', 'Stores - ' + company_abbr)
    stock_entry_details['s_warehouse'] = s_warehouse_doc.name
    
    if repairing_order['workflow_state'] == "QC Passed":
        t_warehouse_doc = frappe.get_doc('Warehouse', 'Finished Goods - ' + company_abbr)
        stock_entry_details['t_warehouse'] = t_warehouse_doc.name
    if repairing_order['workflow_state'] == "QC Failed":
        t_warehouse_doc = frappe.get_doc('Warehouse', 'Work In Progress - ' + company_abbr)
        stock_entry_details['t_warehouse'] = 'Work In Progress - ' + company_abbr # t_warehouse_doc.name
    if repairing_order['workflow_state'] == "Rejected":
        # Create A Warehouse Scrap for Rejected Items
        t_warehouse_doc = frappe.get_doc('Warehouse', 'Scrap - ' + company_abbr)
        stock_entry_details['t_warehouse'] = t_warehouse_doc.name
    print({"t_warehouse_doc": t_warehouse_doc.name})
        
    stock_entry_details['qty'] = 1
    stock_entry_details['basic_rate'] = repairing_order['purchase_cost']
    
    item_doc = frappe.get_doc('Item', repairing_order['repair_item'])
    stock_entry_details['item_code'] = repairing_order['repair_item']
    stock_entry_details['serial_no'] = repairing_order['repair_item_sr_no']
    stock_entry_details['uom'] = item_doc.stock_uom
    stock_entry_line_items.append(stock_entry_details)

stock_entry['items'] = stock_entry_line_items

doc = frappe.get_doc(stock_entry)

try:
    doc.save()
except Exception as e:
    print("Error saving stock entry:", e)

frappe.response.data = doc.as_dict()```

How can I solve this?

Hi @chnk8802,
Add below line into the stock entry details dict it will solve your issue

stock_entry_details[‘use_serial_batch_fields’] = 1

1 Like