function readOnly(count){ }
Starting November 20, the site will be set to read-only. On December 4, 2023,
forum discussions will move to the Trailblazer Community.
+ Start a Discussion
BobBob 

Adding date ranges to a trigger

I need to add multiple date ranges to my trigger below or maybe create a apex class to perform this function.
I have two objects Unit__c & Warranty__c . There are date fields on both object (Unit__c, ShipDateForm__c) (Warranty__c, WrntyStartDate__c).
I need my trigger or apex class to compare the ShipDateForm__c and the WrntyStartDate__c. can I do a IF statement with 3 conditions, because i need to check another field to.
This is what it looks like below, but it doesn't work.  Any help would be appreciated

CODE SNIPPET:

if (newUnit .Warranty_Type__c == null && newUnit .ShipDateForm__c <= '2011-12-30' && newUnit .WrntyStartDate__c <= '2011-12-30' ) {
      Warranty__c product = wrntyMap.get(newUnit.Product__c);


FULL TRIGGER:

trigger Trigger_UpdateWarranties on Unit__c (before insert, before update ) {

// Step 1: Create a set of all products of Units to query
  Set<String> allwrnty = new Set<String>();
 
for (Unit__c newUnit : Trigger.new) {
    if (newUnit.Product__c != null) {
      allwrnty.add(newUnit.Product__c);    }
  }

  // Step 2: Query for all the Units in Step 1
  List<Warranty__c> potentialproduct = [SELECT Id, Name,Warranty_Active_Date__c, Product__c FROM Warranty__c
                                 WHERE Product__c IN :allwrnty];

 
   // Step 3: Make a Map that lets you search for units by product
  Map<String, Warranty__c> wrntyMap = new Map<String, Warranty__c>();
  for (Warranty__c w : potentialproduct) {
    wrntyMap.put(w.Product__c, w);
  }
 // Step 4: Get the matching Warranty in the Map by product!
  for (Unit__c newUnit : Trigger.new) {
   
  if (newUnit .Warranty_Type__c == null && newUnit .ShipDateForm__c <= '2011-12-30' && newUnit .WrntyStartDate__c <= '2011-12-30' ) {
      Warranty__c product = wrntyMap.get(newUnit.Product__c);
      
       
     
      if (product != null) {
        newUnit.Warranty_Type__c = product.Id;
      }
    }
  }
    
 
    }
Ross Gilbert 18Ross Gilbert 18
Hi,

What exactly are you trying to do here?  Could you just explain it in words without any code yet?  Thanks
BobBob
Yes, I have a (Unit__c) object that has a record for each peice of  fitness equipment. The equipemnt record has a ship date field.  The second object (Warranty__c) has a record for every warranty type. The warranty type record has a warranty active date field.  The equipment record ship date needs to be within a date range that matches a warranty type record. There are multiple date ranges so I will need have the ability to add those date ranges.
BobBob
Any luck with this, I’m still stuck.