This question has been flagged
2 Replies
3417 Views

Hello everyone!

I have phone numbers in contacts written in different forms, with whitespaces, without, in group of 2 - 3 digits.

How to search in those all forms entering number in search form as one continous string of digits?

Let's say writing in search form - 123456789 i want to get results with phone numbers:

123456789

123 456 789

12 34 56 789

+33 12 34 56 789

etc.

The solution (stupid?) is to add wildcard "%" between each letter in search form - %1%2%3%4%5%6%7%8%9%

But maybe there is another - better solution? :)


Avatar
Discard

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,

Best Answer

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!

Avatar
Discard
Author

Jack, I chose the solution from the GUI.

I installed base_automation, but I cannot see plase to put triggers - phone, mobile. Where it should be?

Maybe it's needed to somehow restart engine after installing base_automation?

Author

UPDATE. I somehow open "Planned Actions" instead of "Automated Action". It looks, that i have no more spaces in phone numbers so... IT WORKS!

Please PM me you address I will send you big beer :)

Hello Lucas,

When you select Action To Do->Execute Python Code it should be below the field "Apply On".

Thanks,

Author Best Answer

Hello Jack,

it depends, i cannot assume that all ODOO users will hold on new rule with entering phone number, does your solution will replace all existing numbers with space-less ones, and eventually remove spaces in number entered in future? In short, is it "stupid proof"? :)

Avatar
Discard