How to pre populate the fields data when we click on the create a new document?

In short,
How to pre populate some fields when you click on the link option which is “create a new document”??

See this image and i want that how I can target the option in the link field which is create a new document ( here document is basically depends on the Doctype).

I want that when I click on this option, some of fields must be pre populated in that document.

I know one thing which is frappe.new_doc and we can write in this in the object form.
But how can I run that function when I click on that option.

How i can target that option in that link field which can help me to pre populate the fields in that document.

@ajaymahiwal check this Creation by link autofill field in new doc

But where is solution ??? Can you please tell or @NCP

You have to create a button and then apply the logic, i think, directly not possible, that for you have to check the core functionality of “Create a new”.

This is possible by using get_route_options_for_new_doc

As per latest stable Frappe v15.45.0 this works as expected: It opens the forms for creating new docs of Land and Animal and then also pre-fills the fields ‘farmer_id’ and ‘producer’ in those respective forms from the “Producer” doctype.

frappe.ui.form.on("Producer", {
    add_land: function (frm) {
        frappe.new_doc('Land', {}, doc => {
            doc.farmer_id = frm.doc.name;
        });
    },
    add_animal: function (frm) {
        frappe.new_doc('Animal', {}, doc => {
            doc.producer = frm.doc.name;
        });
    },
    refresh(frm) {},
});

Explanation

  1. frappe.new_doc with Callback:
  • The third argument is a callback function that provides the newly created document (doc).
  • You can modify the doc object directly to set field values like producer.
  1. Setting Field Values:
  • doc.producer = frm.doc.producer; sets the producer field in the new document to the value from the current form.
  1. Simpler and Cleaner:
  • This method avoids using frappe.route_options and directly modifies the document object, ensuring that fields are correctly pre-filled.