Custom Controllers

How do we create our controllers functions as in erpnext.controllers.queries…

I need to set a query in a child table (PO Item form). I’m doing this with a set_query function with the query to be set in a python function. In erpnext, this is achieved through the controllers module.

How do I achieve a similar controllers function for my custom app? As a dumb, I created controllers folder in my custom app, and added my query inside queries.py. When i try to call that through set_query function, I get the error 'App Controllers not instaled".

Here is my js code to set the query in Item Code field in Purchase Order Item form -

me.frm.set_query(“item_code”, “items”, function(doc, cdt, cdn) {
return { query: “myapp.controllers.queries.supplier_items_query” }
});

And this is the actual query -

def supplier_items_query(doctype, txt, searchfield, start, page_len, filters):
return frappe.db.sql(“”“select parent from tabItem Supplier
where supplier = ‘S1’
and docstatus < 2"”")

Any ideas how to achieve this in custom app? If I put this query under erpnext.controllers.queries file, it works fine. But I dont want to edit the erpnext file, and want to have this function in my custom app.

@kirthi
write your query method as above def supplier_item_query .
if you check purchase_common.js
there is standard query written for item_code. you have to just replace item query with your query.But its applied for all over Purchase flow.

Check out my answer here - Calendar view for sales opportunity? - #6 by alec_ruizramon1
Same idea as that, be sure to run bench build and bench clear-cache to get the js files built and included!

You’ll also want to put the put your js function inside something that only binds it to the PO - something like this should work …
frappe.ui.form.on(“Purchase Order”, “refresh”, function() {
// code
}

Thanks @Sangram @alec_ruizramon1 It worked!