+ Start a Discussion

Trigger to match fields in another object

I have 2 custom objects

Timecard (pse__Timecard_Header__c)
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 tcdata = new List ();

     for(pse__Timecard_Header__c tc: Trigger.new)
         tc.pse__Start_Date__c tc.pse__End_Date__c tcdata.add(tc); 
Any help would be appreciated. Thanks in advance.
Trigger will work like this:
  1. Gather a list of all timecard start date, a list of all timecard end dates, and a list of all timecard regions.
  2. Run a SOQL query for periods where start date is in list of start dates, end day is in list of end dates, and region is in list of regions.
  3. Go through that query result. If any of the results match any values in trigger.new, throw an error.
you should have Relation Ship between TimeCard and Project right?
you have to run the SOQL(Example below) to get the region on the Project (pse__Proj__c). You can use the relationship name (pse__Proj__r) to query.

Map<id,pse__Timecard_Header__c> timeCardMap = new Map<id,pse__Timecard_Header__c>([select id,pse__Proj__r.pse__Region__c from pse__Timecard_Header__c where id in :trigger.new]);