function readOnly(count){ }
Starting November 20, the site will be set to read-only. On December 4, 2023,
forum discussions will move to the Trailblazer Community.
+ Start a Discussion
Prince VenkatPrince Venkat 

Specified Format

Hi
Whenever we click on edit on contact phone it should be displayed in this specified format (xxx) xxx-xxxx

but i am getting like this how can we achieve this User-added image
Best Answer chosen by Prince Venkat
CharuDuttCharuDutt
Hii Prince Venkat
Try Below Trigger
trigger AccountTrigger on Account (before insert,before update){
     string nondigits = '[^0-9]';
  string PhoneDigits;
    if(trigger.isInsert){
        if(trigger.isBefore){
            system.debug('Beforeinsert');
            for(Account acc: trigger.new){
                if(acc.phone != Null){
                    PhoneDigits = acc.phone.replaceAll(nondigits,'');
                    if (PhoneDigits.length() == 10){ 
     acc.phone = '('+PhoneDigits.substring(0,3)+')' + '-' +
           PhoneDigits.substring(3,6) + '-' +
           PhoneDigits.substring(6,10);
                    }
                }
            }
                
        }
    } else if(trigger.isupdate){
        if(trigger.isBefore){
            system.debug('Beforeinsert');
            for(Account acc: trigger.new){
                if(acc.phone != Null){
                    PhoneDigits = acc.phone.replaceAll(nondigits,'');
                    if (PhoneDigits.length() == 10){ 
     acc.phone = '('+PhoneDigits.substring(0,3)+')' + '-' +
           PhoneDigits.substring(3,6) + '-' +
           PhoneDigits.substring(6,10);
                    }
                }
            }
                
        }
    } 
}
Please Mark It As Best Answer If It Helps
Thank You!

All Answers

CharuDuttCharuDutt
Hii Prince Venkat
Try Below Trigger
trigger AccountTrigger on Account (before insert,before update){
     string nondigits = '[^0-9]';
  string PhoneDigits;
    if(trigger.isInsert){
        if(trigger.isBefore){
            system.debug('Beforeinsert');
            for(Account acc: trigger.new){
                if(acc.phone != Null){
                    PhoneDigits = acc.phone.replaceAll(nondigits,'');
                    if (PhoneDigits.length() == 10){ 
     acc.phone = '('+PhoneDigits.substring(0,3)+')' + '-' +
           PhoneDigits.substring(3,6) + '-' +
           PhoneDigits.substring(6,10);
                    }
                }
            }
                
        }
    } else if(trigger.isupdate){
        if(trigger.isBefore){
            system.debug('Beforeinsert');
            for(Account acc: trigger.new){
                if(acc.phone != Null){
                    PhoneDigits = acc.phone.replaceAll(nondigits,'');
                    if (PhoneDigits.length() == 10){ 
     acc.phone = '('+PhoneDigits.substring(0,3)+')' + '-' +
           PhoneDigits.substring(3,6) + '-' +
           PhoneDigits.substring(6,10);
                    }
                }
            }
                
        }
    } 
}
Please Mark It As Best Answer If It Helps
Thank You!
This was selected as the best answer
AbhinavAbhinav (Salesforce Developers) 
Check this:

The format used for currency, dates, times, phone numbers, and names of people in Salesforce is determined by your Locale setting.

https://help.salesforce.com/s/articleView?id=sf.basics_understanding_values_curr_date_phone.htm&type=5

Thanks!
CharuDuttCharuDutt
Hi Prince Venkat
1: GO To Setup In Quick Find Box Search Process Builder And Select Process Builder
2:
Click NEW Button Specify The Name Of Process And Select The Option In Start The process When A Record Changes
 Follow As Show in Images below
3:User-added image
User-added image
User-added image

Click Save Button  And Click Activate Button.



Please Mark It As Best Answer If It Helps
Thank You!

 
Prince VenkatPrince Venkat
Hi charuDutt
But If we are using Ischanged in the criteria, we can't check the 'Advanced' checkbox
 
Prince VenkatPrince Venkat
Yes
User-added image
CharuDuttCharuDutt
there's no need to for advanced checkbox to be checked process builder will work without it also