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 

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
Agustina GarciaAgustina Garcia
Hi,

Below code is just a structure. You would need to implement the trigger, but the email call is done.

This would be the trigger:
trigger checkDuplicateValue on Account (after insert)
{
	if(Trigger.isAfter){
        if(Trigger.isInsert)
        { 
        	//your code to check if this is the new record has same value a
        	Boolean duplicateValue = false;

        	//Your code to know if the value is duplicated

        	if(duplicateValue)
        	{   
        		String userEmail = accountOwnerEmail; //You need to get also this info
            	EmailHelper.sendEmail(userEmail);
			}
        }
    }
}
And this the email:
 
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 this helps
Agustina
 
Soubhagya Ranjan 2Soubhagya Ranjan 2
Hi 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 });
    }
}
Agustina GarciaAgustina Garcia
Your main issue is that the owner email is not on Owner.Name field. You would need to use this Owner.Name on User table and retrieve the Email. In addition I have enhanced the trigger in order to bukify it. But that last enhancement is not the reason of your failure.

Hope below code works for you:
 
trigger checkDuplicateValue on Account (after insert)
{
    if(Trigger.isAfter)
    {   
        //Get list of Users
		Map<Id,User> userMap = new Map<Id,User>([SELECT Name, Email FROM User]);
        
        //Get all Ids in order to do a query
        //and bulkify the trigger
        Set<Id> accIdSet = new Set<Id>();
    	for(Account a : Trigger.new)
        {
            accIdSet.add(a.Id);
        }
        
        List<Account> accList = [Select ID, owner.Id, Name, fax, phone 
                                 From Account 
                                 Where Id in :accIdSet];
        
        if(!accList.isEmpty())
        {
			//Create a map with the Name and the Owner Id
			//With the Owner Id I can look for the User in the first Map
			Map<String, Id> accMap = new Map<String, Id>();
            for(Account acc : accList)
            {
                accMap.put(acc.Name, acc.owner.Id);
            }
            
            for(Account acc : Trigger.new)
            {
                String accName = acc.Name;
                if(accMap.containsKey(accName))
                {
                    //Double check the name for the new account
                    Id userId = accMap.get(accName);
					acc.adderror('Account already exists in your Organization with name ' + acc.Name);
					
                    //Retrieve the owner's email
                    String theEmail = userMap.get(userId).Email;
					EmailHelper.sendEmail(theEmail);
                }
            }
        }
    }
}

Agustina