You need to sign in to do that
Don't have an account?
Soubhagya 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 .
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.
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.
Hi Soubhagya,
Following is the trigger and its controller which will send an email if duplicate account is create in your org.
I hope this will help you.
Thanks,
Sukanya