Field not permitted in query: customer

frappe.ui.form.on('Quotation', {
    refresh(frm) {
        // Query for child customers where parent_customer is customer_name
        frappe.db.get_list("Customer", {
            filters: { 'parent_customer': frm.doc.customer_name },
            fields: ['name', 'customer_name']
        }).then(child_customers => {
            // Include the parent customer in the list of customers to be queried for addresses
            let all_customers = [frm.doc.customer_name].concat(child_customers.map(c => c.name));

            // Fetch all addresses (no need to check is_group here)
            frappe.db.get_list("Address", {
                fields: ['name', 'address_line1', 'address_line2', 'city', 'state', 'country', 'customer']
            }).then(addresses => {
                // Filter the addresses to include only those related to our customers
                let filtered_addresses = addresses.filter(address =>
                    all_customers.includes(address.customer)  // Filter by customer names
                );

                // Prepare the addresses for the 'customer_address' field
                let address_options = filtered_addresses.map(address => {
                    return {
                        value: address.name,
                        label: `${address.address_line1}, ${address.address_line2}, ${address.city}, ${address.state}, ${address.country}`
                    };
                });

                // Update the 'customer_address' field with the filtered addresses
                frm.set_df_property('customer_address', 'options', address_options);

                // Optionally, set the first address as the default value
                if (filtered_addresses.length > 0) {
                    frm.set_value('customer_address', filtered_addresses[0].name);  // Set default address if available
                }
            }).catch(err => {
                console.error("Error fetching addresses:", err);
            });
        }).catch(err => {
            console.error("Error fetching child customers:", err);
        });
    }
});

I change the customer doctype to a tree structure to able it to hold subsidiaries.
Now I am trying to create a new set of addresses in customer address from quotation doctype. Those address will be consists of the parent customer and the child customer. but I got an error stating Field not permitted in query: customer.
Can anyone help me with this?

because the address doctype has no customer field.

1 Like

I see. Thanks for pointing it out. let me try it out.

Now that you mentioned that customer is not a field in address doctype. Also I did check the table of address and there is no column that saves the customer or link to it. How does address save the Link for customer in the addresses?

you have to get the child table data.

How would I get the child table data? It is also not on the address table.

what is the use case, you want all customer addresses displayed in the quotation, right?

Only the related ones, I made the Customer doctype to a tree. there are new fields like is_group, old_parent and parent_customer. Whenever a parent customer is selected as a customer in quotations. customer address field should have all the address of the said parent and its child customers, I did change the approach as it only needs the addresses so I rewrite the whole code to simple override the filter of customer_address. but it wont allow me

frappe.ui.form.on('Quotation', {
    refresh(frm) {
        // Make sure the customer is selected
        if (!frm.doc.customer_name) {
            return;  // No customer selected, don't proceed with filter setting
        }

        // Fetch child customers for the selected parent customer
        frappe.db.get_list("Customer", {
            filters: { 'parent_customer': frm.doc.customer_name },
            fields: ['name', 'customer_name']
        }).then(child_customers => {
            // Combine parent and child customers into a single array
            let all_customers = [frm.doc.customer_name].concat(child_customers.map(c => c.name));

            console.log('All customers (parent + children):', all_customers);  // Debugging

            // Set the filter for 'customer_address' field to include addresses of parent and child customers
            frm.fields_dict['customer_address'].get_query = function(doc, cdt, cdn) {
                console.log('Applying filter for customer_address field...');  // Debugging

                return {
                    filters: {
                        'link_doctype': 'Customer',  // Ensure it's related to a Customer
                        'link_name': ['in', all_customers]  // Filter by link_name being any of the parent or child customers
                    }
                };
            };

            console.log('Customer Address filter set successfully.');  // Debugging
        }).catch(err => {
            console.error("Error fetching child customers:", err);
        });
    }
});