Skip to main content
3 of 3
added 341 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__c 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__c); // id of the manager

P.S. I don't understand why are you adding Id to the sendTo list, I think it should have been email but may be you have some other requirement. Secondly, Manager is out-of-the-box standard User field. Manager__r means you have implemented a custom field. So the above solution is as per your requirement mentioned in the question.

Mahmood
  • 6k
  • 5
  • 39
  • 62