• y s reddy
  • NEWBIE
  • 0 Points
  • Member since 2013

  • Chatter
    Feed
  • 0
    Best Answers
  • 0
    Likes Received
  • 0
    Likes Given
  • 1
    Questions
  • 0
    Replies

public PageReference OnSendEmail()
{
//string strOWEA = '';
string ContactId = apexpages.currentpage().getparameters().get('ContactId');
PageReference redirect = new PageReference('/apex/enterdetailspage');
redirect.setRedirect(true);
try
{
if(dateStatusReport.Item_Date__c == null)
{
ApexPages.addMessage(new ApexPages.message(ApexPages.severity.INFO,'Room Expenditure Date field can not be blank.Please enter a Date.'));
return null;
}
if(lstTimesLog.size()==0){
ApexPages.addMessage(new ApexPages.message(ApexPages.severity.INFO,'There is no data for send status report.'));
return null;
}
else
{
lstContact = new list<contact>();
set<id> setemailAddress = new set<id>();
list<string> lstemailAddress = new list<string>();
setPMId = new set<string>();
// setTLId = new set<string>();
setRMId = new set<string>();

if(ContactId !=null)
lstContact = [select id,Name,ReportsTo.Email,Email from Contact where Id =: ContactId];
system.debug('xxxxxxxxxxxxxxxxxxxxxxx'+lstContact);

lstTimesLog = [SELECT id,Name,Contact__r.Name,Item_Date__c,Item_Names__c,Session__c,Amount__c,Contact__r.ReportsTo.Email,Contact__r.Email
FROM Item__c
WHERE Contact__c = : ContactId AND Item_Date__c =: dateStatusReport.Item_Date__c
ORDER BY Item_Date__c DESC];

string strDate;
string strContactName;
string HtmlBody = 'Daily Room Expediture';
if(lstTimesLog.size()>0)
{
for(Item__c testTimesLog : lstTimesLog)
{
Datetime TLDate = testTimesLog.Item_Date__c;
strDate = TLDate.format('dd/MM/yyyy');

strContactName = testTimesLog.Contact__r.Name;
system.debug('strContactName****'+strContactName);
}
}

HtmlBody += '<br></br>';
//HtmlBody += '<br><br><br><br><b>Daily Status Report of '+strContactName+' for '+strDate+'</b></br></br></br></br>';
HtmlBody += '<br><br><br><br><b>'+ lstContact[0].Name +' - Daily Status Report - '+'['+strDate+']'+'</b></br></br></br></br>';
HtmlBody += '<br></br>';
HtmlBody += '<br><body><table border= \"3\" width= \"100%\" cellspacing=\"0\" cellpadding=\"10\" style><tr style=\"background-color:#6D7579;color:white;\" ALIGN=\"CENTER\"><th>Item Date</th><th>Item Name</th><th>Session</th><th>Amount</th></tr></br>';


if(lstContact.size()>0)
{
for(Contact eachContact : lstContact)
{
Totalamount = 0;

updateContact = new list<Contact>();
updateContact.add(eachContact);

if(lstTimesLog.size()>0)
{
for(Item__c objTimeLog : lstTimesLog)
{

if(objTimeLog.Contact__r.Email !=null)
{
if(setPMId.contains(objTimeLog.Contact__r.Email)){
setPMId.add(objTimeLog.Contact__r.Email);
}
else
{
lstemailAddress.add(objTimeLog.Contact__r.Email);
}
}
if(eachContact.ReportsTo.Email !=null)
{
if(setRMId.contains(eachContact.ReportsTo.Email)){
setRMId.add(eachContact.ReportsTo.Email);
}
else
{
lstemailAddress.add(eachContact.ReportsTo.Email);
}
}

if(OtherEmail != null && OtherEmail != '')
{
lstemailAddress.add(OtherEmail);
}

Totalamount += objTimeLog.Amount__c;

Datetime TLDate = objTimeLog.Item_Date__c;
string RoomExpenditureDate= TLDate.format('dd/MM/yyyy');

// HtmlBody+='<tr ALIGN=\"CENTER\"><td> '+Room Expenditure Date+' </td><td>'+objTimeLog.Item_Date__c+'</td><td>'+objTimeLog.Item_Names__c+'</td><td>'+objTimeLog.Session__c+'</td><td>'+objTimeLog.Amount__c+'</td></tr>';
HtmlBody+='<tr ALIGN=\"CENTER\"><td>'+RoomExpenditureDate+'</td><td>'+objTimeLog.Item_Names__c+'</td><td>'+objTimeLog.Session__c+'</td><td>'+objTimeLog.Amount__c+'</td></tr>';


}

}

HtmlBody +='<tr ALIGN=\"LEFT\"><td COLSPAN=\"7\"><b>Total Amount in (Rs/-) : '+Totalamount+'</b></td></tr>';
HtmlBody +='</table></body>';
HtmlBody += '<br></br>';
HtmlBody +='<b>Note : </b> This is an unmonitored mailbox. Please do not reply back on this email address.';

system.debug('lstemailAddress******'+lstemailAddress);
lstemailAddress.clear();
for(wrapListForEmail s :lstEmailToAddress){
if(s.email != null){
lstemailAddress.add(s.email);
}
}

//Messaging.MassEmailMessage mail = new Messaging.MassEmailMessage ();
Messaging.SingleEmailMessage mail = new Messaging.SingleEmailMessage();
// if(strOWEA !=null)
// mail.setOrgWideEmailAddressId(strOWEA);
// mail.setToAddresses(lstemailAddress);
String[] emailToAddress = new String[] {lstContact[0].Email};
mail.setToAddresses(emailToAddress);
String[] emailCCAddress = new String[] {lstContact[0].ReportsTo.Email};
mail.setCcAddresses(emailCCAddress);
mail.setSubject(lstContact[0].Name +' - Daily Status Report - '+'['+strDate+']');
mail.setHtmlBody(HtmlBody);
Messaging.sendEmail(new Messaging.SingleEmailMessage[] {mail});
lstemailAddress.clear();
}
}
return redirect;
}
return null;
}

catch(Exception e)
{
ApexPages.addMessage(new ApexPages.message(ApexPages.severity.INFO,e.getMessage()));
return null;
}
}