function readOnly(count){ }
Don't have an account?
Search for an answer or ask a question of the zone or Customer Support.
You need to sign in to do that
Sign in to start searching questions
Signup for a Developer Edition
Sign in to start a discussion
Public static List<Case> newCases = new list<case>();
public static void updateWorkOrders(list<case> applicableTriggeredCases){
map<id,case> applicableCases = new map<id,case>();
for(case a:applicableTriggeredCases){
if((a.type.equals('Repair') || a.type.equals('Routine Maintenance') ) && a.IsClosed)
applicableCases.put(a.Id,a);
}
Map<Id, Integer> mapProduct = new Map<Id, Integer>();
Map<Id, decimal> WorkPartMCycleMap = new Map<Id, decimal>();
Map<Id, id> WorkPartEquipmentMap = new Map<Id, id>();
Map<Id, decimal> EquipmentMCycldeMap = new Map<Id, decimal>();
list<string> EquipmentIdList = new list<string>();
List<Work_Part__c> WorkParts = [select Id, Equipment__c,Maintenance_Request__c from Work_Part__c where Maintenance_Request__c in :applicableCases.keySet()];
for(Work_Part__c WorkPart: WorkParts){
if(!EquipmentIdList.contains(WorkPart.Equipment__c))
EquipmentIdList.add(WorkPart.Equipment__c);
WorkPartEquipmentMap.put(WorkPart.Id,WorkPart.Equipment__c);
}
List<Product2 > EquipmentDetailsList = [SELECT Id,Maintenance_Cycle__c FROM Product2 where Id in :EquipmentIdList];
for(Product2 EquipmentDetails: EquipmentDetailsList){
EquipmentMCycldeMap.put(EquipmentDetails.Id,EquipmentDetails.Maintenance_Cycle__c);
}
for(Work_Part__c WorkPart: WorkParts){
WorkPartMCycleMap.put(WorkPart.Id,EquipmentMCycldeMap.get(WorkPartEquipmentMap.get(WorkPart.Id)));
}
system.debug(WorkPartMCycleMap);
for(Case a: applicableCases.values()){
Case newCase = new Case();
newCase.Vehicle__c = a.Vehicle__c;
newCase.Equipment__c = a.Equipment__c;
newCase.Type = 'Routine Maintenance';
newCase.Subject = String.isBlank(a.Subject) ? 'Routine Maintenance Request' : a.Subject;
newCase.Date_Reported__c = a.CreatedDate.Date();
newCase.Status = 'New';
newCase.Product__c = a.Product__c;
newCase.AccountId = a.AccountId;
newCase.ContactId = a.ContactId;
newCase.AssetId = a.AssetId;
newCase.Origin = a.Origin;
newCase.Reason = a.Reason;
decimal minMaintenanceCycle = 0;
for(Work_Part__c NewWork:WorkParts){
if(NewWork.Maintenance_Request__c == a.Id){
if(minMaintenanceCycle > WorkPartMCycleMap.get(NewWork.Id) || minMaintenanceCycle == 0){
minMaintenanceCycle = WorkPartMCycleMap.get(NewWork.Id);
system.debug(WorkPartMCycleMap.get(NewWork.Id));
}
}
}
newCase.Date_Due__c = Date.today().addDays((integer)Math.ceil(minMaintenanceCycle));
newCases.add(newCase);
}
if(newCases.size() > 0){
insert newCases;
}
}
}
// call MaintenanceRequestHelper.updateWorkOrders
//
list<case> applicableCases = new list<case>();
if(Trigger.isUpdate && Trigger.isAfter){
for(Case oCase: Trigger.new){
if ((oCase.IsClosed) && (oCase.Type.equals('Repair') || oCase.Type.equals('Routine Maintenance'))){
for(Case aCase: Trigger.old){
if(aCase.Id == oCase.Id && !aCase.IsClosed){
applicableCases.add(oCase);
}
}
}
}
}
else if(Trigger.isInsert && Trigger.isAfter){
for(Case oCase: Trigger.new){
if ((oCase.IsClosed) && (oCase.Type.equals('Repair') || oCase.Type.equals('Routine Maintenance'))){
applicableCases.add(oCase);
}
}
}
if(applicableCases.size()>0){
MaintenanceRequestHelper.updateWorkOrders(applicableCases);
}
}