0

I am looking for a way to format Phone numbers on global standards in Contact object based on the Users Locale, as default for USA and Canada platform formatting the as (EX: (897) 814-8981) base on location.

I need a auto formatting process for all other global phone numbers too. There is any App exchange tool for performing this task else suggest me a work around.

Any help is Appreciated..

1 Answer 1

0

I once wrote a piece of Apex Code, this could be useful for you but it might need some work.

    private static final Set<Integer> PHONENUMBER_ONEDIGITCOUNTRYCODES = new Set<Integer>
    {
            1, 7
    };
    private static final Set<Integer> PHONENUMBER_THREEDIGITCOUNTRYCODES = new Set<Integer>
    {
            35, 37, 38, 42, 99, 21, 22, 23, 24, 25, 26, 29, 35, 37, 38, 42, 50, 59, 67, 68, 69, 85, 88, 96, 97, 9
    };


    global virtual class PhoneNumber
    {

        public Integer countryCode { get; set; }
        public Integer areaCode { get; set; }
        public Long localNumber { get; set; }


        global PhoneNumber(String rawPhoneNumber)
        {
            if (rawPhoneNumber != null && rawPhoneNumber.length() > 0)
            {
                String str = rawPhoneNumber.replaceAll('([+-])', '').replaceAll(' ', '')
                        .replaceAll('\\(', '')
                        .replaceAll('\\)', '');
                if (str.length() == 11)
                {
                    if (PHONENUMBER_ONEDIGITCOUNTRYCODES.contains(Integer.valueOf(str.substring(0, 1))))
                    {

                        this(Integer.valueOf(str.substring(0, 1)),
                                Integer.valueOf(str.substring(1, 4)),
                                Integer.valueOf(str.substring(4)));
                    }
                    else if (PHONENUMBER_THREEDIGITCOUNTRYCODES.contains(Integer.valueOf(str.substring(0, 2))))
                    {

                        this(Integer.valueOf(str.substring(0, 3))
                                , Integer.valueOf(str.substring(3, 6))
                                , Integer.valueOf(str.substring(6)));
                    }
                    else
                    {

                        this(Integer.valueOf(str.substring(0, 2))
                                , Integer.valueOf(str.substring(2, 4))
                                , Integer.valueOf(str.substring(4)));
                    }
                }
                else
                {
                    localNumber = Long.valueOf(str);
                }
            }
        }

        global PhoneNumber(Integer country, Integer area, Integer zone)
        {
            System.debug(country + '  ' + area + '  ' + zone);
            this.countryCode = country;
            this.areaCode = area;
            this.localNumber = zone;
        }

        global override String toString()
        {
            return ((countryCode != null) ? '+' + String.valueOf(countryCode) : '')
                    + ((areaCode != null) ? '(' + String.valueOf(areaCode) + ')' : '')
                    + ((localNumber != null) ? String.valueOf(localNumber) : '');
        }
    }

You must log in to answer this question.

Not the answer you're looking for? Browse other questions tagged .