You need to sign in to do that
Don't have an account?
Bob
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.
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; } } } }
Also, there are some more tweaks which you can do to make this trigger work better.
All Answers
Please look into the below modified code:
Please do let me know if it helps you.
Regards,
Mahesh
Also, there are some more tweaks which you can do to make this trigger work better.