You need to sign in to do that
Don't have an account?
Rony57
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
Thanx
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
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...
Did you add the <Apex:PagesMessages/> tag in your vf page ?
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..
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 ..!!
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....
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..
You can also check with these post for solution
https://developer.salesforce.com/forums/ForumsMain?id=906F0000000AaqcIAC