+ Start a Discussion
Khalid mnKhalid mn 

If inserting two Account with same phone no then delete one account on insertion..

Hi,
I have a scenario where if we insert two account at a time with same phone no then delete one account..
I tried below Trigger but there problem is if i am inserting one account with existing account phone no then it is working fine but on the insertion of two account it's not working...
can someone please help me...
trigger DelOneAcc on Account (before insert) {  
    // Here i am storing phone of account into set
    Set <String> phoneSet = new Set<String>();      
    // Iterate through each Account and add their phone number to their respective Sets   
    for (Account Acc:trigger.new) {        
        phoneSet.add(Acc.phone);         
    }      
    // New list to store the found phone numbers  
    List <Account> AccList = new List<Account>();      
        
    AccList = [Select Id, Name, Phone from account WHERE phone IN :phoneSet];     
    // Iterating through each account record to see if the same phone was found     
    for (Account Acc:AccList) {        
        If (AccList.size() > 1) {                             
        }  
    }   
    delete AccList; 
}
Thank You in Advance
 
RenatoFRenatoF
for (Account Acc:AccList) {        
        If (AccList.size() > 1) {                             
        }  
    }   
    delete AccList;

This is correct? The if body is empty..
Prashant_pgPrashant_pg
Are you getting any errors or just functionality is not working?

If it is 2nd case.... this trigger will check only existing records of salesforce. Instead of deleting existing records you can try preventing new records from being inserted.
PriyaPriya (Salesforce Developers) 

Hi Khalid,

Is this your complete code ?

May I know what action you are taking in IF block section ?

Regards,

Priya ranjan

Khalid mnKhalid mn
trigger DelOneAcc on Account (after insert) {  
    // Here i am storing phone of account into set
    Set <String> phoneSet = new Set<String>();      
    // Iterate through each Account and add their phone number to their respective Sets   
    for (Account Acc:trigger.new) {        
        phoneSet.add(Acc.phone);         
    }      
    // New list to store the found phone numbers  
    List <Account> AccList = new List<Account>();      
        
    AccList = [Select Id, Name, Phone from account WHERE phone IN :phoneSet];     
    // Iterating through each account record to see if the same phone was found     
    for (Account Acc:AccList) {        
        If (AccList.size() > 0) {                             
        }  
    }   
    delete AccList[0]; 
}
This is the compelete code here i this code work for insertion if i am inserting 2 or more account with same phone it deleting only one but my requirement is delete all the existing account with same phone no and also delete that account which i am inserting with same phone except one.
 
Ankit Maini.Ankit Maini.
Try the below code:
trigger AccountTrigger on Account (after insert) {
	// Here i am storing phone of account into set
    Set <String> phoneSet = new Set<String>();      
    // Iterate through each Account and add their phone number to their respective Sets   
    Map<String,Id> mapOfPhone = new Map<String, Id>();
    for (Account Acc:trigger.new) {  
        	phoneSet.add(Acc.phone); 
                
    }      
    // New list to store the found phone numbers  
    List <Account> AccList = new List<Account>();      
        
    AccList = [Select Id, Name, Phone from account WHERE phone IN :phoneSet Order By CreatedDate];     
    // Iterating through each account record to see if the same phone was found   
    Set<String> phoneContainsSet = new Set<String>();
    List<Account> phoneDeleteSet = new List<Account>();
    for (Account Acc:AccList) {        
        If (!phoneContainsSet.contains(acc.Phone)) {   
            phoneContainsSet.add(acc.Phone);
            //delete AccList; 
        }  
        else {
            phoneDeleteSet.add(acc);
        }
    }   
    
    if(phoneDeleteSet.size()>0) {
        delete phoneDeleteSet;
    }
    
}

Thanks
Ankit​​​​​​​