Fetch values from a linked document ​

When a user selects a document in a Link field, ERPNext can copy values from that document into other fields of the form. For example, when a user selects a Terms and Conditions template, ERPNext can show its text.

You set this with the Fetch From property. No script is needed.

Prerequisites ​

  • The System Manager role.
  • A Link field on the form. Read Add a link field.
  • The field name of the value in the linked DocType. Open Customize Form for the linked DocType, and read the Name column. For example, the text of a Terms and Conditions template is in terms.

Add the fetched field ​

This example adds terms and conditions to a custom form.

  1. Open Customize Form for the DocType.
  2. Add a Link field:
    • Label: Terms Template
    • Options: Terms and Conditions
    • Note its field name, for example custom_terms_template.
  3. Below it, add a field for the value:
    • Label: Terms
    • Field type: Text Editor, or the type of the source field.
    • Fetch From: custom_terms_template.terms, which is the link field name, a dot, and the source field name.
  4. Select Update, and reload the page.

When a user selects a template in Terms Template, ERPNext fills in Terms.

Keep a value that the user entered ​

By default, ERPNext overwrites the fetched field each time the link changes. To keep a value that the user entered, select Fetch on Save if Empty. ERPNext then fetches the value on save, and only if the field is empty.

Fetch with a client script ​

Before the Fetch From property existed, client scripts used frm.add_fetch. It still works:

js
frappe.ui.form.on("Sales Invoice", {
	setup(frm) {
		// link field, field in the linked document, field in this form
		frm.add_fetch("customer", "vat_id", "vat_id");
	},
});

Use Fetch From when you can. It needs no script.