I am new to the Salesforce OOB features as of now. I have an Account and made the Phone field compulsory while creating an account.

Ex: I have an Account created using Phone=(911) 345-0987, if someone tried to create the account using the same phone, then he should get the error message saying that Phone=(911) 345-0987 is already in used. Please use the other one.

How to achieve this functionality ?

Note: If I used trigger solution, then it works only for the 50,000 Accounts and after that it will not works???

Update-1, posts Santanu's answer:

enter image description here

  • You need to probably use a trigger to check for duplicates – Carlos Naranjo May 21 '17 at 16:35
  • @Carlos Naranjo - Please see my answers below and few doubts along with that ? – user4567570 May 21 '17 at 16:50
  • @Carlos Naranjo - Will this works for bulk data upload ? – user4567570 May 21 '17 at 16:53
  • I think, you have taken the picture when duplicate rule settings is "allow" options and it will show alert or report, is that correct? – Santanu Boral May 22 '17 at 0:48

Rather than using trigger, it is recommended to leverage OOTB Duplicate Management.

Define Matching Rules

matching rule

Define Duplicate Rule (Allow, display alert and report)

duplicate rule

Display of error during duplicate record creation

duplicate error

Define Duplicate Rule (block)

define rule block

Display of error during duplicate record creation

block error

For more information, refer Managing Duplicate Records in Salesforce

or Duplicate Management Trailhead

Also, think of making the Phone field as unique in the configuration, just in case your use case supports, then this duplicate management will not be required.

  • Thanks. Only problem I see although it gives alert and if still go for it then it saves the same account for both Number. What we can do in this regard? Pls see above posts updates and answer on all query. – user4567570 May 21 '17 at 17:43
  • updated my answer, you can configure to entirely block. So no duplicated reocrds get saved – Santanu Boral May 21 '17 at 17:49
  • 2
    I like this approach, its clean. Also better to go for this instead of Trigger I think. – user4567570 May 21 '17 at 17:55
  • Can we deploy Matching Management using Change Set ? – user4567570 May 21 '17 at 19:25
  • Yes, you can deploy that – Santanu Boral May 21 '17 at 19:35

I think you can use the very standard trigger used for Leads which it is a old SFDC example on how to use trigger:

trigger accountPhonePreventer on Account (before insert, before update) {

Map<String, Account> accountMap = new Map<String, Account>();
for (Account account : System.Trigger.new) {

    if ((account.Phone != null) &&
            (System.Trigger.isInsert ||
            (account.Phone != 
                System.Trigger.oldMap.get(account.Id).Phone))) {

        if (accountMap.containsKey(account.Phone)) {
            account.Phone.addError('Another new account has the '
                                + 'same phone number.');
        } else {
            accountMap.put(account.Phone, account);

for (Account account : [SELECT Phone FROM Account
                  WHERE Phone IN :accountMap.KeySet()]) {
    Account newAccount = accountMap.get(account.Phone);
    newAccount.Phone.addError('A account with this phone number '
                           + 'already exists.');

This should prevent the inserting and update of accounts with the same phone and it will display the error on addError

This error should fire every time that an account meets the criteria, in this case, same phone number.

I will also suggest that you move your code logic out of your triggers. You can create help class with your logic and then call it from your trigger.

  • Can we override SDFC custom message ? I have already highlighted in bold in my post above ! – user4567570 May 21 '17 at 17:29

Thanx @Carlos Naranjo for suggestions. I write trigger which shows me my custom error message.

trigger AccountTrigger on Account (before insert) {
    for(Account acct : Trigger.New){
        List<Account> acctList = [Select Id, AccountNumber from Account where Phone= :acct.Phone];
        if(acctList.size() > 0){
            acct.addError('Phone '+acct.Phone +' is already present. Please use other one.');

I don't want to show error message highlighted in the green box. Does this using trigger solution will work for only 50,000 accounts only ? What after 50,000 accounts ?

enter image description here

  • Your solution won't work if there are duplicate records in the file itself. – Raul May 24 '17 at 8:19

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.