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 

email not sending to user

email not sent to user .

Trigger : 

trigger checkDuplicateValue on Account (after insert)
        //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 :
        List<Account> accList = [Select ID, owner.Id, Name, fax, phone 
                                 From Account 
                                 Where Id in :accIdSet];
            //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 :
                String accName = acc.Name;
                    //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;

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

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 =;" .
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 =; .. but it is not working ..