0

I have a requirement where i need to send the data in email body with tabular format. I used to fetch data using batch class and then written a method which used to create body of html email template. But email body data didn't come in tabular format.

Class :

global class AssetEndOfLifeBatch implements Database.Batchable, Database.stateful{

global string subject;
global string body;
//global string finalStr = 'Asset Number, Product Number, Product Name, Serial Number, Asset End Of Life\n';
global List<Asset> assetList = new List<Asset>();

global Database.QueryLocator start(Database.BatchableContext BC){
    return Database.getQueryLocator('SELECT Name, ProductCode, Product2.Name, SerialNumber, Asset_End_Of_Life__c FROM Asset WHERE  Asset_End_Of_Life__c != null');
}
global void execute(Database.BatchableContext BC, List<Asset> assetList1){
    assetList.addAll(assetList1);
    system.debug('assetList'+assetList);
}
global void finish(Database.BatchableContext BC){
    system.debug('**** Final String *** ' + assetList);

    Integer year = Date.Today().Year();
    string yearValue = string.valueOf(year);
    Messaging.SingleEmailMessage email =new Messaging.SingleEmailMessage();
    String[] toAddresses = new list<string> {'rranjan642@gmail.com'};
    String subject ='ATTENTION: ';
    email.setSubject(subject);
    email.setToAddresses( toAddresses );

    String emailBody = gethtmlBody(assetList);
    email.setHtmlBody(emailBody);
    Messaging.SendEmailResult [] r = Messaging.sendEmail(new Messaging.SingleEmailMessage[] {email});
    System.debug('**********Last Line Of The Code *********');
}

public static string gethtmlBody(List<Asset> assetList){

string tableData = '';
string htmlbody = '';
htmlbody =
'<html>'
+  '<body>'
+    '<STYLE type="text/css">'
+      'TH {font-size: 11px; font-face: arial;background: #CCCCCC;'
+           'border-width: 1;  text-align: center }'
+      'TD  {font-size: 11px; font-face: verdana }' 
+      'TABLE {border: solid #CCCCCC; border-width: 1}'
+      'TR {border: solid #CCCCCC; border-width: 1}'
+    '</STYLE>'
+    '<font face="arial" size="2">'
+      '<p>Dear Customers,</p>'
+      '<p>At #####, we understand it is essential to your long-range planning to be able to anticipate changes in your imaging systems. Therefore, we have developed a standard practice of giving you notice of any such change.</p>'
+     '<p>We are writing you today due to a change in the product lifecycle of the following systems:</p>'
+   '<p>ASSET_TABLE</p>'
+   '<p>Due to the age of this system/these systems, we will no longer be able to provide you with full service support.</p>'
+   '<p>However, because you are a valued ##### customer, we would like to offer you the SmartPath Loyalty Program, with incentives designed to help you transition to newer technology.</p>'
+   '<p>The SmartPath Loyalty Program provides a portfolio of solutions and innovations designed to optimize, enhance, and transform existing imaging system capabilities with new ##### solutions.</p>'
+   '<p>Current service agreements will continue through the scheduled expiration date, however after December 31st, CALENDAR_YEAR, if ##### determines that service coverage is hindered due to the unavailability of parts or trained personnel, or that the imaging system can no longer be maintained in a safe or effective manner as determined by #####, then ##### may terminate this Agreement pursuant to the Service Agreement Terms and Conditions. </p>'
+   '<p>##### values you as a customer and would like the opportunity to discuss this letter and your needs. Your ##### Account Manager can work with you to identify which product and service solution best fit your business requirements.</p>'
+   '<p>If you have immediate questions or concerns, please call us at <b>1-800-229-6417</b> or contact your local ##### Account Manager.</p>'
+   '<p>Sincerely,</p>'
+   '<p><b>##### </b></p>';

    //open table..
    tableData = '<table border="1" style="border-collapse: collapse"><tr><th>Asset Name</th><th>Product Number</th><th>Product Name</th><th>Serial Number</th><th>EOL Date</th></tr>';

    //iterate over list and output columns/data into table rows...
    for(Asset a : assetList){
        tableData += '<tr><td>' + a.Name + '<tr><td>' + a.ProductCode + '<tr><td>' + a.Product2.Name + '<tr><td>' + a.SerialNumber + '<tr><td>' + a.Asset_End_Of_Life__c + '</td></tr>';
    }
    system.debug('************tableData**********'+tableData);
//close table...
tableData += '</table>';
system.debug('********** tableData ************'+tableData);
htmlBody = htmlBody.replace('ASSET_TABLE', tableData);
return htmlBody
}

}

Can anyone tell is this is the right approach to get the data in tabular format or do i need to use other email template like visulaforce email template?

Thanks, Ravi

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Browse other questions tagged or ask your own question.