Adding Items in sales order

I have a use case, where I need to add Items in Sales Order Items table based on some calculations either using client side or server side scripting.
I am able to write code but, when I am triggering the code

  1. At on submit, It is saying record already submitted
    2.At Validate, It is not updating the items in the sales order items table.

Thanks in advance


Would you mind sharing your custom script please?

Perhaps on save, it’s doc status is getting updated as 1. That might be the reason for the first issue “Record already submitted”.

Here is the code wriiten server side, here iam adding items to sales order items table. I tried trigger the code at both on submit and before submit
sales_order_obj = frappe.get_doc(“Sales Order”, name)
for each in free_goods:

        print "in free items merging_____________________________"
        free_item = each.get("free_item")
        print free_item, "_____________________________"
        # frappe.get_doc({})
        if free_item:
            item_obj = frappe.get_doc("Item", free_item)
            record = frappe.get_doc({
                "doctype": "Sales Order Item",
                "item_code": free_item,
                "qty": each.get("wholesale_eligible_quantity"),
                "parent": name,
                "parenttype": each.get("parenttype"),
                "item_name": item_obj.get("item_name"),
                "description": item_obj.get("description"),
            wholesale_eligible_quantity = each.get("wholesale_eligible_quantity")
            # if wholesale_eligible_quantity>0:
            #     record = {
            #         "doctype": "Sales Order Item",
            #         "item_code": free_item,
            #         "qty": wholesale_eligible_quantity,
            #         "parent": name,
            #         "parenttype": each.get("parenttype"),
            #         "item_name": item_obj.get("item_name"),
            #         "description": item_obj.get("description"),
            #     }
            print record, "+++++++++++++++++++++++++"

            print sales_order_obj.docstatus, "_________________________"
            print "here1"
            sales_order_obj.append("items", record)
            print "here2"

            # sales_order_obj.update(record)
            # sales_order_obj.submit()
            print   "here3"
            print sales_order_obj.docstatus, "_________________________"
            print "here4"

@rohit_w @kolate_sambhaji can you please review once?

You need to call code in validate or before_submit event.

Please refer following code.

Add child table row in Python Code:

//add child table row using JavaScript Code
$.each(r.message, function(i, d) {
                           var row = frappe.model.add_child(cur_frm.doc, "Sales Order Item", "items");
                           row.item_code = d.item_code;
                           row.item_name = d.item_name;
                           row.item_type = d.item_type;
                           row.parent_item =;
                           row.rate = 0;