I have 2 custom objects

  1. Timecard (pse__Timecard_Header__c)
  2. Period (c2g__codaPeriod__c).

They don't have relationship.

Timecard fields:- Start date (pse__Start_Date__c), Close date (pse__CloseDate__c), Project (pse__Proj__c). Now here, inside project is one field Region (pse__Region__c).

Period fields:- Start date (c2g__StartDate__c), Close date (c2g__Closed__c), Region (c2g__Region__c ), status (checkbox).

If the status is checked and start date, close date and region are matching with the start date, close date and region of Timecard object then I want Timecard to be rejected.

I started with the following code but then get lost realising I don't have direct access to region inside project.

trigger TimecardRejection on pse__Timecard_Header__c (before update) {
    List <Object> tcdata = new List <Object> ();

       for(pse__Timecard_Header__c tc: Trigger.new){
            Date startDate = tc.pse__Start_Date__c;
            Date endDate = tc.pse__End_Date__c;


Any help would be appreciated. Thanks in advance.

1 Answer 1


If I understand your case correctly, you in fact do have a direct link to period, but you have to walk across the Project object to get there. You can do that in trigger by querying for the relevant project records using a SOQL query. You only have to bear in mind, that triggers can run for 1-N records, so you mustn't add the query in the loop. Instead, you loop over trigger.new, add the Project IDs from the pse__Proj__c custom field into a set and then use the set for the SOQL query. That way, you get the Project objects which are associated to your Timecards from the database, including the Period.

Edit: This is what I had in mind:

//First you get the Ids of the projects from your Timecards
Set<String> projectIdSet = new Set<String>();
for(pse__Timecard_Header__c tc: Trigger.new){

//Then you query for the projects and put the result into a map
Map<Id, Project__c> projectMap = new Map<Id, Project__c>[select Id, pse__Region__c from Project__c where Id in :projectIdSet];

// And this is how you can access the rregion for the individual timecards from the projectMap
for(pse__Timecard_Header__c tc: Trigger.new){
  • @Kolin i appreciate your effort in taking out time and commenting. I am new to salesforce and I partially understood what you are trying to convey. I would appreciate if you can help me with some piece pseudo code. Thank you very much.
    – LazyBones
    Nov 17, 2014 at 22:00
  • I added some pseudo code to clarify Nov 18, 2014 at 7:50
  • thank you kolin. what you have written I have partially achieved my goal. I still need to figure out how to get the start date end date with company ID for those regions. thank you for the help.
    – LazyBones
    Nov 18, 2014 at 14:58

You must log in to answer this question.

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