0

I am trying to write a Trigger. I have 2 objects.

  1. Timecard pse__Timecard_Header__c
  2. Period c2g__codaPeriod__c

In Timecard object, I have a field called Project pse__Project__r. Inside this field I have a field Region Id pse__Region__r.

Now, in Period Object I have a field company id c2g__OwnerCompany__c.

I am trying to compare the region id (Timecard object) with company id (period object). I have the following code written. I just want the final step to be done i.e. to get the value from map so that I can compare it.

trigger Timecard on pse__Timecard_Header__c (after insert) {

  List<pse__Timecard_Header__c> tc_list = [select Id, pse__Project__r.pse__Region__r.ffpsai__OwnerCompany__r.Id 
                                             from pse__Timecard_Header__c
                                             where Id IN :Trigger.newMap.KeySet()];
 Map<id, id> timecardAndCompanyMap = new Map<id, id> ();

    for (pse__Timecard_Header__c tc : tc_list)
    {
        timecardAndCompanyMap.put(tc.Id, tc.pse__Project__r.pse__Region__r.ffpsai__OwnerCompany__r.Id);
   }


    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 =: timecardAndCompanyMap.get(id)];
}`

1 Answer 1

2

A Map has a values() method but this returns a collection so you will need to use an IN term in the query:

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__OwnerCompany__c IN :timecardAndCompanyMap.values()
        ];

Its not clear from your question how to further limit the set of c2g__OwnerCompany__c objects to one object for a pse__Timecard_Header__c object.

3
  • thank u...that is helpful. Lastly, I am creating a Map for Period object but here the Id is coming from Timecard Object and I don't know how to retreive that. Map <Id, c2g__codaPeriod__c> pMap = new Map<Id, c2g__codaPeriod__c>(); for (c2g__codaPeriod__c pDetails: periodDetails) { pMap.put(pDetails.Id, pDetails); }
    – LazyBones
    Dec 3, 2014 at 14:20
  • @sus If you can say exactly what field (on what object) holds the ID of the c2g__codaPeriod__c object I can add some code to get the right object to the answer.
    – Keith C
    Dec 3, 2014 at 14:26
  • I am going with an alternate approach so I won't need what I was asking in my previous comment. thanks though
    – LazyBones
    Dec 3, 2014 at 15:03

You must log in to answer this question.

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