You need to sign in to do that
Don't have an account?
Guru 91
problem in writing Test Class?
global class ApprovalDelegationsBatchable implements Database.Batchable<sObject> {
String query;
Map<String, String> emailTemplateMap;
global ApprovalDelegationsBatchable() {
query = 'Select Id, Name,'+
' Status__c,'+
' Primary_Delegate__c,'+
' Secondary_Delegate__c,'+
' User__r.Out_Of_Office__c,'+
' User__r.Secondary_Delegated_Approver_Id__c,'+
' User__r.DelegatedApproverId,'+
' Notified_Date__c,'+
' Lease_Approval__r.Comments__c,'+
' Lease_Approval__c,'+
' Lease_Approval__r.Scenario__r.Lease_Analysis__r.Assumption_Comparison__c, '+
' Lease_Approval__r.Scenario__r.Lease_Analysis__r.Opportunity__r.Name, '+
' Lease_Approval__r.Scenario__r.Lease_Analysis__r.Opportunity__r.ownerId, '+
' Lease_Approval__r.Scenario__r.Lease_Analysis__r.Opportunity__r.Account.Account_Subtype__c,'+
' Lease_Approval__r.Scenario__r.Lease_Analysis__r.Opportunity__c,'+
' Lease_Approval__r.Scenario__c';
for(String s : Schema.SObjectType.Scenario__c.fields.getMap().keySet()){
query+=',Lease_Approval__r.Scenario__r.'+s;
}
query+=' From Lease_Approval_Role__c Where Status__c = \'Awaiting Response\'';
emailTemplateMap = LeaseApprovalHelper.getLeaseApprovalTemplates();
}
global Database.QueryLocator start(Database.BatchableContext BC) {
return Database.getQueryLocator(query);
}
global void execute(Database.BatchableContext BC, List<sObject> scope) {
List<Lease_Approval_Role__c> larList = new List<Lease_Approval_Role__c>();
List<Messaging.SingleEmailMessage> emailsToSend = new List<Messaging.SingleEmailMessage>();
Set<Id> opps = new Set<Id>();
Set<Id> delegateIds = new Set<Id>();
Set<Id> dealMakerIds = new Set<Id>();
Map<Id, String> scenarioToUnitInfoMap = new Map<Id, String>();
//Map<Id, Opportunity> oppMap = new Map<Id, Opportunity>([Select Id, Name, AccountId, Account.Account_Subtype__c From Opportunity Where Id in ]);
for(sobject s : scope){
Lease_Approval_Role__c lar = (Lease_Approval_Role__c)s;
dealMakerIds.add(lar.Lease_Approval__r.Scenario__r.Lease_Analysis__r.Opportunity__r.OwnerId);
scenarioToUnitInfoMap.put(lar.Lease_Approval__r.Scenario__c, null);
if(lar.User__r.DelegatedApproverId != null && LeaseApprovalHelper.getDiffBusinessDays(lar.Notified_Date__c, System.today())>=4){
opps.add(lar.Lease_Approval__r.Scenario__r.Lease_Analysis__r.Opportunity__c);
delegateIds.add(lar.User__r.DelegatedApproverId);
if(lar.User__r.Secondary_Delegated_Approver_Id__c != null) delegateIds.add(lar.User__r.Secondary_Delegated_Approver_Id__c);
}
}
Set<Id> combinedUnits = new Set<Id>();
for(Lease_Analysis_Selected_Unit__c su : [Select Id, Scenario__c, Unit__c,Unit__r.Unit__r.Property__c, Unit__r.Unit__r.Property_Address__c, Unit__r.Unit__r.Property_Code__c, Unit__r.Unit__r.Name, Unit__r.Unit__r.Property__r.Name from Lease_Analysis_Selected_Unit__c where Scenario__c IN : scenarioToUnitInfoMap.keySet() order by Scenario__c]){
if(su.Unit__c==null){
combinedUnits.add(su.Id);
}
else{
String unitTable;
if(scenarioToUnitInfoMap.containsKey(su.Scenario__c) && scenarioToUnitInfoMap.get(su.Scenario__c)!=null){
unitTable = scenarioToUnitInfoMap.get(su.Scenario__c);
if(unitTable.contains(su.Unit__r.Unit__r.Property__r.Name)){
unitTable += '<tr><td>'+su.Unit__r.Unit__r.Name+' - ' + su.Unit__r.Unit__r.Property_Code__c + '</td></tr>';
}
else{
unitTable += '<br/><table> <tr><td>'+su.Unit__r.Unit__r.Property__r.Name+'</td></tr>'+
'<tr><td>'+su.Unit__r.Unit__r.Property_Address__c+'</td></tr>'+
'<tr><td>'+su.Unit__r.Unit__r.Name+' - ' + su.Unit__r.Unit__r.Property_Code__c + '</td></tr>';
}
scenarioToUnitInfoMap.put(su.Scenario__c, unitTable);
}
else{
unitTable = '<table> <tr><td>'+su.Unit__r.Unit__r.Property__r.Name+'</td></tr>'+
'<tr><td>'+su.Unit__r.Unit__r.Property_Address__c+'</td></tr>'+
'<tr><td>'+su.Unit__r.Unit__r.Name+' - ' + su.Unit__r.Unit__r.Property_Code__c + '</td></tr>';
scenarioToUnitInfoMap.put(su.Scenario__c, unitTable);
}
}
}
for(Combined_Unit__c su : [Select Id, Unit_Opportunity__r.Unit__r.Property_Code__c, Selected_Unit__r.Scenario__c, Unit_Opportunity__r.Unit__r.Name, Unit_Opportunity__r.Unit__r.Property__r.Name, Unit_Opportunity__r.Unit__r.Property_Address__c from Combined_Unit__c where Selected_Unit__c IN : combinedUnits]){
String unitTable;
if(scenarioToUnitInfoMap.containsKey(su.Selected_Unit__r.Scenario__c) && scenarioToUnitInfoMap.get(su.Selected_Unit__r.Scenario__c)!=null){
unitTable = scenarioToUnitInfoMap.get(su.Selected_Unit__r.Scenario__c);
if(unitTable.contains(su.Unit_Opportunity__r.Unit__r.Property__r.Name)){
unitTable += '<tr><td>'+su.Unit_Opportunity__r.Unit__r.Name+' - ' + su.Unit_Opportunity__r.Unit__r.Property_Code__c + '</td></tr>';
}
else{
unitTable += '<br/><table> <tr><td>'+su.Unit_Opportunity__r.Unit__r.Property__r.Name+'</td></tr>'+
'<tr><td>'+su.Unit_Opportunity__r.Unit__r.Property_Address__c+'</td></tr>'+
'<tr><td>'+su.Unit_Opportunity__r.Unit__r.Name+' - ' + su.Unit_Opportunity__r.Unit__r.Property_Code__c + '</td></tr>';
}
scenarioToUnitInfoMap.put(su.Selected_Unit__r.Scenario__c, unitTable);
}
else{
unitTable = '<table> <tr><td>'+su.Unit_Opportunity__r.Unit__r.Property__r.Name+'</td></tr>'+
'<tr><td>'+su.Unit_Opportunity__r.Unit__r.Property_Address__c+'</td></tr>'+
'<tr><td>'+su.Unit_Opportunity__r.Unit__r.Name+' - ' + su.Unit_Opportunity__r.Unit__r.Property_Code__c + '</td></tr>';
scenarioToUnitInfoMap.put(su.Selected_Unit__r.Scenario__c, unitTable);
}
}
for(String s : scenarioToUnitInfoMap.values()){
s+='</table>';
}
Map<Id, Opportunity> oppMap = new Map<Id, Opportunity>([Select Id, Name, AccountId, Account.Account_Subtype__c From Opportunity Where Id in :opps]);
Map<Id, Opportunity> larToOppMap = new Map<Id, Opportunity>();
System.debug(delegateIds);
Map<Id, User> delegates = new Map<Id, User>([Select Id, Name, Email From User where Id IN :delegateIds]);
Map<Id, User> dealMakerMap = new Map<Id, User>([Select Id, Name From User where Id IN :dealMakerIds]);
for(sobject s : scope){
Lease_Approval_Role__c lar = (Lease_Approval_Role__c)s;
larToOppMap.put(lar.Id, oppMap.get(lar.Lease_Approval__r.Scenario__r.Lease_Analysis__r.Opportunity__c));
System.debug('DAYS: '+LeaseApprovalHelper.getDiffBusinessDays(lar.Notified_Date__c, System.today()));
if(lar.User__r.DelegatedApproverId != null && LeaseApprovalHelper.getDiffBusinessDays(lar.Notified_Date__c, System.today())>=4){ //Nothing to do if no delegates for user or if notified less than 4 business days ago
if(lar.User__r.Out_Of_Office__c){
if(lar.User__r.Secondary_Delegated_Approver_Id__c != null){
if(lar.Secondary_Delegate__c == null){
lar.Secondary_Delegate__c = lar.User__r.Secondary_Delegated_Approver_Id__c;
larList.add(lar);
}
//send email
Messaging.SingleEmailMessage mail = LeaseApprovalHelper.createApprovalActionNeededEmail(emailTemplateMap.get('Approver_Action_Needed'),
//delegates.get(lar.User__r.Secondary_Delegated_Approver_Id__c).Name,
dealMakerMap.get(lar.Lease_Approval__r.Scenario__r.Lease_Analysis__r.Opportunity__r.ownerId).Name,
delegates.get(lar.User__r.Secondary_Delegated_Approver_Id__c).Email,
larToOppMap.get(lar.Id),
lar.Lease_Approval__r,
lar.Id,
scenarioToUnitInfoMap.get(lar.Lease_Approval__r.Scenario__c));
mail.setSubject('Secondary Delegate Action Required(Primary Waited 4 days.');
emailsToSend.add(mail);
}
}else{
//if more than 8, send to secondary, else send to primary
if(LeaseApprovalHelper.getDiffBusinessDays(lar.Notified_Date__c, System.today())>8 && lar.User__r.Secondary_Delegated_Approver_Id__c != null){
//email to secondary, add secondary to lar
lar.Secondary_Delegate__c = lar.User__r.Secondary_Delegated_Approver_Id__c;
larList.add(lar);
Messaging.SingleEmailMessage mail = LeaseApprovalHelper.createApprovalActionNeededEmail(emailTemplateMap.get('Approver_Action_Needed'),
dealMakerMap.get(lar.Lease_Approval__r.Scenario__r.Lease_Analysis__r.Opportunity__r.ownerId).Name,
delegates.get(lar.User__r.Secondary_Delegated_Approver_Id__c).Email,
larToOppMap.get(lar.Id),
lar.Lease_Approval__r,
lar.Id,
scenarioToUnitInfoMap.get(lar.Lease_Approval__r.Scenario__c));
mail.setSubject('Secondary Delegate Action Required(Primary Waited 4 days.');
emailsToSend.add(mail);
}else{
//email to primary, add primary to lar
lar.Primary_Delegate__c = lar.User__r.DelegatedApproverId;
larList.add(lar);
Messaging.SingleEmailMessage mail = LeaseApprovalHelper.createApprovalActionNeededEmail(emailTemplateMap.get('Approver_Action_Needed'),
dealMakerMap.get(lar.Lease_Approval__r.Scenario__r.Lease_Analysis__r.Opportunity__r.ownerId).Name,
delegates.get(lar.User__r.DelegatedApproverId).Email,
larToOppMap.get(lar.Id),
lar.Lease_Approval__r,
lar.Id,
scenarioToUnitInfoMap.get(lar.Lease_Approval__r.Scenario__c));
mail.setSubject('Primary Delegate Action Required(Main Approver Waited 4 days.');
emailsToSend.add(mail);
}
}
}
}
if(larList.size()>0){
update larList;
}
if(emailsToSend.size()>0){
Messaging.sendEmail(emailsToSend);
}
}
global void finish(Database.BatchableContext BC) {
}
}
global class ApprovalDelegationsSchedulable implements Schedulable {
global void execute(SchedulableContext sc) {
ApprovalDelegationsBatchable b = new ApprovalDelegationsBatchable();
database.executebatch(b);
}
}
Thanks
String query;
Map<String, String> emailTemplateMap;
global ApprovalDelegationsBatchable() {
query = 'Select Id, Name,'+
' Status__c,'+
' Primary_Delegate__c,'+
' Secondary_Delegate__c,'+
' User__r.Out_Of_Office__c,'+
' User__r.Secondary_Delegated_Approver_Id__c,'+
' User__r.DelegatedApproverId,'+
' Notified_Date__c,'+
' Lease_Approval__r.Comments__c,'+
' Lease_Approval__c,'+
' Lease_Approval__r.Scenario__r.Lease_Analysis__r.Assumption_Comparison__c, '+
' Lease_Approval__r.Scenario__r.Lease_Analysis__r.Opportunity__r.Name, '+
' Lease_Approval__r.Scenario__r.Lease_Analysis__r.Opportunity__r.ownerId, '+
' Lease_Approval__r.Scenario__r.Lease_Analysis__r.Opportunity__r.Account.Account_Subtype__c,'+
' Lease_Approval__r.Scenario__r.Lease_Analysis__r.Opportunity__c,'+
' Lease_Approval__r.Scenario__c';
for(String s : Schema.SObjectType.Scenario__c.fields.getMap().keySet()){
query+=',Lease_Approval__r.Scenario__r.'+s;
}
query+=' From Lease_Approval_Role__c Where Status__c = \'Awaiting Response\'';
emailTemplateMap = LeaseApprovalHelper.getLeaseApprovalTemplates();
}
global Database.QueryLocator start(Database.BatchableContext BC) {
return Database.getQueryLocator(query);
}
global void execute(Database.BatchableContext BC, List<sObject> scope) {
List<Lease_Approval_Role__c> larList = new List<Lease_Approval_Role__c>();
List<Messaging.SingleEmailMessage> emailsToSend = new List<Messaging.SingleEmailMessage>();
Set<Id> opps = new Set<Id>();
Set<Id> delegateIds = new Set<Id>();
Set<Id> dealMakerIds = new Set<Id>();
Map<Id, String> scenarioToUnitInfoMap = new Map<Id, String>();
//Map<Id, Opportunity> oppMap = new Map<Id, Opportunity>([Select Id, Name, AccountId, Account.Account_Subtype__c From Opportunity Where Id in ]);
for(sobject s : scope){
Lease_Approval_Role__c lar = (Lease_Approval_Role__c)s;
dealMakerIds.add(lar.Lease_Approval__r.Scenario__r.Lease_Analysis__r.Opportunity__r.OwnerId);
scenarioToUnitInfoMap.put(lar.Lease_Approval__r.Scenario__c, null);
if(lar.User__r.DelegatedApproverId != null && LeaseApprovalHelper.getDiffBusinessDays(lar.Notified_Date__c, System.today())>=4){
opps.add(lar.Lease_Approval__r.Scenario__r.Lease_Analysis__r.Opportunity__c);
delegateIds.add(lar.User__r.DelegatedApproverId);
if(lar.User__r.Secondary_Delegated_Approver_Id__c != null) delegateIds.add(lar.User__r.Secondary_Delegated_Approver_Id__c);
}
}
Set<Id> combinedUnits = new Set<Id>();
for(Lease_Analysis_Selected_Unit__c su : [Select Id, Scenario__c, Unit__c,Unit__r.Unit__r.Property__c, Unit__r.Unit__r.Property_Address__c, Unit__r.Unit__r.Property_Code__c, Unit__r.Unit__r.Name, Unit__r.Unit__r.Property__r.Name from Lease_Analysis_Selected_Unit__c where Scenario__c IN : scenarioToUnitInfoMap.keySet() order by Scenario__c]){
if(su.Unit__c==null){
combinedUnits.add(su.Id);
}
else{
String unitTable;
if(scenarioToUnitInfoMap.containsKey(su.Scenario__c) && scenarioToUnitInfoMap.get(su.Scenario__c)!=null){
unitTable = scenarioToUnitInfoMap.get(su.Scenario__c);
if(unitTable.contains(su.Unit__r.Unit__r.Property__r.Name)){
unitTable += '<tr><td>'+su.Unit__r.Unit__r.Name+' - ' + su.Unit__r.Unit__r.Property_Code__c + '</td></tr>';
}
else{
unitTable += '<br/><table> <tr><td>'+su.Unit__r.Unit__r.Property__r.Name+'</td></tr>'+
'<tr><td>'+su.Unit__r.Unit__r.Property_Address__c+'</td></tr>'+
'<tr><td>'+su.Unit__r.Unit__r.Name+' - ' + su.Unit__r.Unit__r.Property_Code__c + '</td></tr>';
}
scenarioToUnitInfoMap.put(su.Scenario__c, unitTable);
}
else{
unitTable = '<table> <tr><td>'+su.Unit__r.Unit__r.Property__r.Name+'</td></tr>'+
'<tr><td>'+su.Unit__r.Unit__r.Property_Address__c+'</td></tr>'+
'<tr><td>'+su.Unit__r.Unit__r.Name+' - ' + su.Unit__r.Unit__r.Property_Code__c + '</td></tr>';
scenarioToUnitInfoMap.put(su.Scenario__c, unitTable);
}
}
}
for(Combined_Unit__c su : [Select Id, Unit_Opportunity__r.Unit__r.Property_Code__c, Selected_Unit__r.Scenario__c, Unit_Opportunity__r.Unit__r.Name, Unit_Opportunity__r.Unit__r.Property__r.Name, Unit_Opportunity__r.Unit__r.Property_Address__c from Combined_Unit__c where Selected_Unit__c IN : combinedUnits]){
String unitTable;
if(scenarioToUnitInfoMap.containsKey(su.Selected_Unit__r.Scenario__c) && scenarioToUnitInfoMap.get(su.Selected_Unit__r.Scenario__c)!=null){
unitTable = scenarioToUnitInfoMap.get(su.Selected_Unit__r.Scenario__c);
if(unitTable.contains(su.Unit_Opportunity__r.Unit__r.Property__r.Name)){
unitTable += '<tr><td>'+su.Unit_Opportunity__r.Unit__r.Name+' - ' + su.Unit_Opportunity__r.Unit__r.Property_Code__c + '</td></tr>';
}
else{
unitTable += '<br/><table> <tr><td>'+su.Unit_Opportunity__r.Unit__r.Property__r.Name+'</td></tr>'+
'<tr><td>'+su.Unit_Opportunity__r.Unit__r.Property_Address__c+'</td></tr>'+
'<tr><td>'+su.Unit_Opportunity__r.Unit__r.Name+' - ' + su.Unit_Opportunity__r.Unit__r.Property_Code__c + '</td></tr>';
}
scenarioToUnitInfoMap.put(su.Selected_Unit__r.Scenario__c, unitTable);
}
else{
unitTable = '<table> <tr><td>'+su.Unit_Opportunity__r.Unit__r.Property__r.Name+'</td></tr>'+
'<tr><td>'+su.Unit_Opportunity__r.Unit__r.Property_Address__c+'</td></tr>'+
'<tr><td>'+su.Unit_Opportunity__r.Unit__r.Name+' - ' + su.Unit_Opportunity__r.Unit__r.Property_Code__c + '</td></tr>';
scenarioToUnitInfoMap.put(su.Selected_Unit__r.Scenario__c, unitTable);
}
}
for(String s : scenarioToUnitInfoMap.values()){
s+='</table>';
}
Map<Id, Opportunity> oppMap = new Map<Id, Opportunity>([Select Id, Name, AccountId, Account.Account_Subtype__c From Opportunity Where Id in :opps]);
Map<Id, Opportunity> larToOppMap = new Map<Id, Opportunity>();
System.debug(delegateIds);
Map<Id, User> delegates = new Map<Id, User>([Select Id, Name, Email From User where Id IN :delegateIds]);
Map<Id, User> dealMakerMap = new Map<Id, User>([Select Id, Name From User where Id IN :dealMakerIds]);
for(sobject s : scope){
Lease_Approval_Role__c lar = (Lease_Approval_Role__c)s;
larToOppMap.put(lar.Id, oppMap.get(lar.Lease_Approval__r.Scenario__r.Lease_Analysis__r.Opportunity__c));
System.debug('DAYS: '+LeaseApprovalHelper.getDiffBusinessDays(lar.Notified_Date__c, System.today()));
if(lar.User__r.DelegatedApproverId != null && LeaseApprovalHelper.getDiffBusinessDays(lar.Notified_Date__c, System.today())>=4){ //Nothing to do if no delegates for user or if notified less than 4 business days ago
if(lar.User__r.Out_Of_Office__c){
if(lar.User__r.Secondary_Delegated_Approver_Id__c != null){
if(lar.Secondary_Delegate__c == null){
lar.Secondary_Delegate__c = lar.User__r.Secondary_Delegated_Approver_Id__c;
larList.add(lar);
}
//send email
Messaging.SingleEmailMessage mail = LeaseApprovalHelper.createApprovalActionNeededEmail(emailTemplateMap.get('Approver_Action_Needed'),
//delegates.get(lar.User__r.Secondary_Delegated_Approver_Id__c).Name,
dealMakerMap.get(lar.Lease_Approval__r.Scenario__r.Lease_Analysis__r.Opportunity__r.ownerId).Name,
delegates.get(lar.User__r.Secondary_Delegated_Approver_Id__c).Email,
larToOppMap.get(lar.Id),
lar.Lease_Approval__r,
lar.Id,
scenarioToUnitInfoMap.get(lar.Lease_Approval__r.Scenario__c));
mail.setSubject('Secondary Delegate Action Required(Primary Waited 4 days.');
emailsToSend.add(mail);
}
}else{
//if more than 8, send to secondary, else send to primary
if(LeaseApprovalHelper.getDiffBusinessDays(lar.Notified_Date__c, System.today())>8 && lar.User__r.Secondary_Delegated_Approver_Id__c != null){
//email to secondary, add secondary to lar
lar.Secondary_Delegate__c = lar.User__r.Secondary_Delegated_Approver_Id__c;
larList.add(lar);
Messaging.SingleEmailMessage mail = LeaseApprovalHelper.createApprovalActionNeededEmail(emailTemplateMap.get('Approver_Action_Needed'),
dealMakerMap.get(lar.Lease_Approval__r.Scenario__r.Lease_Analysis__r.Opportunity__r.ownerId).Name,
delegates.get(lar.User__r.Secondary_Delegated_Approver_Id__c).Email,
larToOppMap.get(lar.Id),
lar.Lease_Approval__r,
lar.Id,
scenarioToUnitInfoMap.get(lar.Lease_Approval__r.Scenario__c));
mail.setSubject('Secondary Delegate Action Required(Primary Waited 4 days.');
emailsToSend.add(mail);
}else{
//email to primary, add primary to lar
lar.Primary_Delegate__c = lar.User__r.DelegatedApproverId;
larList.add(lar);
Messaging.SingleEmailMessage mail = LeaseApprovalHelper.createApprovalActionNeededEmail(emailTemplateMap.get('Approver_Action_Needed'),
dealMakerMap.get(lar.Lease_Approval__r.Scenario__r.Lease_Analysis__r.Opportunity__r.ownerId).Name,
delegates.get(lar.User__r.DelegatedApproverId).Email,
larToOppMap.get(lar.Id),
lar.Lease_Approval__r,
lar.Id,
scenarioToUnitInfoMap.get(lar.Lease_Approval__r.Scenario__c));
mail.setSubject('Primary Delegate Action Required(Main Approver Waited 4 days.');
emailsToSend.add(mail);
}
}
}
}
if(larList.size()>0){
update larList;
}
if(emailsToSend.size()>0){
Messaging.sendEmail(emailsToSend);
}
}
global void finish(Database.BatchableContext BC) {
}
}
global class ApprovalDelegationsSchedulable implements Schedulable {
global void execute(SchedulableContext sc) {
ApprovalDelegationsBatchable b = new ApprovalDelegationsBatchable();
database.executebatch(b);
}
}
Thanks