You need to sign in to do that
Don't have an account?
![Soubhagya Ranjan 2 Soubhagya Ranjan 2](https://dfc-org-production.my.site.com/img/userprofile/default_profile_45_v2.png)
trigger to send mail when find duplicate value
while inserting records into account if the phone number is duplicate then it will show an error and send email to account owner to modify the filed value .
how to write trigger for this .
Thanks,
Soubhagya
how to write trigger for this .
Thanks,
Soubhagya
Below code is just a structure. You would need to implement the trigger, but the email call is done.
This would be the trigger: And this the email:
Hope this helps
Agustina
Please check my below code and let me know what changes i need to do ...
Trigger :
trigger AccountDuplicateTrigger on Account (before insert,before update) {
for(Account a:Trigger.new)
{
List<Account> acc=[select ID,owner.name,name,fax,phone from account where Name=:a.Name And Fax=:a.Fax And Phone=:a.Phone];
if(acc.size()>0)
{
a.adderror('Account already exists in your Organization with name '+a.name );
String userEmail = a.owner.name;
EmailHelper.sendEmail(a.owner.name);
}
}
}
Apex Class :
public with sharing class EmailHelper
{
public static void sendEmail(String userEmail)
{
Messaging.SingleEmailMessage mail = new Messaging.SingleEmailMessage();
mail.setToAddresses(new String[] {userEmail});
mail.setSenderDisplayName('My Email');
mail.setSubject('My email subject');
mail.setPlainTextBody('Process fails duplicate value');
Messaging.sendEmail(new Messaging.SingleEmailMessage[] { mail });
}
}
Hope below code works for you:
Agustina