How to fill in the autosuggestion for a field in childtable of current doctype with data read from a field of childtable in different dictype

Hello,

I am trying to update the data read from the field of childtable present in other doctype in the field of childtable of current doctype. I am able to read the data from childtable but i cant find how to fill it in the autosuggestion options for the field in current doctype. Can anybody guide me through ?

details about the usecase -

SOURCEDOCTYPE = Vendors_List
SOURCECHILDTABLE = Item_Table
SOURCECHILDTABLE_FIELD = Item_Name

TARGETDOCTYPE = Order
TARGETDOCTYPE_FIELD = Link to SOURCEDOCTYPE
TARGETCHILDTABLE = Order_Table
TARGETCHILDTABLE_FIELD = Order_Item_Name

Selection in TARGETDOCTYPE ->TARGETDOCTYPE_FIELD is taken to access all the values from SOURCEDOCTYPE->SOURCECHILDTABLE->SOURCECHILDTABLE_FIELD and fill in the autosuggestion of the TARGETDOCTYPE ->TARGETCHILDTABLE->TARGETCHILDTABLE_FIELD.

PS : I am not looking for population the whole table but fill the autosuggestion and user will select the option for that field in the table.

I have tried 2 methods/options. opiton1 worked but didnt satisfy my requirement and second option gave me the error

Option1: JS side code

vendor_name: function(frm) {
console.log("Vendor Selected : " + frm.doc.vendor_name);

            frappe.model.with_doc("Vendor List", frm.doc.vendor_name, function() {
                    var tabledata = frappe.model.get_doc("Vendor List", frm.doc.vendor_name);
                    $.each (tabledata.item_list, function(index, row) {
                            console.log("Index : " + index  +  " Item Name :  " + row.item_name);
                            var d = frm.add_child("order_table");
                            d.item_name = row.item_name;
                            frm.refresh_field("order_table");
                    });
            });               
 }


This is not i intend to do it but i want user to choose from autosuggestion…

Options2 : JS Code:
vendor_name: function(frm) {
console.log("Vendor Selected : " + frm.doc.vendor_name);

            frm.fields_dict.order_table.grid.get_field('item_name').get_query = function() {
                    console.log("Before calling get_vendor_item from vendor_list.py");
                    return {
                            query: 'freshpoint_oms.freshpoint_oms.doctype.vendor_list.vendor_list.get_vendor_item',
                            filters: {
                                    'parent': frm.doc.vendor_name
                            }
                    }
            }
            
    }

Python side:
def get_vendor_item(doctype, txt, searchfield, start, page_len, filters):
data = frappe.get_list(“Item List Table”, filters, [“item_name”])
return data

but i get error -
image

All i want to achieve is -

Would love to hear from the community,

Regards,
Praveen

Hi Community,

Its been 4d and i havent got a reply to my query. I wanted to understand if my query is unclear or too long for anyone to understand so that i can try to edit the query and simplify it further. Kindly help me here as i am stuck with this query.

Thanks & Regards,
Praveen N

I’m not an expert. But what you want Indeed is to use get_query here’s the documentation Since what you want to do is offer your items from another document. You should override the query method, and your method ideally puts your suggestion first. I would do something like this:

def my_query(self):
    # This will get you the items you want' to suggest
    suggestion = frappe.db.get_all(
        doctype='Vendor Items List',
        filters = {
            parent='The document that has the items you want to suggest'
        },
        fields="name"
        )
    # This will get you all of the items option
    # Make sure to filter for what you want
    all = frappe.get_all(doctype='Items', fields="name")

    # And is very important to note that frappe.get_all
    # returns a list of dicts as [{'name': 'Item1},{'name': 'Item2}]
    # And I'm not completly sure but I think the result of the query
    # Is a list of document names, so you can do:
    # This will convert from the list of dicts to ['Item1', 'Item2', etc]
    suggestions = list(map(lambda x: x["name"], suggestion)
    all = list(map(lambda x: x["name"], all)
    # Then set suggestion forward in the list and use set
    # to delete duplicates
    result = list(set(suggestion + all))
    return result

Take it with a grain of salt, since I haven’t wrote my first query method so far :smiley:

Javier, Thanks for responding. I will surely give it a try and let you know. If it works, then u have indirectly wrote your first query method :wink:

1 Like