Batch Class and Dynamic Create Email Template
global class batchClasAndSendDynamicEmailTemplate implements Database.Batchable<Sobject> {
set<Id> suid= new set<Id>();
set<id> sweekplanid = new set<id>();
map<id,user> mapManager = new map<id,user>();
Map<ID,List<Weekly_Plan__c>> mapUserIdToAWPList = new Map<Id,List<Weekly_Plan__c>>();
Map<ID,String> mapUserIdToManagerEmail = new Map<Id,String>();
Map<ID,String> mapManagerIdTomanager = new Map<Id,String>();
list<string> lstEmail = new list<string>();
string tempBody = '';
List<Weekly_Plan__c> listPJPWeeklyUpdate = new List<Weekly_Plan__c>();
global Database.Querylocator start(Database.Batchablecontext BC)
{
return Database.getQueryLocator([Select Is_Rejected__c,
No_of_Day_Plan__c,
Status__c,
Unique_Field__c ,
User__c,
Week_Start_date__c,
Id,
OwnerId,
Owner.Name,
Escalation__c,
(SELECT ID
FROM ProcessInstances
ORDER BY CreatedDate DESC
LIMIT 1),
Name From Weekly_Plan__c
where Status__c ='Sent for Approval'
and (Week_Start_date__c=:System.today() )]);
}
global void execute(Database.Batchablecontext BC,List<Weekly_Plan__c> listWeeklyPlan)
{
system.debug('listWeeklyPlan****'+listWeeklyPlan);
Messaging.Email[] allMails = new List<Messaging.Email>();
set<id> managerid = new set<id>();
List<Messaging.SingleEmailMessage> singleEmails=new List<Messaging.SingleEmailMessage>();
string MailBody='';
string emailBody = '';
string tempemailBody = '';
string AWPName='';
string UserName='';
string WeekStartDate;
string ManagerName ='';
if(listWeeklyPlan !=null && listWeeklyPlan.size()>0){
tempemailBody += ' <table width="100%" style="font-size:14px;font-family: Arial;"> ';
tempemailBody += '<tr>';
tempemailBody += '<td>';
tempemailBody += '<table height="100px;" width="100%" border="1px solid black;" cellspacing="0" cellpadding="0">';
tempemailBody += '<tr bgcolor="#D4D4D6">';
tempemailBody += '<td width="11%" align="center">';
tempemailBody += '<b> WEEK START DATE </b>';
tempemailBody += '</td>';
tempemailBody += '<td width="8%" align="center">';
tempemailBody += '<b> REQUESTED BY </b>';
tempemailBody += '</td>';
tempemailBody += '<td width="25%" align="center">';
tempemailBody += '<b> MANAGER </b>';
tempemailBody += '</td>';
tempemailBody += '</tr>';
List<User> listUser = [Select Manager.Email, email,UserRole.name,name,
ManagerId,isActive,
Id
From User where isActive=true];
if(listUser.size()>0)
{
for(user objuser :listUser)
{
mapManager.put(objuser.id,objuser);
}
}
set<id> setprocessinstance = new set<id>();
for(Weekly_Plan__c objWeekPlan : listWeeklyPlan)
{
sweekplanid.add(objWeekPlan.id);
//suid.add(objWeekPlan.OwnerId);
if(objWeekPlan.User__c!=null && mapManager.get(objWeekPlan.User__c)!=null && mapManager.get(objWeekPlan.User__c).managerid!=null)
{
system.debug('******::'+mapManager.get(objWeekPlan.User__c).name);
if( mapManager.get(mapManager.get(objWeekPlan.User__c).ManagerId).Managerid!=null)
{
system.debug('******::'+mapManager.get(mapManager.get(objWeekPlan.User__c).ManagerId).Managerid);
if(mapUserIdToAWPList.get(mapManager.get(mapManager.get(objWeekPlan.User__c).ManagerId).Managerid)== null)
{
mapUserIdToAWPList.put(mapManager.get(mapManager.get(objWeekPlan.User__c).ManagerId).Managerid, new List<Weekly_Plan__c>());
}
mapUserIdToAWPList.get(mapManager.get(mapManager.get(objWeekPlan.User__c).ManagerId).Managerid).add(objWeekPlan);
}
else if(mapManager.get(mapManager.get(objWeekPlan.User__c).ManagerId).UserRole.name.contains('Director'))
{
system.debug('#####::'+mapManager.get(mapManager.get(objWeekPlan.User__c).ManagerId));
if(mapUserIdToAWPList.get(mapManager.get(objWeekPlan.User__c).ManagerId)== null)
{
mapUserIdToAWPList.put(mapManager.get(objWeekPlan.User__c).ManagerId,new List<Weekly_Plan__c>());
}
mapUserIdToAWPList.get(mapManager.get(objWeekPlan.User__c).ManagerId).add(objWeekPlan);
}
}
if(objWeekPlan.ProcessInstances!=null )
{
setprocessinstance.add(objWeekPlan.ProcessInstances[0].id);
}
}
system.debug('setprocessinstance:'+setprocessinstance);
map<id,id> mapProcessInstanceworkitem = new map<id,id>();
if(setprocessinstance.size()>0)
{
for(ProcessInstanceWorkitem obj :[Select Id,ProcessInstanceId from ProcessInstanceWorkitem where ProcessInstanceId in:setprocessinstance order by Createddate desc ])
{
if(mapProcessInstanceworkitem.get(obj.ProcessInstanceId)==null)
{
mapProcessInstanceworkitem.put(obj.ProcessInstanceId,obj.id);
}
}
}
system.debug('mapProcessInstanceworkitem:'+mapProcessInstanceworkitem);
system.debug('$$$$:'+mapUserIdToAWPList);
system.debug('$$$$:'+mapUserIdToAWPList.size());
for(Id eachId : mapUserIdToAWPList.keySet())
{
lstEmail = new List<String>();
List<String> lstCCEmail = new List<String>();
emailBody = '';
if(mapManager.get(eachId).email!=null)
{
lstEmail.add(mapManager.get(eachId).email);
emailBody += '<html><body> Dear ' + mapManager.get(eachId).name+',<br><br>Please find the list of the following AWP, auto approved from the system due to the manager didn’t approve the AWP for their team member<br><br> ';
}
else
{
system.debug('Continue::'+eachId);
continue;
}
emailBody += '<html><body> Please take necessary action.<br><br> ';
emailBody += ' <div style="border:1px solid black;width:95%;">';
//string strName = mapManager.get(mapManager.get(mapManager.get(eachId).ManagerId).Managerid).Name;
//system.debug('strName'+strName);
Messaging.SingleEmailMessage mail = new Messaging.SingleEmailMessage();
tempBody = '';
List<Weekly_Plan__c>ListOfPJPweeklyPlan = mapUserIdToAWPList.get(eachId);
system.debug('ListOfPJPweeklyPlan###'+ListOfPJPweeklyPlan);
for(Weekly_Plan__c objPWP :ListOfPJPweeklyPlan){
if(objPWP.User__c !=null)
{
if(mapManager.get(objPWP.User__c) !=null)
{
if(mapManager.get(mapManager.get(objPWP.User__c).ManagerId )!=null && mapManager.get(mapManager.get(objPWP.User__c).ManagerId).email!=null)
{
lstCCEmail.add( mapManager.get(mapManager.get(objPWP.User__c).ManagerId).email);
ManagerName =mapManager.get(mapManager.get(objPWP.User__c).ManagerId).name;
system.debug('lstEmail---'+lstCCEmail);
}
}
}
if(objPWP.Name !='')
AWPName = objPWP.Name;
if(objPWP.User__c !=null && mapManager.get(objPWP.User__c)!=null )
UserName = mapManager.get(objPWP.User__c).Name;
if(objPWP.Week_Start_date__c !=null)
WeekStartDate = objPWP.Week_Start_date__c.format();
tempBody += '<tr style="border:1px solid black;border-collapse:collapse;">';
tempBody += '<td width="11%" align="center">';
tempBody += '' + WeekStartDate + '';
tempBody += '</td>';
tempBody += '<td width="8%" align="center">';
tempBody += '' + UserName + '';
tempBody += '</td>';
tempBody += '<td width="25%" align="center">';
tempBody += '' + ManagerName+ '';
tempBody += '</td>';
tempBody += '</tr>';
}
emailBody +=tempemailBody;
emailBody += tempBody;
emailBody += '</table> ';
emailBody += '</table> ';
emailBody += '</div> ';
emailBody += '</body></html>' ;
system.debug('emailBody@@ '+emailBody);
system.debug('lstEmail###@@ '+lstEmail);
if(lstEmail !=null && lstEmail.size()>0)
{
mail.setHtmlBody(emailBody);
mail.setsubject('Escalation : AWP Approved by system');
mail.setToAddresses(lstEmail);
mail.setccAddresses(lstCCEmail);
system.debug('mail###'+mail);
singleEmails.add(mail);
system.debug('singleEmails###'+singleEmails);
}
}
list<Approval.ProcessWorkitemRequest> lstAR = new list<Approval.ProcessWorkitemRequest>();
for(Weekly_Plan__c objWeekPlan : listWeeklyPlan){
if(objWeekPlan.ProcessInstances!=null && mapProcessInstanceworkitem.get(objWeekPlan.ProcessInstances[0].id)!=null )
{
system.debug('>>>:'+mapProcessInstanceworkitem.get(objWeekPlan.ProcessInstances[0].id));
Approval.ProcessWorkitemRequest req = new Approval.ProcessWorkitemRequest();
req.setComments('Approved by system');
req.setAction('Approve');
req.setWorkitemId(mapProcessInstanceworkitem.get(objWeekPlan.ProcessInstances[0].id));
lstAR.add(req);
objWeekPlan.Status__c = 'Approved';
objWeekPlan.Escalation__c = true;
listPJPWeeklyUpdate.add(objWeekPlan);
}
system.debug('listPJPWeeklyUpdate%%%%'+listPJPWeeklyUpdate);
}
system.debug('KKKKKKKKKK:'+lstAR.size());
if(lstAR.size()>0)
{
list<Approval.ProcessResult> lstresult = Approval.process(lstAR);
}
if(listPJPWeeklyUpdate !=null && listPJPWeeklyUpdate.size()>0){
update listPJPWeeklyUpdate;
system.debug('listPJPWeeklyUpdate@@@%%%%'+listPJPWeeklyUpdate);
}
if(singleEmails !=null && singleEmails.size()>0)
{
Messaging.SendEmail(singleEmails);
}
}
}
global void finish(Database.Batchablecontext BC)
{
}
}
}
set<Id> suid= new set<Id>();
set<id> sweekplanid = new set<id>();
map<id,user> mapManager = new map<id,user>();
Map<ID,List<Weekly_Plan__c>> mapUserIdToAWPList = new Map<Id,List<Weekly_Plan__c>>();
Map<ID,String> mapUserIdToManagerEmail = new Map<Id,String>();
Map<ID,String> mapManagerIdTomanager = new Map<Id,String>();
list<string> lstEmail = new list<string>();
string tempBody = '';
List<Weekly_Plan__c> listPJPWeeklyUpdate = new List<Weekly_Plan__c>();
global Database.Querylocator start(Database.Batchablecontext BC)
{
return Database.getQueryLocator([Select Is_Rejected__c,
No_of_Day_Plan__c,
Status__c,
Unique_Field__c ,
User__c,
Week_Start_date__c,
Id,
OwnerId,
Owner.Name,
Escalation__c,
(SELECT ID
FROM ProcessInstances
ORDER BY CreatedDate DESC
LIMIT 1),
Name From Weekly_Plan__c
where Status__c ='Sent for Approval'
and (Week_Start_date__c=:System.today() )]);
}
global void execute(Database.Batchablecontext BC,List<Weekly_Plan__c> listWeeklyPlan)
{
system.debug('listWeeklyPlan****'+listWeeklyPlan);
Messaging.Email[] allMails = new List<Messaging.Email>();
set<id> managerid = new set<id>();
List<Messaging.SingleEmailMessage> singleEmails=new List<Messaging.SingleEmailMessage>();
string MailBody='';
string emailBody = '';
string tempemailBody = '';
string AWPName='';
string UserName='';
string WeekStartDate;
string ManagerName ='';
if(listWeeklyPlan !=null && listWeeklyPlan.size()>0){
tempemailBody += ' <table width="100%" style="font-size:14px;font-family: Arial;"> ';
tempemailBody += '<tr>';
tempemailBody += '<td>';
tempemailBody += '<table height="100px;" width="100%" border="1px solid black;" cellspacing="0" cellpadding="0">';
tempemailBody += '<tr bgcolor="#D4D4D6">';
tempemailBody += '<td width="11%" align="center">';
tempemailBody += '<b> WEEK START DATE </b>';
tempemailBody += '</td>';
tempemailBody += '<td width="8%" align="center">';
tempemailBody += '<b> REQUESTED BY </b>';
tempemailBody += '</td>';
tempemailBody += '<td width="25%" align="center">';
tempemailBody += '<b> MANAGER </b>';
tempemailBody += '</td>';
tempemailBody += '</tr>';
List<User> listUser = [Select Manager.Email, email,UserRole.name,name,
ManagerId,isActive,
Id
From User where isActive=true];
if(listUser.size()>0)
{
for(user objuser :listUser)
{
mapManager.put(objuser.id,objuser);
}
}
set<id> setprocessinstance = new set<id>();
for(Weekly_Plan__c objWeekPlan : listWeeklyPlan)
{
sweekplanid.add(objWeekPlan.id);
//suid.add(objWeekPlan.OwnerId);
if(objWeekPlan.User__c!=null && mapManager.get(objWeekPlan.User__c)!=null && mapManager.get(objWeekPlan.User__c).managerid!=null)
{
system.debug('******::'+mapManager.get(objWeekPlan.User__c).name);
if( mapManager.get(mapManager.get(objWeekPlan.User__c).ManagerId).Managerid!=null)
{
system.debug('******::'+mapManager.get(mapManager.get(objWeekPlan.User__c).ManagerId).Managerid);
if(mapUserIdToAWPList.get(mapManager.get(mapManager.get(objWeekPlan.User__c).ManagerId).Managerid)== null)
{
mapUserIdToAWPList.put(mapManager.get(mapManager.get(objWeekPlan.User__c).ManagerId).Managerid, new List<Weekly_Plan__c>());
}
mapUserIdToAWPList.get(mapManager.get(mapManager.get(objWeekPlan.User__c).ManagerId).Managerid).add(objWeekPlan);
}
else if(mapManager.get(mapManager.get(objWeekPlan.User__c).ManagerId).UserRole.name.contains('Director'))
{
system.debug('#####::'+mapManager.get(mapManager.get(objWeekPlan.User__c).ManagerId));
if(mapUserIdToAWPList.get(mapManager.get(objWeekPlan.User__c).ManagerId)== null)
{
mapUserIdToAWPList.put(mapManager.get(objWeekPlan.User__c).ManagerId,new List<Weekly_Plan__c>());
}
mapUserIdToAWPList.get(mapManager.get(objWeekPlan.User__c).ManagerId).add(objWeekPlan);
}
}
if(objWeekPlan.ProcessInstances!=null )
{
setprocessinstance.add(objWeekPlan.ProcessInstances[0].id);
}
}
system.debug('setprocessinstance:'+setprocessinstance);
map<id,id> mapProcessInstanceworkitem = new map<id,id>();
if(setprocessinstance.size()>0)
{
for(ProcessInstanceWorkitem obj :[Select Id,ProcessInstanceId from ProcessInstanceWorkitem where ProcessInstanceId in:setprocessinstance order by Createddate desc ])
{
if(mapProcessInstanceworkitem.get(obj.ProcessInstanceId)==null)
{
mapProcessInstanceworkitem.put(obj.ProcessInstanceId,obj.id);
}
}
}
system.debug('mapProcessInstanceworkitem:'+mapProcessInstanceworkitem);
system.debug('$$$$:'+mapUserIdToAWPList);
system.debug('$$$$:'+mapUserIdToAWPList.size());
for(Id eachId : mapUserIdToAWPList.keySet())
{
lstEmail = new List<String>();
List<String> lstCCEmail = new List<String>();
emailBody = '';
if(mapManager.get(eachId).email!=null)
{
lstEmail.add(mapManager.get(eachId).email);
emailBody += '<html><body> Dear ' + mapManager.get(eachId).name+',<br><br>Please find the list of the following AWP, auto approved from the system due to the manager didn’t approve the AWP for their team member<br><br> ';
}
else
{
system.debug('Continue::'+eachId);
continue;
}
emailBody += '<html><body> Please take necessary action.<br><br> ';
emailBody += ' <div style="border:1px solid black;width:95%;">';
//string strName = mapManager.get(mapManager.get(mapManager.get(eachId).ManagerId).Managerid).Name;
//system.debug('strName'+strName);
Messaging.SingleEmailMessage mail = new Messaging.SingleEmailMessage();
tempBody = '';
List<Weekly_Plan__c>ListOfPJPweeklyPlan = mapUserIdToAWPList.get(eachId);
system.debug('ListOfPJPweeklyPlan###'+ListOfPJPweeklyPlan);
for(Weekly_Plan__c objPWP :ListOfPJPweeklyPlan){
if(objPWP.User__c !=null)
{
if(mapManager.get(objPWP.User__c) !=null)
{
if(mapManager.get(mapManager.get(objPWP.User__c).ManagerId )!=null && mapManager.get(mapManager.get(objPWP.User__c).ManagerId).email!=null)
{
lstCCEmail.add( mapManager.get(mapManager.get(objPWP.User__c).ManagerId).email);
ManagerName =mapManager.get(mapManager.get(objPWP.User__c).ManagerId).name;
system.debug('lstEmail---'+lstCCEmail);
}
}
}
if(objPWP.Name !='')
AWPName = objPWP.Name;
if(objPWP.User__c !=null && mapManager.get(objPWP.User__c)!=null )
UserName = mapManager.get(objPWP.User__c).Name;
if(objPWP.Week_Start_date__c !=null)
WeekStartDate = objPWP.Week_Start_date__c.format();
tempBody += '<tr style="border:1px solid black;border-collapse:collapse;">';
tempBody += '<td width="11%" align="center">';
tempBody += '' + WeekStartDate + '';
tempBody += '</td>';
tempBody += '<td width="8%" align="center">';
tempBody += '' + UserName + '';
tempBody += '</td>';
tempBody += '<td width="25%" align="center">';
tempBody += '' + ManagerName+ '';
tempBody += '</td>';
tempBody += '</tr>';
}
emailBody +=tempemailBody;
emailBody += tempBody;
emailBody += '</table> ';
emailBody += '</table> ';
emailBody += '</div> ';
emailBody += '</body></html>' ;
system.debug('emailBody@@ '+emailBody);
system.debug('lstEmail###@@ '+lstEmail);
if(lstEmail !=null && lstEmail.size()>0)
{
mail.setHtmlBody(emailBody);
mail.setsubject('Escalation : AWP Approved by system');
mail.setToAddresses(lstEmail);
mail.setccAddresses(lstCCEmail);
system.debug('mail###'+mail);
singleEmails.add(mail);
system.debug('singleEmails###'+singleEmails);
}
}
list<Approval.ProcessWorkitemRequest> lstAR = new list<Approval.ProcessWorkitemRequest>();
for(Weekly_Plan__c objWeekPlan : listWeeklyPlan){
if(objWeekPlan.ProcessInstances!=null && mapProcessInstanceworkitem.get(objWeekPlan.ProcessInstances[0].id)!=null )
{
system.debug('>>>:'+mapProcessInstanceworkitem.get(objWeekPlan.ProcessInstances[0].id));
Approval.ProcessWorkitemRequest req = new Approval.ProcessWorkitemRequest();
req.setComments('Approved by system');
req.setAction('Approve');
req.setWorkitemId(mapProcessInstanceworkitem.get(objWeekPlan.ProcessInstances[0].id));
lstAR.add(req);
objWeekPlan.Status__c = 'Approved';
objWeekPlan.Escalation__c = true;
listPJPWeeklyUpdate.add(objWeekPlan);
}
system.debug('listPJPWeeklyUpdate%%%%'+listPJPWeeklyUpdate);
}
system.debug('KKKKKKKKKK:'+lstAR.size());
if(lstAR.size()>0)
{
list<Approval.ProcessResult> lstresult = Approval.process(lstAR);
}
if(listPJPWeeklyUpdate !=null && listPJPWeeklyUpdate.size()>0){
update listPJPWeeklyUpdate;
system.debug('listPJPWeeklyUpdate@@@%%%%'+listPJPWeeklyUpdate);
}
if(singleEmails !=null && singleEmails.size()>0)
{
Messaging.SendEmail(singleEmails);
}
}
}
global void finish(Database.Batchablecontext BC)
{
}
}
}
Comments
Post a Comment