+ Start a Discussion
Soubhagya Ranjan 2Soubhagya Ranjan 2 

email not sending to user

email not sent to user .

Trigger : 

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);
                }
            }
        }
    }
}



Class :

public with sharing class EmailHelper
{
    public static void sendEmail(String theEmail)
    {
        Messaging.SingleEmailMessage mail = new Messaging.SingleEmailMessage();
        mail.setToAddresses(new String[] {theEmail});
        mail.setSenderDisplayName('My Email');
        mail.setSubject('My email subject');
        mail.setPlainTextBody('Process fails duplicate value');
        Messaging.sendEmail(new Messaging.SingleEmailMessage[] { mail });
    }
}

 
Sure@DreamSure@Dream
Hi Shoubagya,

in a single transaction, you can't have more than 10 calls to Messaging.sendEmail() method.

If you are inserting more than 10 accounts, you will be getting that exception .
check the debug logs, by adding a few system.debug statements.

please mark this as the solution, if it solves your problem.
Dhruv Gour 14Dhruv Gour 14
Hi ,

Please replace line    "Id userId = accMap.get(accName);" by    "Id userId = userinfo.id;" .
Soubhagya Ranjan 2Soubhagya Ranjan 2
Hi Sure@Dream 

I am trying to insert one account but still didnt get any email .
 
Soubhagya Ranjan 2Soubhagya Ranjan 2
Hi Dhruv ,

I tried with Id userId = userinfo.id; .. but it is not working ..