0

In order to retrieve a contact, having a cell phone number of 09362724853, I use following code:

$newSMS_contact = new Contact;
$newSMS_contact->retrieve_by_string_fields(array('phone_mobile'=>'09362724853'));

How about retrieving a contact having a cell phone number of 09362724853 OR 9362724853 OR +989362724853 with sugar internal functions?

This doesn't work:

$newSMS_contact = new Contact;
$newSMS_contact->retrieve_by_string_fields(array('phone_mobile'=>'09362724853', 'phone_mobile'=>'9362724853', 'phone_mobile'=>'+989362724853'));
hpaknia
  • 2,769
  • 4
  • 34
  • 63

2 Answers2

3

The thing is that the function which you are trying to utilize was created for other goals. Since it fetches only one row from DB and fills a Bean with it, the Array of parameters will be turned into a string separated by AND operators. But you have completely different case.

I would suggest to use another approach, which is less convenient but more reliable:

$contact_bean  = new Contact();
$contacts_list = $contact_bean->get_full_list(null, '(phone_mobile = "09362724853" OR phone_mobile = "9362724853" OR phone_mobile = "+989362724853")');

Eventually, you will have an array of beans. Probably, for some modules, you will need to use table aliases for fields definition into SQL supplement.

Dan
  • 46
  • 3
  • Atlast it worked, but with a little difference: $bean = BeanFactory::getBean('Contacts'); $contact_list = $bean->get_full_list('', 'contacts.phone_mobile LIKE \'%9362724853\''); like statement is not the matter. BeanFactory::getBean is the key point! – hpaknia Aug 26 '13 at 18:07
  • Yup... I missed one buggy moment, sorry - when you call Sugar native classes (Accounts, Contacts, Leads etc) you must write their names in singular. (e.g. $contact = new Contact(); $account = new Account); /* but */ $contact = loadBean('Contacts'); // etc... ) – Dan Sep 01 '13 at 11:03
0

If I were you, I'd have strict rules when the phone numbers are put in the system so you can be sure your phone numbers follow a certain format in the database. (Something like E.164: http://en.wikipedia.org/wiki/E.164) You can enforce the rules with a custom SugarField (or override one that exists) that has Javascript and Server-side validation.

This way, you won't have to worry about that logic in this piece of the code or anywhere else you want to deal with phone numbers.

Chad Hutchins
  • 474
  • 3
  • 9
  • I use izeno_sms module, this module can send sms to any field in other sugar modules! So I can not check all fields of sugar modules to be a valid phone number. Sugar is a giant system, there should be a systematic way for my question! – hpaknia Aug 25 '13 at 03:47
  • @HPM if you made a custom SugarField that represented E.164 phone fields and changed the field types of your phone number fields you would be able to easily control it all however you like. This way would give you a single place to control the logic for the many phone fields you may have. – Chad Hutchins Aug 25 '13 at 15:07
  • izeno_sms module can use for example contact name (!) to send sms. It means if contact name is 'hasan', an sms is sent to the number:hasan. Of course sms provider can not send this sms, but suppose I have a contact list, having phone number in name field. It is not rational to change field type of name in contacts module. – hpaknia Aug 25 '13 at 15:38