Yes "stupid proof" is what I specialise in!
The best way would be to create either an automated action or a onchange event depending on your knowledge of creating Odoo apps you can do an onchange event which will remove all spaces from numbers as soon as they click off the field eg:
(requires creating an external app)
@api.onchange("phone")
def remove_spaces_from_phone_number(self):
for record in self:
if record.phone:
record.phone = record.phone.replace(" ", "")
(do the same for mobile)
Or create an automated action in Settings->Tehnical->Automated Action. You would set the fields as Model->Contact, Action To Do->Execute Python Code and put "phone, mobile" in the On Change Fields Trigger. (Also make sure the module "base_automation" is installed.
(can all be done within Odoo GUI)
The Python code would be:
if record.phone:
record["phone"] = record.phone.replace(" ", "")
if record.mobile:
record["mobile"] = record.mobile.replace(" ", "")
For already existing phone numbers you can then write a scheduled action which would be:
all_contacts = env["res.partner"].search(["|", ("phone", "!=", False), ("mobile", "!=", False)])
for contact in all_contacts:
if contact.phone:
contact["phone"] = contact.phone.replace(" ", "")
if contact.mobile:
contact["mobile"] = contact.mobile.replace(" ", "")
You can run this once and it should be ok as users can no longer add spaces to numbers, or have this running once a day just incase.
I hope this helps!
Hello Lucas,
Would you prefer to keep the spaces in the numbers? As I have a solution which will remove all spaces, meaning that you wouldn't need the wildcard.
Thanks,