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
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:{
    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, FROM Account Where Id IN:]){
            // 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>();
            system.debug('==============' +;
            // step 4: set who the email is sent from
            mail.setReplyTo(' ');
            mail.setSenderDisplayName('Your Name');
           // step 5: set email contents
           mail.setSubject('Duplicate Account is Created');
           mail.setHtmlBody('This Account' + ' ' +  + ' ' + 'already exists');
           // Add your email to the master list
         // send all emails in the master list
        system.debug('=====Mail is Sent==========');


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();
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]){
        if(setAccountName <> NULL && !setAccountName.isEmpty()){
           for(Account objAccDup: lstAccount) {
        }// 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){
                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');
        if(lstMail<> NULL && !lstMail.isEmpty()){

I hope this will help you.