What is the best way to Auto Create a DocType entry in my Custom App?

Hello,

In my Custom App I have a DocType called Route Card. This DocType has two child tables in it. Here one child table is filled with data from Work Order’s Operations child table and another child table is empty at the time of creation.

What I want to do is when a user clicks on Start in Work Order I want to create the Route Card for current Work Order in background.

I don’t understand as to from where to start. Can someone guide me as to how I can auto create Route Card.

TIA

Yogi Yang

Hello YogiYang,

there are two solutions for creating documents in the background.

  1. Auto Repeat
  2. Using python
    doc = frappe.get_doc({
    “doctype”:“Route Card”,
    “reference_doctype”:“Work Order”,
    “reference_name”:doc.name,
    “item” :[
    {
    “item_code” :child.item_code,
    “item_name”: child.item_name
    }
    ]
    }).insert(ignore_permissions=True,ignore_mandatory=True)
    doc.save()
    Note: this is an example code
    you can run this code on any server events(like After Submit, After Insert) or on the client side using API(like on Button).
1 Like

Hello @Rehan_Ansari ,

The child in my DocType has multiple rows so how can I insert multiple rows here?

TIA

Yogi Yang

You can also do it by setting up the cron job.

You can pass multiple child and multiple rows in this way →

  1. multiple rows in one child table
“child-table-name” :[
#first row
{
“item_code” :child.item_code,
“item_name”: child.item_name
},
#second row
{
“item_code” :child.item_code,
“item_name”: child.item_name
}
#and so on
]
  1. multiple child table
#first child
“first-child-table-name” :[
{
“item_code” :child.item_code,
“item_name”: child.item_name
}
],
#second child
“second-child-table-name” :[
{
“item_code” :child.item_code,
“item_name”: child.item_name
}
]

I hope I answered your question correctly.?

1 Like

Hello @Rehan_Ansari,

But I have to read the data from the DocType My Work Order and insert them in the child of Route Card.

Will a loop work?

Here is the code that I am trying to build.

import frappe
from dateutil.relativedelta import relativedelta
from frappe import _
from frappe.model.document import Document
from frappe.model.mapper import get_mapped_doc
from frappe.model.document import Document

class MyWorkOrder(Document):
    def create_route_card(my_work_order, operations):
        if isinstance(operations, str):
            operations = json.loads(operations)

        my_work_order = frappe.get_doc("My Work Order", my_work_order)

        doc = frappe.get_doc({
            "date":"now",
            "my_work_order_num": my_work_order.name,
            "customer_code": my_work_order.customer_code,
            "item_to_mfg": my_work_order.production_item,
            "qty": my_work_order.qty,
            "operations":[{
                "operation": operations.operation,
                "workstation": operations.workstation,
                "source_warehouse": operations.source_warehouse,
                "target_warehouse": operations.target_warehouse,
                "perform_qc": operations.perform_qc,
                "time_in_mins": operations.time_in_mins,
                "quality_inspection_template": operations.quality_inspection_template
            }]
        }).insert(ignore_permissions=True,ignore_mandatory=True)
        # Save Document
        doc.save()

TIA

Yogi Yang

        my_work_order = frappe.get_doc("My Work Order", my_work_order)

        doc = frappe.get_doc({
            **mention doctype**
            "doctype":"Route Card"
            "date":"now",
            "my_work_order_num": my_work_order.name,
            "customer_code": my_work_order.customer_code,
            "item_to_mfg": my_work_order.production_item,
            "qty": my_work_order.qty,
            "operations":[{
                "operation": operations.operation,
                "workstation": operations.workstation,
                "source_warehouse": operations.source_warehouse,
                "target_warehouse": operations.target_warehouse,
                "perform_qc": operations.perform_qc,
                "time_in_mins": operations.time_in_mins,
                "quality_inspection_template": operations.quality_inspection_template
            }]
        }).insert(ignore_permissions=True,ignore_mandatory=True)
        # Save Document
        doc.save()

yes this will work, just mentioned the doctype.

I am assuming that you have to read data from Work Order and you are creating multiple Route Cards (Doctype).

Hello,

Here I want to repeat the following code but don’t understand as to how to insert a loop in between.

"operations":[{
                "operation": operations.operation,
                "workstation": operations.workstation,
                "source_warehouse": operations.source_warehouse,
                "target_warehouse": operations.target_warehouse,
                "perform_qc": operations.perform_qc,
                "time_in_mins": operations.time_in_mins,
                "quality_inspection_template": operations.quality_inspection_template
            }]

What I am trying to do is copy the data of Processes from Work Order to Route Card.

TIA

Yogi Yang

Hi YogiYang
use following server side script for your issue(insert multiple rows).
```
my_work_order = frappe.get_doc(“My Work Order”, my_work_order)
Rut_Card = frappe.new_doc(“Route Card”)
Rut_Card.date = “now”
Rut_Card.my_work_order_num = my_work_order.name,
etc…
for i in operations:
Rut_Card.append(“your_route_card_child_table_field_name”,{
“operation”: i.operation,
“workstation”: i.workstation,
“source_warehouse”: i.source_warehouse,
“target_warehouse”: i.target_warehouse,
“perform_qc”: i.perform_qc,
“time_in_mins”: i.time_in_mins,
“quality_inspection_template”: i.quality_inspection_template
})
Rut_Card.flags.ignore_permissions = 1
Rut_Card.save()
frappe.db.commit()

1 Like

@anil_pise,

Your code sample helped me a lot but the child items are getting inserted blank.

I am using this code.

def create_route_card(my_work_order):
    # if isinstance(operations, str):
    #     operations = json.loads(operations)

    my_work_order = frappe.get_doc("My Work Order", my_work_order)
    # Get the Processed from Work Order
    my_work_order_processes = frappe.get_list("My Work Order Operations", {"parent": my_work_order.name})
    
    my_route_card = frappe.new_doc("My Route Card")
    my_route_card.date = datetime.datetime.now()
    my_route_card.my_work_order_num = my_work_order.name
    my_route_card.customer_code = my_work_order.customer_code
    my_route_card.item_to_mfg = my_work_order.production_item
    my_route_card.qty = my_work_order.qty
    
    for spc in my_work_order_processes:
        my_route_card.append("operations",{
            "operation": spc.operation,
            "workstation": spc.workstation,
            "source_warehouse": spc.source_warehouse,
            "target_warehouse": spc.target_warehouse,
            "perform_qc": spc.perform_qc,
            "time_in_mins": spc.time_in_mins,
            "quality_inspection_template": spc.quality_inspection_template
        })
        my_route_card.flags.ignore_permissions = 1
        my_route_card.flags.ignore_mandatory = 1

        my_route_card.save()
        frappe.db.commit()

Is there any mistake in this code?

TIA

Yogi Yang