1

I have 2 objects Timecard (pse__Timecard_Header__c) and Periods (c2g__codaPeriod__c).

There is a lookup field in Timecard Period__c which is related to Period.

From Timecard, I need end date pse__End_Date__c and company id (lookup field) pse__Project__r.pse__Region__r.ffpsai__OwnerCompany__r.Id

From Periods, I need
start date c2g__StartDate__c and owner company c2g__OwnerCompany__c.

The idea is here to compare these fields i.e.

  1. end date(timecard) = start date (period) and

  2. company id(timecard) = owner company (period)

I am stuck at the last step as how to compare these values and if they are matched then assign it to the respective fields.

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_set = new Set<Date>();

    for (pse__Timecard_Header__c tcdate: tc_list)
    {
        Date aDate = tcdate.pse__End_Date__c.toStartOfMonth();
        tc_set.add(aDate);
    }

    Set<String> tc_setofId = new Set<String>();
    for (pse__Timecard_Header__c tcId: tc_list)
    {
        String s = tcId.pse__Project__r.pse__Region__r.ffpsai__OwnerCompany__c;
        tc_setofId.add(s);
    }

    List <c2g__codaPeriod__c> periodDetails = [Select Id, c2g__StartDate__c,c2g__EndDate__c, c2g__OwnerCompany__c, c2g__Closed__c 
        from c2g__codaPeriod__c
        where c2g__StartDate__c IN: tc_set
        and c2g__OwnerCompany__c IN: tc_setofId];

    for (pse__Timecard_Header__c timecardCompare: tc_list){

        for (c2g__codaPeriod__c periodCompare: periodDetails){
            //if 
            //to do comparison here
        }
    }   
}
  • What is the relationship between the two? – dphil Dec 3 '14 at 16:26
  • there is a lookup field in Timecard object called Period__c which is related to Period. – LazyBones Dec 3 '14 at 16:32
1

Your logic is incorrect and you are performing necessary queries as well as storing them in a way that make your code very difficult to process the record.

In the below code I have created a set of period record IDs.

The I create a map of those periods.

This allows you to cycle through the timecard headers and quickly access the associated period record and compare the values:

trigger Timecard on pse__Timecard_Header__c (after insert) {


  List<pse__Timecard_Header__c> tc_list = [select Id, Period__c, 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()];

Set<ID> period_ids = new Set<ID>();

for (pse__Timecard_Header__c tc: tc_list)
    {
        period_ids.add(tc.Period__c);
    }


Map <ID,c2g__codaPeriod__c> periodDetails = New Map<ID,c26__codaPeriod__c>([Select Id, c2g__StartDate__c,c2g__EndDate__c, c2g__OwnerCompany__c, c2g__Closed__c 
                                            from c2g__codaPeriod__c
                                            where c2g__StartDate__c IN: tc_set
                                            and ID IN: period_ids]);

for (pse__Timecard_Header__c timecardCompare: tc_list){

    if(periodDetails.containsKey(timecarCompare.Period__c)){

      if(timeCardCompare.pse__End_Date__c == periodDetails.get(timeCardCompare.Period__c).c2g__EndDate__c){
        //do stuf if equal
      }

    }

}

}
  • thank you for your revised code. This is definitely a better approach than mine. But I believe you are adding Timecard Id inside Set<ID> instead of adding company Id. – LazyBones Dec 3 '14 at 18:19
  • @sus - No, I am adding the Period ID so you can query for the related periods. You are not clear on what you want for company so you will have to work that out. – Eric Dec 3 '14 at 18:24
0

This is what I needed.

for (pse__Timecard_Header__c timecardCompare: tc_list){

    for (c2g__codaPeriod__c periodCompare: periodDetails){

        if ((timecardCompare.pse__End_Date__c == periodCompare.c2g__StartDate__c) && 
              (timecardCompare.pse__Project__r.pse__Region__r.ffpsai__OwnerCompany__c == periodCompare.c2g__OwnerCompany__c))
        {
                timecardCompare.Period__c = periodCompare.Id;
        }


    }
}   
  • Is this an answer? If so, mark it as accepted if not, to add clarification to your question, please edit your original question. – Eric Dec 3 '14 at 18:25

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.