Syntax Error when Linking Doctypes Via Server Script

Hello,
I hope you’re well.
I am trying to achieve an automatic creation of delivery note from sales order upon submission.
I have written this script but in order to link the docs, I need to populate the ‘items_to_load’ against the sales order.
The Script


`items_to_load = [“against_sales_order”:doc.name, “sales_order_item”: doc.item]
for item in doc.items:
items_to_load.append({‘item_code’:item.item_code,‘rate’:item.rate, ‘qty’:item.qty, ‘uom’:item.uom})

#Create the Delivery Note
frappe.get_doc(dict(
doctype = ‘Delivery Note’,
title = doc.customer,
customer = doc.customer,
company = doc.company,
docstatus = 0,
items = items_to_load,
)).insert()

frappe.msgprint(msg=(f’Delivery Not for {doc.customer} has been created’),title=(‘Automation’), indicator=‘green’)`

Error I get upon Sales Order Submit

SyntaxError: ('Line 1: SyntaxError: invalid syntax at statement: \'items_to_load = ["against_sales_order":doc.name, "sales_order_item": doc.item]\'',)

I’d really appreciate some assistance.

hello, you may want to use {} with key value pairs like:
items_to_load = [{“against_sales_order”:doc.name, “sales_order_item”: doc.item}]

1 Like

Thank you for the response.
That solved the problem but now I’m facing another error unfortunately.

The script includes the quantity field so why is it showing this error message?

@Rashid_Salim delivery notes can’t have items with 0 quantity

Hi,
There is a quantity set on the item in the sales order. See below

I have also selected the source warehouse and still the issue persists.

Hi PyJumber,

I posted a secondary problem. Could you have any idea why it’s causing an error?

Thank you

Hello Rashid, can you post the whole server script code, i will try to look into

Here’s the code below:

items_to_load = [{“against_sales_order”:doc.name, “sales_order_item”:doc.items}]
for item in doc.items:
items_to_load.append({‘item_code’:item.item_code,‘rate’:item.rate,‘qty’:item.qty,‘uom’:item.uom})

#Create the Delivery Note
frappe.get_doc(dict(
doctype = ‘Delivery Note’,
title = doc.customer,
customer = doc.customer,
company = doc.company,
docstatus = 0,
items = items_to_load,
)).insert(ignore_permissions=True)

frappe.msgprint(msg=(f’Delivery Not for {doc.customer} has been created’),title=(‘Automation’), indicator=‘green’)