Community
Showing results for 
Search instead for 
Do you mean 
Reply

Access PhoneNumber object for a contact's configured phone number

Copper Contributor
Posts: 7
Country: USA

Access PhoneNumber object for a contact's configured phone number

I have a Contact and I can get the phone, or mobile phone field values (as they are displayed), but I cannot seem to figure out how to get the RawNumber, FormattedNumber, and CountryCode from an existing Contact phone field.  I really need to see which country code a particular phone number was configured with (it is not the same as the country where the contact resides in many cases).

 

See test code below...If someone could help me (where it says HELP below), I would sure appreciate it!  I have searched around this forum and tried quite a few things but seem to have a mental block.  Existing code sample I saw used PhoneNumber to SET the country code (usually, presumptively, to the same country as the Contact business country).  I want to GET the country code from an existing configured phone number.

 

 // Get all contacts.

ContactList contactList = actFwk.Contacts.GetContacts(null);

foreach (Contact c in contactList)
{
   // Get business phone and mobile phone numbers.  This works,

   // but it only gets me the displayed value.  I need country code too.

   object oPhone = c.Fields["PHONE", FieldNameType.Alias];
   object oMobile = c.Fields["MOBILE_PHONE", FieldNameType.Alias];

   // Only if both are present...
  
   if ((oMobile != null) && (oPhone != null))
   {

      // Print contact info.


      log.Debug("Contact Name = " + c.FullName +

         ", Phone = " + (string)oPhone +

         ", Mobile Phone = " + (string)oMobile);

      PhoneNumber pnPhone = HELP;  // HELP: Need PhoneNumber object, business phone.


      PhoneNumber pnMobile = HELP; // HELP: Need PhoneNumber object, mobile phone.

 

      // Good, now I have the RawNumber, FormattedNumber, and CountryCode

      // for this contact's business and mobile numbers.  Lets print them.

      log.Debug("   PhoneNumber (Phone) = " + pnPhone.RawNumber +

         ", " + pnPhone.FormattedNumber + ", " + pnPhone.CountryCode);


      log.Debug("   PhoneNumber (Mobile) = " + pnMobile.RawNumber +

         ", " + pnMobile.FormattedNumber + ", " + pnMobile.CountryCode);
   }
}

 

Employee
Posts: 1,163
Country: USA

Re: Access PhoneNumber object for a contact's configured phone number

Hello Jim,

 

Looks like you've already been able to get the field descriptor for the phone fields, from there, if c is your contact you can get the phone number object like this:

 

PhoneNumber pNum = _ActApp.ActFramework.PhoneManager.GetPhoneNumberFromString(PhoneField.GetValue(c).ToString(), c);

From the PhoneNumber you can get the CountryCode, Raw and Formatted number.

 

Matthew Wood
Act! SDK Support
Community Moderator
Copper Contributor
Posts: 7
Country: USA

Re: Access PhoneNumber object for a contact's configured phone number

First, I really appreciate the response and service level this forum provides.  Thank you!

 

Second, your suggestion did not work for what I want.  I post code, and output below.  In fact I had already seen use of this in other forum posts but could not and still cannot make it work.

 

I have a contact whose business is in France, business phone is French, but mobile number is German.  I can verify this in the ACT! user interface and also in the TBL_PHONE table in the DB directly.  His mobile number has country = 49 (Germany) in both the user interface and TBL_PHONE.

 

When I use your code, I get 33 (France) as the country code on his mobile number which is not reality.  To me, this seems to be what "GetPhoneNumberFromString" does:  It takes a string of digits and formats it as IT MIGHT BE USED by the given contact...just taking the country of residence of the contact and making that the country code...as if it were not already known in the Contact record.

 

What I need is:  Give me the currently configured PhoneNumber values for that particular phone field in that Contact record.  I don't care where the guy works or lives...he has already entered his phone number including a, possibly foreign, country code.

 

Output from code below, shows GetPhoneNumberFromString() returned 33 as mobile phone CC.  I promise that is not what is in the UI or in TBL_PHONE.  I have verified it so many times!

 

Contact Name = Louis Renault, Phone = 49133 1 46 93 28 30, Mobile Phone = 44711208
   PhoneNumber (Phone)  Raw = 49133146932830, Formatted = 49133 1 46 93 28 30, CountryCode = 33
   PhoneNumber (Mobile) Raw = 44711208, Formatted = 44711208, CountryCode = 33

 

Code:

 

// Get all contacts.
ContactList contactList = actFwk.Contacts.GetContacts(null);

foreach (Contact c in contactList)
{
   // Get business phone and mobile phone numbers.

   ContactFieldDescriptor cfdPhone = actFwk.Contacts.GetContactFieldDescriptor("PHONE", FieldNameType.Alias);
   ContactFieldDescriptor cfdMobile = actFwk.Contacts.GetContactFieldDescriptor("MOBILE_PHONE", FieldNameType.Alias);

   object oPhoneValue = cfdPhone.GetValue(c);
   object oMobileValue = cfdMobile.GetValue(c);

   log.Debug("Contact Name = " + c.FullName + ", Phone = " + cfdPhone.GetValue(c) + ", Mobile Phone = " + cfdMobile.GetValue(c));

   if ((oPhoneValue != null) && (oMobileValue != null))
   {
      PhoneNumber pnPhone = actFwk.PhoneManager.GetPhoneNumberFromString(cfdPhone.GetValue(c).ToString(), c);
      PhoneNumber pnMobile = actFwk.PhoneManager.GetPhoneNumberFromString(cfdMobile.GetValue(c).ToString(), c);

      log.Debug("   PhoneNumber (Phone)  Raw = " + pnPhone.RawNumber + ", Formatted = " + pnPhone.FormattedNumber + ", CountryCode = " + pnPhone.CountryCode);
      log.Debug("   PhoneNumber (Mobile) Raw = " + pnMobile.RawNumber + ", Formatted = " + pnMobile.FormattedNumber + ", CountryCode = " + pnMobile.CountryCode);
   }
}