TimeSheetSplitsTrigger and Test Class
TimeSheetSplitsTrigger :
trigger TimeSheetSplitsTrigger on Krow__Timesheet_Split__c (after insert) {
if(Trigger.isAfter && Trigger.isInsert){
Set<id> setOfParentKrTskIds = new Set<id>();
for(Krow__Timesheet_Split__c rec:Trigger.New){
if(rec.Krow__Krow_Task__c != NULL){
setOfParentKrTskIds.add(rec.Krow__Krow_Task__c);
}
}
//System.debug('#####1.0 : '+ setOfParentKrTskIds);
if(setOfParentKrTskIds != null){
Map<Id,krow__task__c> updateKrowTask = new Map<Id,krow__task__c>();
Set<Id> setOfIdsRootParent = new Set<Id>();
for(krow__task__c krTsk : [SELECT Name, Krow__Root__c FROM krow__task__c WHERE Id IN : setOfParentKrTskIds]){
if(krTsk.Name == 'No Shows' || krTsk.Name == 'No Show' && krTsk.Krow__Root__c != Null){
setOfIdsRootParent.add(krTsk.Krow__Root__c);
}
}
//System.debug('#####1.1 : '+ setOfIdsRootParent);
List<Krow__Task__c> listOfkrTsk = [SELECT id, name, No_Shows__c, Krow__Parent_Task__c, (select id, name from Krow__Root_Task__r) from Krow__Task__c where ID IN : setOfIdsRootParent];
Map<id, Krow__Task__c> mapOfNoShowkrTsk = new Map<id, Krow__Task__c>();
for(Krow__Task__c val:listOfkrTsk){
Integer newCount = 0;
for(Krow__Task__c rec:val.Krow__Root_Task__r){
if(rec.Name == 'No Shows' || rec.Name == 'No Show'){
mapOfNoShowkrTsk.put(rec.Id,null);
System.debug('#####1.2 : '+ rec.Name );
}
}
}
//System.debug('#####1.3 :'+ mapOfNoShowkrTsk);
if(!mapOfNoShowkrTsk.isEmpty()){
mapOfNoShowkrTsk = new Map<Id,Krow__Task__c>([SELECT id, name, No_Shows__c, Krow__Parent_Task__c, (select id, name from krow__Timesheet_Splits__r) from Krow__Task__c where id in : mapOfNoShowkrTsk.keySet()]);
//System.debug('mapOfNoShowkrTsk : '+ mapOfNoShowkrTsk.get('a378B0000008wfYQAQ').krow__Timesheet_Splits__r.size());
for(Krow__Task__c val:listOfkrTsk){
Integer newCount = 0;
for(Krow__Task__c rec:val.Krow__Root_Task__r){
if(rec.Name == 'No Shows' || rec.Name == 'No Show'){
if(mapOfNoShowkrTsk.containsKey(rec.Id ) && mapOfNoShowkrTsk.get(rec.Id).krow__Timesheet_Splits__r.size() != 0 && mapOfNoShowkrTsk.get(rec.Id).krow__Timesheet_Splits__r != null){
newCount += mapOfNoShowkrTsk.get(rec.Id).krow__Timesheet_Splits__r.size();
}
}
}
val.No_Shows__c = Double.valueOf(newCount);
//System.debug('######1.6 : '+ val.No_Shows__c);
updateKrowTask.put(val.Id,val);
}
}
//System.debug('#####1.7 : '+ updateKrowTask);
if(!updateKrowTask.isEmpty()){
Update updateKrowTask.values();
}
}
}
}
Test Class
@isTest
private class TimeSheetSplitsTriggerTestClass {
@isTest static void validateTimesheetSplits(){
Krow__Timesheet__c krTimesheetObj = new Krow__Timesheet__c();
krTimesheetObj.CreatedDate = System.today();
Insert krTimesheetObj;
krow__task__c ktObjParent = new krow__task__c();
ktObjParent.Name = 'No Show';
ktObjParent.Krow__Status__c = 'None';
Insert ktObjParent;
krow__task__c ktObjChild = new krow__task__c();
ktObjChild.Name = 'No Show';
ktObjChild.Krow__Status__c = 'None';
ktObjChild.Krow__Root__c = ktObjParent.Id;
ktObjParent.Krow__Root__c = ktObjParent.Id;
Insert ktObjChild;
Krow__Timesheet_Split__c krObj = new Krow__Timesheet_Split__c();
krObj.Krow__Krow_Task__c = ktObjChild.id;
krObj.Krow__Hours__c = 5;
krObj.Krow__Date__c = System.today();
krObj.Krow__Timesheet__c = krTimesheetObj.id;
Insert krObj;
}
}
Comments
Post a Comment