Skip to main content
2 of 3
added 759 characters in body
Mahmood
  • 6k
  • 5
  • 39
  • 62

You did not query the Managerid in your first SOQL, hence the error.

Replace the following

User u = [select id, Email, Name from user LIMIT 10];

With this

User u = [select id, Email, Manager__c Name from user LIMIT 10];

Efficiency check

On the efficiency grounds, you have not fully mentioned your requirements, but you could use something like the code given below to make your stuff more readable and better:

User userwithManger = [select id, Email, Name, Manager__r.Id from user LIMIT 10];
// You dont need to use the following soql. User with his/her manager id could be retrieved using single soql. more effcient.
// User u1 =[select id, Name, Manager__r.id from user where Id  =:u.Manager__r.id];
Messaging.MassEmailMessage mail = new Messaging.MassEmailMessage();
List<String> sendTo = new List<String>();
sendTo.add(userwithManger.Id);  // id of the user
sendTo.add(userwithManger.Manager__r.Id); // id of the manager
Mahmood
  • 6k
  • 5
  • 39
  • 62