📱

Get Our Mobile App

Take your business learning on the go!

Download on the App StoreGet it on Google Play

Fetch Data From One DocType To Another | Frappe | ERPNext

ERPNext Solutions with Nihantra15:19

Transcription

Hello Everyone! Welcome to the community talks with Nihantra! Today, we will learn how to Fetch data from one doctype to another doctype. I have seen a lots of users having this issue in the Frappe Forum. So, let's explore it and learn about it.

So, go to the doctype list; here you can see that I already created a child data that name is the invoice item. Here I created, here I created a field because we have to get the sales invoice data; also created a doctype whose name is invoice data. Here you can see that I have added an invoice item that we have seen recently in this table.

First, we will learn to fetch the data with the default features. So, we have to create a field; so set the label name like sales invoice ID, set the option sales invoice. Now, we have to fetch the customer from the sales invoice ID, but the sales invoice ID does not… so uh, let's set the option customer here. You can see sales invoice ID does not show in the "fetch form". So, first you have to save the doctype because sales invoice ID is not created. Now, again check the "fetch from" list; here you can see the sales invoice ID is appear, so select it and select the customer and save it. Also, we have to create the invoice date; invoice date we will get the date from the sales invoice ID just like the customer got date and save the doctype. Also, we have to "fetch from" is set for the company; select company and save the doctype. Open the invoice data doctype; reload the page. Let's select the sales invoice ID; so you can see the details coming properly, but you can't get item details using the "fetch from" feature. So, let's create the client script; we have to get sales invoice details; so select the doctype like invoice data and enable the script.

Sometimes users say how to get master and parents level transaction data at parents doctype and child doctype. So, first we have to remove the "fetch from" condition from the customer, invoice date, and the company; save the doctype. Go to the invoice data and test the data is coming or not. So, it's not coming. Now, we get the customer, invoice date, and Company from the sales invoice using the client script. So, we use frappe.db.get_value; the sales invoice; we have to also add the filter; we have to frm.doc.sales_invoice_id; we have to get the value from the sales invoice; so set the invoice field name that we want; customer; open the sales invoice to check the field name; copy the posting date; also we have to add the company function of value. Now we have to use the frm.set_value; customer value.customer; copy the line; paste it; invoice date value.posting_date; company value.company; set the event; reload the page and check the output. Select the ID; so here you can see the customer, invoice date, and Company is coming properly according the ID, but again child table data is not coming; we cannot set the child table data using the frappe.db.get_value.

So, now we have to use the frappe.call, but we have to first define the condition; if frm.doc.sales_invoice_id; frappe.call; now we have to enter the method like frappe.client.get; set the args; use doctype sales invoice filter because we have to set the filter because we want to get the invoice detail according to the sales invoice ID; set the callback; we have to apply the condition like if r.message; now added a console log for the testing; reload the page and check the output; open the console; select the ID; so you can see the whole sales invoice details coming in properly. Now set the parents value; frm.set_value r.message.customer; copy this line; paste it; invoice date r.message.posting_date; company r.message.company; let's reload the page and check the value; so value is coming properly; now we get the sales invoice table detail and set them in the invoice item table. So, we have to set the "For Loop"; r.message.items.forEach of function item; now we have to add the data in child a to use the frm.add_child; so copy the field name; now child.item_code is equal to; also copy the field name from the sales invoice item table item; let's copy that; paste it; now we have to add the refresh field; copy the field name; paste it and save it; reload the page and select the ID; so here you can see the value added successfully in the child table. Let's select the another one, so it's coming properly; but problem is that as many times as you select any invoice ID, it adding value; so delete all Row from the child table. So, we have to use something like frm.clear_table; copy the table name invoice item; let's configure the another field in code; just copy the line and past it; qty qty uom uom rate rate; we also we have to add more one row for the amount; let's reload the page and check the final output. Select the ID; so here you can see the all value where successfully added in the child table. Here you can see invoice according item is coming properly. Let's select another one; let's open the sales invoice ID for the cross testing; it's coming properly.

Thanks for the watching! If you enjoy the video please like 👍, share 🔗 and subscribe 🔔. Don't forget to leave a comment 💬 below. Thank You 🙏