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
Soubhagya Ranjan 2Soubhagya Ranjan 2 

want to send email to owner of account when duplicate account is created

I want to write a trigger in account object . when a new record is created with same name then it will automatically send mail to account owner that duplicate record is created .
 
Gaurav PatilGaurav Patil

It will handle bulk condition also

1. Write trigger after insert on the account object.
2. Get created records in Trigger.new
3. Create a set of name from new records
4. Fire soql: if name in exist in this set 
5. If the result contains a non-zero number of rows then definitely account with the same name exist. 
6. Now check which newly created record is duplicate out of result generated.
7. Send Mail to Account owner.

sfdc  novicesfdc novice
Hi Soubhagya,

Please find the Code for your Requirement.

trigger DupeAccountEmailTrg on Account (after insert) {
    
    set<string> accnames = new set<string>();
    
    for(Account acc: Trigger.new){
        accnames.add(acc.name);
    }
    
    
    List<Account> accounts = [SELECT Id,Name FROM Account WHERE Name IN :accnames];
    
    
    if(accounts.size() > 0){
            
        List<Messaging.singleEmailMessage> mails =  new List<Messaging.singleEmailMessage>();
    
    for(Account myAccount : [SELECT Id,Name,Owner.email FROM Account Where Id IN: trigger.new]){
    
        
            // Step 2: Create a new email
            Messaging.singleemailmessage mail = new Messaging.singleemailmessage();
            
            
            // step 3: set list of people who should get mail
            List<string> sendTo = new List<string>();
            sendTo.add(myAccount.Owner.email);
            system.debug('==============' + myAccount.Owner.email);
            mail.setToAddresses(sendTo);
            
            // step 4: set who the email is sent from
            mail.setReplyTo('youreamil@gmail.com ');
            mail.setSenderDisplayName('Your Name');
            
           // step 5: set email contents
           mail.setSubject('Duplicate Account is Created');
           mail.setHtmlBody('This Account' + ' ' + myaccount.name  + ' ' + 'already exists');
           
           // Add your email to the master list
           mails.add(mail);
           
       
        }
        
         // send all emails in the master list
           Messaging.sendEmail(mails);
        
        system.debug('=====Mail is Sent==========');
        
        
    }
    
  

 }


        
Thanks.
Sukanya BanekarSukanya Banekar

Hi Soubhagya,

Following is the trigger and its controller which will send an email if duplicate account is create in your org.

Apex Trigger

trigger test on Account (before update ) {
   AccController cntr= new AccController();
   cntr.sendMailToOwner(trigger.new);
}
 
Apex Class

public with sharing class AccController{
    public void sendMailToOwner(List<Account> lstAccount){
        set<String> setAccountName= new set<String> ();
        list<Account> lstAccountDup = new list<Account>();
        for(Account objAcc: [select Name from Account]){
            setAccountName.add(objAcc.Name);
        }
        if(setAccountName <> NULL && !setAccountName.isEmpty()){
           for(Account objAccDup: lstAccount) {
               if(setAccountName.contains(objAccDup.Name)){
                   lstAccountDup.add(objAccDup);
               }
           }
        }// end if
        if(lstAccountDup <> NULL && !lstAccountDup.isEmpty()){
            sendEmail(lstAccountDup );
        }
    }
    
    public void sendEmail(list<Account> lstDupAccount){
        list<Messaging.singleEmailMessage> lstMail= new list<Messaging.singleEmailMessage>();
        Messaging.singleEmailMessage mail= new Messaging.singleEmailMessage();
        List<string> listOwnerEmail;
        for(Account objAcc: lstDupAccount){
            listOwnerEmail= new List<string>();
            if(objAcc.Owner.Email <> NULL){
                listOwnerEmail.add(objAcc.Owner.Email);
                mail.setToAddresses(listOwnerEmail);
                mail.setSubject('Duplicate Account is Created');
                mail.setHtmlBody('Hi,'+objAcc.Owner +'<br/><br/> Your organisation has found duplicate Accounts named as:\n' +objAcc.Name+'\n');
                lstMail.add(mail);
            }          
        }
        if(lstMail<> NULL && !lstMail.isEmpty()){
            Messaging.sendEmail(lstMail); 
        }
    }
}
 

I hope this will help you.

Thanks,
Sukanya