I'm in need of some help regarding the writing of test script that covers enough of the below trigger that I have managed to get working on my Sandbox account. The trigger is to automatically fill Month field in Timecard (ex: 2014/009) whenever a new Timecard is created. The trigger is running fine but I need to write a test case which passes this trigger.
Trigger
trigger Timecard on pse__Timecard_Header__c (after insert) {
List<pse__Timecard_Header__c> tc_list = [select Id, pse__End_Date__c, pse__Project__r.pse__Region__r.ffpsai__OwnerCompany__r.Id
from pse__Timecard_Header__c
where Id IN :Trigger.newMap.KeySet()];
system.debug('TC_LIST: ' + tc_list);
Set<Date> tc_setofDate = new Set<Date>();
Set<Id> tc_setofId = new Set<Id>();
for (pse__Timecard_Header__c tcdate: tc_list)
{
Date aDate = tcdate.pse__End_Date__c.toStartOfMonth();
tc_setofDate.add(aDate);
system.debug('SET DATE ' + tc_setofDate);
}
for (pse__Timecard_Header__c tcId: tc_list)
{
Id ids = tcId.pse__Project__r.pse__Region__r.ffpsai__OwnerCompany__c;
tc_setofId.add(ids);
system.debug('SET ID ' + tc_setofId);
}
List <c2g__codaPeriod__c> periodDetails = [Select Id, c2g__StartDate__c,c2g__EndDate__c, c2g__OwnerCompany__c
from c2g__codaPeriod__c
where c2g__StartDate__c IN: tc_setofDate
and c2g__OwnerCompany__c IN: tc_setofId];
system.debug('PERIOD_DETAILS: ' + periodDetails);
List<pse__Timecard_Header__c> tcUpdate = new List<pse__Timecard_Header__c>();
for (pse__Timecard_Header__c timecardCompare: tc_list){
for (c2g__codaPeriod__c periodCompare: periodDetails){
if ((timecardCompare.pse__End_Date__c.toStartOfMonth() == periodCompare.c2g__StartDate__c) &&
(timecardCompare.pse__Project__r.pse__Region__r.ffpsai__OwnerCompany__c == periodCompare.c2g__OwnerCompany__c))
{
timecardCompare.Period__c = periodCompare.Id;
tcUpdate.add(timecardCompare);
}
}
}
update tcUpdate;
}
If anyone could help me on this, it would be grateful.
Test Case for this trigger so far
@IsTest(SeeAllData=true)
public class TimecardTest {
static testmethod void insertTimecard(){
pse__Proj__c testProject = new pse__Proj__c();
testProject.pse__Region__c = 'Nexius Fusion';
pse__Timecard_Header__c testTimecard = new pse__Timecard_Header__c();
testTimecard.pse__End_Date__c = system.today().toStartOfMonth();
testTimecard.pse__Start_Date__c = system.today().toStartOfMonth();
testTimecard.pse__Resource__c= 'Brandon Catchpole';
testTimecard.pse__Project__c = testProject.Id;
insert testTimecard;
c2g__codaPeriod__c testPeriod = new c2g__codaPeriod__c();
testPeriod.c2g__StartDate__c = system.today().toStartOfMonth();
testPeriod.c2g__OwnerCompany__c = 'compare with timecard company id';
Test.startTest();
Test.stopTest();
If anyone could say if I am going in the right direction with this I would be grateful. Attempting to iron out the errors but there is obviously no point if this is not going to work anyway.
Thanks