Field filter based on antoher fields

Hello,
I’m trying to filter a field based on another field in a DocType Custom of my ERPnext app.
In my Custom doctype I have inserted two fields link to Customer and Address (both “default” doctype of erpnext).
My goal would be to select the customer in the Customer field and consequently in the Address field only the addresses of that customer should appear and not all those entered in the system.
How can I do?
Thank you

dear Tiziano_Pedrazzoli,

i have been added custom address field and populate data from customer.
my custom fields are,

  1. Shipping Address Name - Link - shipping_address_name - Address
  2. Shipping Address - Small Text - shipping_address -

my code is

refresh(frm) {

frm.set_query('shipping_address_name', erpnext.queries.address_query);
erpnext.utils.get_address_display(frm, 'shipping_address_name', 'shipping_address', false);

}

Note that my form have customer field. and dont use any filter for them.

Emre, thanks for your reply but I guess that’s not what I’m looking for.
In my scenario each customer can have more addresses and in case it must be possible to assign the same address to more customers. When I select a customer in the custom doctype, I must be able to choose only between the addresses that are connected to that customer.
Therefore adding a reference to the address in the customer and vice versa cannot be a complete solution.
Thanks anyway

you are talking what i mean with script, did you try that? when ever you chose customer you only see that customer addresses in the address field. not the others.

I tried but it doesn’t work, at this point I think I have something wrong.
This is my script:

frappe.ui.form.on('MC_Linea', {
        setup: function(frm) {
                frm.set_df_property('stabilimento', "read_only", 1);
                /*
                //filtro sul campo stabilimento per mostrare solo gli indirizzi del cliente scelto
                        //Porco cane non funziona 
                frm.set_query("stabilimento", function() {
                        return {
                                filters: [
                                        ["Customer","name", "in", cur_frm.doc.customer]
                                ]
                        }
                });*/
        },
        customer: function(frm){
                if(cur_frm.doc.customer == ''){
                        frm.set_df_property('stabilimento', "read_only", 1);
                        cur_frm.doc.stabilimento = '';
                        frm.refresh();
                }else{
                        frm.set_df_property('stabilimento', "read_only", 0);

                }
        },
        refresh: function(frm) {
                frm.set_query('stabilimento_name', erpnext.queries.address_query);
                erpnext.utils.get_address_display(frm, 'stabilimento_name', 'stabilimento', false);
        }
});
~    

These are my fields:

Dear Tiziano,

could you change stabilimento to do small text and make it ready only at customization not in script. and try to use below script.

frappe.ui.form.on('MC_Linea', {
       refresh (frm) {
                frm.set_query('stabilimento_name', erpnext.queries.address_query);
                erpnext.utils.get_address_display(frm, 'stabilimento_name', 'stabilimento', false);
    },
});

Dear emre,
I changed stabilimento to small text and I put it read only, but it show all the addresses.

  1. field : Stabilimento - Link - stabilimento_name - Address
  2. field: Empty_Name - Smal Text - stabilimento -
    did you changed 1. field? change second one with small text.

And you have customer fields right?


stabilimento (second row, without capital letter) is small Text and read only.

edited my comment

I have one customer filed, not the customer_name

could you add that to please

done. should it work now?

suppose to be

doesen’t work, the stabilimento field continues to show all the addresses and not just those of the selected customer

could you try with

{% include 'erpnext/selling/sales_common.js' %}

i mean

{% include 'erpnext/selling/sales_common.js' %}
frappe.ui.form.on('MC_Linea', {
       refresh (frm) {
                frm.set_query('stabilimento_name', erpnext.queries.address_query);
                erpnext.utils.get_address_display(frm, 'stabilimento_name', 'stabilimento', false);
    },
});

i have been added extra address field with this script at Opportunity, when i checked in github there isn’t any filter for address fields only this {% include ‘erpnext/selling/sales_common.js’ %} added at start of script. if its not help also, maybe more experienced community member answer that since its custom doctype it might be different.