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 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();
        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;
        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;


    update tcUpdate;


If anyone could help me on this, it would be grateful.

Test Case for this trigger so far

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';


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.


1 Answer 1


Your trigger will fire as soon as you insert any new pse__Timecard_Header__c records. That's also when your test starts. So, you'll want to do something more like the following:

// prepare all your test data as you currently have, (and more if necessary) 
// wait to actually do the insert that fires the trigger until after preparing all test data.

// From what I can tell, below is when your test should begin, 
// Finish preparing the other test data before executing this insert command 

insert testTimecard;


// make assertions of what you expect to happen after your trigger has run to prove it works as expected
// you can query for results as needed in order to do your assertions

system.assert(expected values as updated by trigger); 
  • thank you your answer. I understand the steps but I am stuck at a point where I don't know how to proceed. If you can help with few lines of code I would really appreciate.
    – LazyBones
    Dec 8, 2014 at 12:31

You must log in to answer this question.

Not the answer you're looking for? Browse other questions tagged .