Home Featured ERPNext / Frappe Filter and Auto-populate Child Table

ERPNext / Frappe Filter and Auto-populate Child Table

by karani

Auto-populating a child table in Frappe and/or ERPNext can make your application look good and give your users a better experience with the software. In this piece, I will demonstrate the following:

  • Adding a Section to a DocType
  • Adding a Child Table to a DocType
  • Writing a Custom Script to Query the backend
  • Executing a Frappe Call to grab the data
  • Writing an API to provide the data needed
  • Auto-populating the data to a child table
  • Refreshing a Frappe field to automatically effect the changes

On this post I will provide the two pieces of code needed to finish the work:

Custom Script Code

We will to use a Custom Script to send the request to our custom endpoint and also to handle the response.

frappe.ui.form.on('Library Member', {

  author: function(frm){

  let author = frm.doc.author;

   if(author){
    frappe.call({
     method: "library_management.api.get_author_articles",
     args: {author: author}
    }).done((r) => {

   frm.doc.my_article = []

   $.each(r.message, function(_i, e){
   let entry = frm.add_child("my_article");
   entry.article_name = e.name;
 })
 refresh_field("my_article")

  })
 }

}

})

API Code

We will write our Custom API endpoint which will be responsible for querying the data we need to return to the frontend.

@frappe.whitelist()
def get_author_articles(author):
articles = frappe.db.sql(f""" SELECT name FROM `tabArticle Library` WHERE article_author='{author}' """, as_dict=True)
return articles

Here is a YouTube video for the whole process.

You may also like

Leave a Comment