How to create a new doctype with data automatically coming from linked doctype

ERPnext have lot of linked doctypes for eg, we can create supplier quotation from RFQ, quotation from supplier quotation and etc, in which there is a button on top of it to create the document.

When we do that, the fields having the same name gets automatically filled. So I want to know how can I do this for my custom doctypes, in which I also can add a button for example at the top to create a new doctype from some doctype with same fields getting automatically populated.

Hi. Go to the “Linked Documents” section of the doctype. Basically, it’s about specifying the doctype and the fields you want to be linked.

More info and examples here:
https://frappeframework.com/docs/user/en/basics/doctypes/actions-and-links#connections-linked-documents-

@Abhiraj_Tulsyan try this :
Doctype is the first doctype name , New Doctype is the one you gonna create
in js write :

frappe.ui.form.on('Doctype', {
	refresh: function(frm) {
	    frm.add_custom_button(__('Create New Doctype'), function(){
	        frappe.model.open_mapped_doc({
	            method:"dotted.path.to.a.python.function",
	            frm:frm
	        })
    }, __("Create"));
	}
})

write a python file :

import frappe
@frappe.whitelist()
def function(source_name,target_doc=None):
    old_doctype=frappe.get_doc("Doctype",source_name)
    new_doctype=frappe.new_doc(New Doctype)
    #populate necessary fields of new_doctype here , example new_doctype.posting_date=...
    return new_doctype

I don’t think this is what I asked.

I’ll check on this for sure.

Can you help me here how to copy child table items here?

@Abhiraj_Tulsyan copy child table where ? from where ?

@bahaou
I have one doctype - RFQ and other doctype - JOB

Basically RFQ gets converted to JOB after all data is fed.

Now I want a button on top of RFQ which says, Create JOB which will open the JOB docform with data from RFQ doctype prepopulated. There are some child tables in RFQ which are also present in JOB so that also needs to be populated.

So I need to copy child table fields as well as normal table fields.

Also here can i see somewhere documentation of this method, frappe.model.open_mapped_doc()?

@Abhiraj_Tulsyan

import frappe
@frappe.whitelist()
def function(source_name,target_doc=None):
    rfq=frappe.get_doc("RFQ",source_name)
    job=frappe.new_doc("JOB")
    #to populate a normal field just use job.field=rfq.field_from_rfq
    #for a table do this 
    for i in rfq.table_examle:
        new_row=job.append("child_table_name_in_job",{})
        new_row.field=i.example_field
    # what I did is just eterate the first child table of rfq and accordingly add a new row in the job table and for each row you gonna populate it's own fields . 
    #if child tables are the same you can just populate the entire table . 
    return job

I don’t know about documentations . open_mapped_doc wil just redirect to a new doctype you built using python code above . which exactly what you need . and this is exactly how standard create buttons work in erpnext .

@bahaou

Okay thanks for it.
One last doubt

frappe.ui.form.on('Doctype', {
	refresh: function(frm) {
	    frm.add_custom_button(__('Create New Doctype'), function(){
	        frappe.model.open_mapped_doc({
	            method:"dotted.path.to.a.python.function",
	            frm:frm
	        })
    }, __("Create"));
	}
})

In this where is source_name to be passed to the whitelisted function?

@Abhiraj_Tulsyan you don’t . function already has frm as parameter . just give it a try and enjoy the magic .

@bahaou Okay I’ll try, but I am more scared to use things when I really don’t know how its happening from behind :slight_smile:

@Abhiraj_Tulsyan you will figure it out once you try it .

Hello @bahaou
This is great. I used it.

Now I want to know one more thing
Can I pass any custom arguments as well?

I actually have a child table through which I want to create a new document. I have a button in my child document so whenever I click on that button, that particular row I want to create a new document out of it of other doctype.

That custom argument will help me know which particular row was clicked so that is required.