Telephone Number Formatting

Telephone Number Formatting

Hi, Following on from the April digest, here is a custom function, that Ive got in place for UK based +44 numbers 

  1. contact = zoho.crm.getRecordById("Contacts", contact_id);

  2. // Get ONLY the Phone field. 
  3. phone_number = contact.get("Phone");

  4. // If phone_number isn't null...
  5. if (!isnull(phone_number)) {
  6.     info "Original phone number: " + phone_number;  // Log the original value

  7.     // Remove spaces
  8.     phone_number = phone_number.replaceAll(" ", "");

  9.     // Replace 01, 02, 07, 0044, +440 or 44 with +44 directly
  10.     if (phone_number.startsWith("01") || phone_number.startsWith("02") || phone_number.startsWith("07")) {
  11.         phone_number = "+44" + phone_number.substring(2);
  12.     } else if (phone_number.startsWith("0044") || phone_number.startsWith("+440")) {
  13.         phone_number = "+44" + phone_number.substring(4);
  14.     } else if (phone_number.startsWith("44")) {
  15.         phone_number = "+" + phone_number; // Simply add the "+" sign at the beginning
  16.     }

  17.     info "Modified phone number: " + phone_number;  // Log the modified value

  18.     // Update ONLY the Phone field if a replacement was made.
  19.     if (phone_number != contact.get("Phone")) { 
  20.         phone_update = Map();
  21.         phone_update.put("Phone", phone_number);
  22.         zoho.crm.updateRecord("Contacts", contact_id, phone_update);
  23.     } else {
  24.         info "No update needed. Phone number is already in the correct format."; 
  25.     }

  26. } else {
  27.     info "No phone number found for this contact.";
  28. }