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.