Strange Issue when with customer name and phone number

I have a custom Doctype with two fields: customer_name and phone_no. One is of type Link and is used to select the name of the customer. The other field is of type Data, where the phone number of the selected customer will be stored.
The expected behavior is as follows: when I select a customer name, a JavaScript event is triggered in a Custom Script. This function must retrieve the value of the mobile_no field in the Customer Doctype and insert it into the phone_no field of my custom Doctype. The strange thing is that, the first time a customer is selected, the phone_no field is not filled in, but if a customer is selected again (either the same or another customer ) the phone_no field is filled correctly.

This is the code of the Custom Script:

frappe.ui.form.on("my_doctype",
    customer_name: function (frm)
    {
        if (cur_frm.doc.customer_name)
            cur_frm.add_fetch('customer_name', 'mobile_no', 'phone_no');
        else cur_frm.doc.phone_no = null;
    }
);

Why does this happen?


Additionally, I want to format the phone number, and I’ve tried it with the following function:

cur_frm.doc.phone_no.replace(/(\d{3})(\d{3})(\d{3})/, '$1 $2 $3');

but it does not work and I do not know why.

You don’t need to use this custom script. I have been facing this with custom queries overriding the standard queries for Link fields.

You can directly map the phone number field in Customer to that field in your custom doctype like this:

As it has been done for full_name do the same for the field which has the phone number. Just find the correct field name from Customize Form and repeat it on that field.

1 Like

Awesome, it is working :blush:
In this way it is easier.

In the past I tried to carry it out in a similar way, but it did not point to the correct fields and so I decided to do it with a custom Script.

Thanks a lot @root13F!