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 

Trigger with date range not working



Below I have a trigger with a custom object called Unit__c. The custom object has a Ship_Date__c field. On my other custom object Warranty__c. I have two date fields Warranty_Active_Date__c  and Warranty_End_Date__c.  I need my trigger to look at the ship date on the unit object and find records that have a ship date >= Warranty_Active_Date__c and a ship date <=  Warranty_End_Date__c. For some reason I can't find why my trigger is not working. Any help would be appreciated. 
 
//Ceated by Bob Poliquin 2/16/2016 works with ship dates
trigger Trigger_UpdateWarranties on Unit__c (before insert, before update ) {


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

  // Step 2: Query for all the Units in Step 1
  List<Warranty__c> potentialproducts = [SELECT Id, Name, Warranty_Active_Date__c, Product__c,Warranty_End_Date__c FROM Warranty__c
                                 WHERE Product__c = :products AND Warranty_Active_Date__c >= :allwrntyad AND Warranty_End_Date__c <= :allwrntyed ];

  
   // 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 : potentialproducts) {
     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) {
      Warranty__c product = wrntyMap.get(newUnit.Product__c);
      
     if (wrntyMap.containsKey(newUnit.Product__c )) {
       newUnit.Warranty_Type__c = product.Id;
      }
    }
  }
    
 
    }

 
Best Answer chosen by Bob
gautam_singhgautam_singh
Assuming you want to accomplish this "Ship Date should be greater than Warranty Active Date & Ship Date should be lesser than End Date" Change WHERE condition of your query to"Warranty_Active_Date__c <= :allwrntyad AND Warranty_End_Date__c >= :allwrntyed"

Also, there are some more tweaks which you can do to make this trigger work better. 
 
trigger Trigger_UpdateWarranties on Unit__c (before insert, before update ) {

    Set<Date> allwrntyad = new Set<Date>();
    Set<Date> allwrntyed = new Set<Date>();
    Set<String> products = new Set<String>();
  
    for (Unit__c newUnit : Trigger.new) {
        if (newUnit.Ship_Date__c != null){
            allwrntyad.add(newUnit.Ship_Date__c);
            allwrntyed.add(newUnit.Ship_Date__c); 
            products.add(newUnit.Product__c);
        }  
    }
system.debug('&&&&&&&' + products);
  // Step 2: Fetch Warranty of all updating/inserting Units which are shipped in between warranty duration
  List<Warranty__c> potentialproducts = [SELECT Id, Name, Warranty_Active_Date__c, Product__c,Warranty_End_Date__c FROM Warranty__c
                                 WHERE Product__c = :products AND Warranty_Active_Date__c <= :allwrntyad AND Warranty_End_Date__c <= :allwrntyed ];

  system.debug('&&&&&&&' + potentialproducts);
   
  Map<String, Warranty__c> wrntyMap = new Map<String, Warranty__c>();
  for (Warranty__c w : potentialproducts) {
     wrntyMap.put(w.Product__c, w);
  }
 // Step 4: Associate the specific warranty in warranty type of unit Object
  for (Unit__c newUnit : Trigger.new) {
     if (newUnit .Warranty_Type__c == null) {
      Warranty__c product = wrntyMap.get(newUnit.Product__c);
      
     if (wrntyMap.containsKey(newUnit.Product__c )) {
       newUnit.Warranty_Type__c = product.Id;
      }
    }
  }
    
 
}




 

All Answers

Mahesh DMahesh D
Hi Bob,

Please look into the below modified code:
 
//Ceated by Bob Poliquin 2/16/2016 works with ship dates
trigger Trigger_UpdateWarranties on Unit__c (before insert, before update ) {
	// Step 1: Create a set of all products of Units to query
    Set<String> products = new Set<String>();
  
	for (Unit__c newUnit : Trigger.new) {
		if (newUnit.Ship_Date__c != null && newUnit .Warranty_Type__c == null){
			products.add(newUnit.Product__c);
		}  
    }

	if(!products.isEmpty()) }
		// Step 2: Query for all the Units in Step 1
		List<Warranty__c> potentialproducts = [SELECT Id, Name, Warranty_Active_Date__c, Product__c, Warranty_End_Date__c FROM Warranty__c
														WHERE Product__c = :products];
	  
		// Step 3: Make a Map that lets you search for units by product
		Map<String, Warranty__c> wrntyMap = new Map<String, Warranty__c>();
	 
		// Step 4: Get the matching Warranty in the Map by product!
		for (Unit__c newUnit : Trigger.new) {
			if (newUnit.Ship_Date__c != null && newUnit.Warranty_Type__c == null) {
				for(Warranty__c war: potentialproducts) {
					if(war.Warranty_Active_Date__c >= newUnit.Ship_Date__c && war.Warranty_End_Date__c <= newUnit.Ship_Date__c) {
						newUnit.Warranty_Type__c = war.Id;
						break;
					}
				}
			}
		}
	}
}

Please do let me know if it helps you.

Regards,
Mahesh
gautam_singhgautam_singh
Assuming you want to accomplish this "Ship Date should be greater than Warranty Active Date & Ship Date should be lesser than End Date" Change WHERE condition of your query to"Warranty_Active_Date__c <= :allwrntyad AND Warranty_End_Date__c >= :allwrntyed"

Also, there are some more tweaks which you can do to make this trigger work better. 
 
trigger Trigger_UpdateWarranties on Unit__c (before insert, before update ) {

    Set<Date> allwrntyad = new Set<Date>();
    Set<Date> allwrntyed = new Set<Date>();
    Set<String> products = new Set<String>();
  
    for (Unit__c newUnit : Trigger.new) {
        if (newUnit.Ship_Date__c != null){
            allwrntyad.add(newUnit.Ship_Date__c);
            allwrntyed.add(newUnit.Ship_Date__c); 
            products.add(newUnit.Product__c);
        }  
    }
system.debug('&&&&&&&' + products);
  // Step 2: Fetch Warranty of all updating/inserting Units which are shipped in between warranty duration
  List<Warranty__c> potentialproducts = [SELECT Id, Name, Warranty_Active_Date__c, Product__c,Warranty_End_Date__c FROM Warranty__c
                                 WHERE Product__c = :products AND Warranty_Active_Date__c <= :allwrntyad AND Warranty_End_Date__c <= :allwrntyed ];

  system.debug('&&&&&&&' + potentialproducts);
   
  Map<String, Warranty__c> wrntyMap = new Map<String, Warranty__c>();
  for (Warranty__c w : potentialproducts) {
     wrntyMap.put(w.Product__c, w);
  }
 // Step 4: Associate the specific warranty in warranty type of unit Object
  for (Unit__c newUnit : Trigger.new) {
     if (newUnit .Warranty_Type__c == null) {
      Warranty__c product = wrntyMap.get(newUnit.Product__c);
      
     if (wrntyMap.containsKey(newUnit.Product__c )) {
       newUnit.Warranty_Type__c = product.Id;
      }
    }
  }
    
 
}




 
This was selected as the best answer