+ Start a Discussion
Mathew Andresen 5Mathew Andresen 5 

sending visualforce page as html body of email

Hi,

I have a list of bottling's done each day that I'm breaking up by user email and then account so each user might be multiple accounts.  The structure I went with was map<email, map <account id, bottling_lot__C>

Then my idea was to pass that map into a visualforce page's custom controller where it could then generate a custom visualforce page which I could then bring back into my main class convert from blob to string and send on it's merry way as an html email.

So my question is in two parts, one is this a reasonable way to do this, or should is there a better solution.

The second is I can't figure out how to get the visualforce page to connect to the right version of my class that has the correct map.  Normally I would pass in an Id of some sort but that won't exactly work here.  I need to pass in the map, or at least a list of some type.

Any ideas?
 
public void sendClientEmail() {
       
        
        Set<String> repSet = new set<String>();
        map<String, Map<string, Bottling_Lot__c>> repMap = new map<String, Map<string, Bottling_Lot__c>>(); // email <account Id, bottling lot>
        Map<string, Bottling_Lot__c> acctMap;
        
        // build a set of all the owner Id that will have to be emailed        
        if (botLotList.size() > 0) {
            for (Bottling_Lot__c bot:botLotList) {  
                repSet.add(bot.client_lookup__r.owner.email);
            }
            for (String s:repSet) {
                acctMap = new map<String, Bottling_lot__c>();
                for (Bottling_Lot__c bot:botLotList) { 
                    if (s == bot.client_lookup__r.owner.email) {
                        acctMap.put(bot.client_lookup__c, bot);
                    }
                 }
                repMap.put(s, acctMap);
             }
            system.debug('repMap = ' + repMap);
         }
        
        // Define the email
       
        for (String s:repSet) {
            Messaging.SingleEmailMessage email = new Messaging.SingleEmailMessage();        
            String[] toAddresses = new String[] {}; 
            String[] ccAddresses = new String[] {}; 
            toAddresses.add('mathew@terravant.com');

            blob body;
            BottlingEmail_Class botClass = new BottlingEmail_Class(repMap);
            PageReference htmlPage = page.bottlingEmail_Page;
            body = htmlPage.getContent();
            String htmlBody = body.toString();
 
			email.setToAddresses(toAddresses);
            email.setSubject('test');
            email.setHtmlBody(htmlBody);
            Messaging.SendEmailResult [] r = Messaging.sendEmail(new Messaging.SingleEmailMessage[] {email});
            
        }
      
        
        
    } // end the send email method
 
<apex:page controller="BottlingEmail_Class" >
    
    <p>
        Hello, the following items were bottled for your clients
    </p>
    
    <table>
        <tr>
        <th>Date</th>
        <th>Account</th>
        <th>Cases</th>
        </tr>
        
        <apex:repeat value="{!botLotList}" var="v">
        	<tr>
                <td>{!v.Bottling_Shift__r.Bottling_Day__r.Bottling_Date__c}</td>
                <td>{!v.client_lookup__r.name}</td>
                <td>{!v.Actual_Bottled__c}</td>
                <td>testing</td>
            
            
            </tr>
        </apex:repeat>
        
        
        
    </table>
    <p>
        end of page
    </p>
    
</apex:page>
 
public class BottlingEmail_Class {
    
    public map<String, Map<string, Bottling_Lot__c>> repMap {get; set;}  // email <account Id, bottling lot>
    public List<Bottling_Lot__c> botLotList {get; set; }
        
    
 
    
    public BottlingEmail_Class(map<String, Map<string, Bottling_Lot__c>> repMap) {
        botLotList = new List<bottling_Lot__c>();
    	this.repMap = repMap;
        for (string key :repMap.keySet()) {
            Map<String, Bottling_lot__c> acctMap = new Map<String, Bottling_Lot__c>();
            acctMap = repMap.get(key);
            for (Bottling_Lot__c lot :acctMap.values() ) {
                botLotList.add(lot);
                
            }
            
            
        }
       
        
        
    } // end the constructor

}

 
Best Answer chosen by Mathew Andresen 5
Mudasir WaniMudasir Wani
Then you need to go for visualforce template and associate a controller where you can pass the list just we pass in visualforce page.
Here is an example how to do it 

https://developer.salesforce.com/docs/atlas.en-us.pages.meta/pages/pages_email_templates_with_apex.htm

All Answers

Mudasir WaniMudasir Wani
Hello,

Create a visualforce template and then use the template  Id in your code to send the email.
Associate the controller with the VF email template to populate data.
That is best solution as per my knowledge.

https://developer.salesforce.com/docs/atlas.en-us.pages.meta/pages/pages_email_templates_creating.htm

Hope this helps
Mathew Andresen 5Mathew Andresen 5
Hi,

I'm sorry I don't understand how this would work.  I need to send an object or collection over to the controller.  Just an ID doesn't contain enough information.
Mudasir WaniMudasir Wani
Then you need to go for visualforce template and associate a controller where you can pass the list just we pass in visualforce page.
Here is an example how to do it 

https://developer.salesforce.com/docs/atlas.en-us.pages.meta/pages/pages_email_templates_with_apex.htm
This was selected as the best answer
Mathew Andresen 5Mathew Andresen 5
What I ended up doing is just created a second visualforce page and then using getcontent.   I will try out your suggestion to as well just so I can understand an alternate way of dong thigns.

Thanks,