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
Devaraj 7Devaraj 7 

what is the best practice for below trigger i want to prevent duplicate phone in my account

trigger predupphone on Account (before insert) {        
  for(Account a:trigger.new)    
 {        
list<Account> la= [select id from Account where phone=:a.Phone];          
if(la.size() > 0)        
 {           
  a.Phone.adderror('phone already exist');         
 }            
  }
Raj VakatiRaj Vakati
trigger DPhone on Account(before insert,before update) 
{

 //Preparing Lead phone in Set from trigger.new

 Set<String> leadSet = new Set<String>();

 for(Account l : trigger.new){

     leadSet.add(l.phone);        
 }

 //query all existing record for item
 List<Account> leadList = [select id from Accountwhere phone in : leadSet];

for(Account l:trigger.new) 
{
    //incase of update
    if(Trigger.isUpdate && l.phone!=null && leadList.size()>0 && Trigger.oldmap.get(l.id).phone!=l.phone)
    {
       l.Phone.adderror('Another record has same Phone No.');
    }

    //Because old map is not available we only check for size
    if(Trigger.isInsert && l.phone!=null && leadList.size()>0)
    {
       l.Phone.adderror('Another record has same Phone No.');
    }

} 

}

 
Raj VakatiRaj Vakati
https://salesforce.stackexchange.com/questions/115638/trigger-to-check-duplicate-on-phone