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
Rony57Rony57 

mobile phone and email field duplicate in account object

Hello Guys,
                I have wrote a trigger on account object that  whenever a user enter duolicate  phone number or email then it will show an error on page and the info will not stored in the account object.
Here is the code
trigger AccountTrigger on Account (before insert, before update)
{
    //Account duplicate protection system.
    if (Trigger.isBefore && (Trigger.isInsert || Trigger.isUpdate))
    {
        Map<String, Account> anAccountMap = new Map<String, Account>();
        for (Account anAccount : System.Trigger.new)
        {
            // Make sure we don't treat an mobile phone that  
            // isn't changing during an update as a duplicate.  
            if ((anAccount.Phone != null) && (Trigger.isInsert || 
                      (anAccount.Phone != Trigger.oldMap.get(anAccount.Id).Phone)))
            {
                // Make sure another new Account isn't also a duplicate  
                if (anAccountMap.containsKey(anAccount.Phone))
                {
                    anAccount.Phone.addError('Another new Account has the same mobile phone.');
                    System.debug('*** Error: found account with dublicate mobile phone: ' + anAccount);
                } else
                {
                    anAccountMap.put(anAccount.Phone, anAccount);
                }
            }
        }  
        // Using a single database query, find all the Accounts in  
        // the database that have the same mobile phone as any  
        // of the Accounts being inserted or updated.  
        for (Account aDBAccount : [SELECT Phone FROM Account WHERE Phone IN :anAccountMap.KeySet()])
        {
            Account newAccount = anAccountMap.get(aDBAccount.Phone);
            newAccount.Phone.addError('An Account with this mobile phone already exists.');
            System.debug('*** Error: found account with dublicate mobile phone: ' + newAccount);
        }
    }
}
Note:-- The thing is that its working but  its not showing any erorr on my particular page. Any help from the MVP's or the consultant  will be great.
Thanx
 
sandeep sankhlasandeep sankhla
Hi Anurag,

I checked your code and its working fine for me..It is showing the error to me if I am inserting Account with same phone number...Let me know if it is not working for you...
Puru AnnamalaiPuru Annamalai
Hi Anurag,

Did you add the <Apex:PagesMessages/> tag in your vf page ?
 
Rony57Rony57
@sandeep its not working for me  as if  it is not showing an eroor.. while inserting  both the feild
sandeep sankhlasandeep sankhla
Hi Anurag,

Same code is working for me, okay can you explain the behaviour ? when you are inserting the same phone number then it is getting saved without showing an error right ??  please cross check once adn let me know..
Rony57Rony57
@puru yes i worte earlier but nothing change ?
Rony57Rony57
Hi sandeep,
                  My  requirement is that whenever user enter  the phone number  or email number , if the data is duplicate   then it won't store in the account object  and it will show an error on  the VF page that duplicate email or phone number ... Thats it ..!!
sandeep sankhlasandeep sankhla
Hi Anurag,

I have used the same code for standard page and overriden VF page also, it is working fine for me....please share your page code here so I can look into that if you are missing anything....
sandeep sankhlasandeep sankhla
Hi Anurag,

Please use Apexpages.addMessage(new Apexpages.message(ApexPages.Severity.Error,e.getMessage()));

use Apexpage message instead of add error okay! It will work for you..
sandeep sankhlasandeep sankhla
Hi Anurag,

You can also check with these post for solution

https://developer.salesforce.com/forums/ForumsMain?id=906F0000000AaqcIAC