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
jameskCAjameskCA 

Help relating multiple collections for Email class

I'm currently creating a class that will be called through the apex scheduler.  It will be sending an email using a template.  The email is based on our custom project object which has lookup fields to the salesrep (user) and customer(person account).  I currently having it working with an initial soql statment to get all the proejcts that meet the criteria for the email.
 
public List<Project__c> getCommissionings(){
        //Sales_Rep__c
        //Id, Name, SenderEmail
        List<Project__c> projsWithComm = 
            [SELECT Id, Commissioning_Passed__c, Customer_Email__c, Sales_Rep__c, Customer__c
             FROM Project__c 
             WHERE Disable_Commissioning_Passed_Alert__c != true 
             //AND Commissioning_Passed__c = LAST_N_DAYS:60
        ];
        
        return projsWithComm;
    }

However, I can't use Customer__c in the setTargetObjectId, I need to do an additional account query to get user fields for the signature of the email based on the Sales_Rep__c.  I'm currently doing those in the actual project for loop as I can't figure out a better way to do it.  
 
public void sendEmail(List<Project__c> projsWithComm){
        // First, reserve email capacity for the current Apex transaction to ensure
        // that we won't exceed our daily email limits when sending email after
        // the current transaction is committed.
        Messaging.reserveSingleEmailCapacity(projsWithComm.size());
        
        for(Project__c p : projsWithComm){

            User salesRep = [SELECT Id, Name, SenderEmail From User WHERE Id = : p.Sales_Rep__c];
            Account a = [Select PersonContactId From Account Where Id = :p.Customer__c];

        	// Now create a new single email message object
        	// that will send out a single email to the addresses in the To, CC & BCC list.
        	Messaging.SingleEmailMessage mail = new Messaging.SingleEmailMessage();
            
            mail.setTemplateId('00X15000000kK6z123');
        	// Strings to hold the email addresses to which you are sending the email.
        	// TODO: Change this address
            String[] toAddresses = new String[] { p.Customer_Email__c };
     
        
        	// Assign the addresses for the To and CC lists to the mail object.
        	mail.setToAddresses(toAddresses);
        
        	// Specify the address used when the recipients reply to the email. 
            mail.setReplyTo( salesRep.SenderEmail );
        
        	// Specify the name used as the display name.
        	mail.setSenderDisplayName('Salesforce Support');
        
        	// Specify the subject line for your email address.
        	//mail.setSubject('Hi! Following up on your commissioning!');
        
        	// Set to True if you want to BCC yourself on the email.
        	mail.setBccSender(true);
            mail.saveAsActivity = false;
            system.debug('CUSTOMER: '+p.Customer__c);
            mail.setTargetObjectId(a.PersonContactId); // Account
 
            mail.setWhatId(p.Id);
			//mail.setTargetObjectId();
            //mail
        	        

	        // Send the email you have created.
        	Messaging.sendEmail(new Messaging.SingleEmailMessage[] { mail });
        }

Any ideas on how to do all the SOQL querying before the project loop?  If so, how do I specify the results from the current project loop iteration?  Thanks in advance.  
jameskCAjameskCA
Can't seem to edit my post so just to clarify, I need to do additional SOQL queries to get the contactid from the account and sales rep frields from the user object.