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
balakrishna mandula 6balakrishna mandula 6 

Duplicate Accounts while converting a lead

Hi,
I am creating web-to-lead and converting that lead into contact not opportunity. while converting lead, if there is duplicate accountname in lead convert detail page then
1. Error is to be displayed on convert detail page
2. Mail should be sent to Email which is there in email field on lead saying "Duplicate Account found"
 
trigger Duplicatecheck on Account (before Insert, before Update) {
Map<String, Id> mapAccount = new Map<String, Id>();
Set<String> setAccName = new Set<String>();
Set<String> email = new Set<String>();
List<String> sendTo = new List<String>();
List<Messaging.SingleEmailMessage> mails =new List<Messaging.SingleEmailMessage>();
list<Account> Acclist=new list<Account>();
    String subject='Lead Duplicate';
 for(Account acc : trigger.new){
        setAccName.add(acc.Name);
        sendTo.add(acc.L_Email__c);
        Acclist.add(acc);
   // System.debug('Contact Name :'+acc.LastName);       
     for(Account acc1 :[SELECT Id,Name FROM Account WHERE  Name IN :setAccName]){ 
        // String x=acc1.Name;
         //String y=x.toUpperCase();
        mapAccount.put(acc1.Name, acc1.Id);
       //System.debug('Contact List:'+acc1); 
        if(acc.Name==acc1.Name){
            
          /*  if(true) {
            Messaging.SingleEmailMessage email1 = new Messaging.SingleEmailMessage();  
             String[] toAddresses = new String[]{'krishnasfdc07@gmail.com'};
            email1.setSubject(subject);
             email1.setToAddresses(toAddresses);
            email1.setPlainTextBody('Hi This is '+acc1.Name+' Duplicate.');
             System.debug('email is'+email1);
             mails.add(email1);
             
              // Messaging.sendEmail(new Messaging.SingleEmailMessage[] {email1});
           // System.debug('contains Key is : '+mapContact.containsKey(acc.LastName)); 
            //System.debug('get Key is : '+mapContact.get(acc1.LastName));
            
            
            //acc.addError('There is already another Account with the same Name. ' +'Refer: <a href=\'/' + mapAccount.get(acc1.Name) + '\'>' + acc1.Name + '</a>',FALSE);
             
          //Messaging.sendEmail(mails);  
            }
            */
             acc.addError('There is already another Account with the same Name. ' +'Refer: <a href=\'/' + mapAccount.get(acc1.Name) + '\'>' + acc1.Name + '</a>',FALSE);   
        }
     }
  }
    
}


 
Grazitti TeamGrazitti Team
Hi Balakrishna,

If you are using VF page then you can add a check in controller itself for any existing accounts and display error messages accordingly. Also, you can create final list of leads to be converted in controller itself and use a batch class for smooth conversion.

Hope it helps you.

Thanks,
Grazitti Team.