You need to sign in to do that
Don't have an account?
Bob
Mapping Two Fields to Match Records
I'm still working with this same trigger below. It works, but it is not 100% acurrate.
I have a trigger for two custom objects (Extended_Warranty__c & Unit__c) The trigger's function is to find the Order_No__c and Product_Code__c field on the Unit__c object and match it to the Ext_Warranty__c and Product_Code__c fields on the Extended_Warranty__c object. Then the Extended Warranty is assigned to a lookup field on the Unit if there is a match. I have been trying to figure out how to get my trigger to match those fields and I am getting frustrated. Is there a way to put a condition in somewhere in my trigger below that will find those two fields and assign the extended warranty to the unit. Any help would be greatly appreciated.
I have a trigger for two custom objects (Extended_Warranty__c & Unit__c) The trigger's function is to find the Order_No__c and Product_Code__c field on the Unit__c object and match it to the Ext_Warranty__c and Product_Code__c fields on the Extended_Warranty__c object. Then the Extended Warranty is assigned to a lookup field on the Unit if there is a match. I have been trying to figure out how to get my trigger to match those fields and I am getting frustrated. Is there a way to put a condition in somewhere in my trigger below that will find those two fields and assign the extended warranty to the unit. Any help would be greatly appreciated.
rigger TriggerUpdateUnitExtWarranty on Unit__c (before insert, before update) { // Step 1: Create a set of all extended warranties of Units to query Set<String> extwrnty = new Set<String>(); for (Unit__c newUnit : Trigger.new) { extwrnty.add(newUnit.Order_No__c); } // Step 2: Query for all the Units in Step 1 List<Extended_Warranty__c> potentialproduct = [SELECT Id, Name,Order_No__c,Product_Code__c FROM Extended_Warranty__c WHERE Name IN :extwrnty]; // Step 3: Make a Map that lets you search for units by extended warranty Map<String, Extended_Warranty__c> ewrntyMap = new Map<String, Extended_Warranty__c>(); for (Extended_Warranty__c w : potentialproduct) { ewrntyMap.put(w.Order_No__c, w); } // Step 4: Get the matching extended warranty in the Map by extended warranty! for (Unit__c newUnit : Trigger.new) { if (newUnit.Ext_Warranty__c == null) { Extended_Warranty__c ordnm = ewrntyMap.get(newUnit.Order_No__c); //ordnm is the name of the extended warranty record that is associated with the unit. if (ordnm != null) { newUnit.Ext_Warranty__c = ordnm.Id; } } } }
trigger TriggerUpdateUnitExtWarranty on Unit__c (before insert, before update) {
// Step 1: Create a set of all extended warranties of Units to query
Set<String> extwrnty = new Set<String>();
for (Unit__c newUnit : Trigger.new) {
extwrnty.add(newUnit.Order_No__c);
}
// Step 2: Query for all the Units in Step 1
List<Extended_Warranty__c> potentialproduct = [SELECT Id, Name,Order_No__c,Product_Code__c FROM Extended_Warranty__c
WHERE Name IN :extwrnty];
//Now create a map
Map<String, Extended_Warranty__c> ewrntyMap = new Map<String, Extended_Warranty__c>();
for (Extended_Warranty__c w : potentialproduct) {
ewrntyMap.put(w.Order_No__c+'_'+Product_Code__c , w);
}
// Step 4: Get the matching extended warranty in the Map by extended warranty!
for (Unit__c newUnit : Trigger.new) {
if (newUnit.Ext_Warranty__c == null) {
Extended_Warranty__c ordnm = ewrntyMap.get(newUnit.Order_No__c+'_'+Product_Code__c );
//ordnm is the name of the extended warranty record that is associated with the unit.
if (ordnm != null) {
newUnit.Ext_Warranty__c = ordnm.Id;
}
}
}
}
Compile Error: Variable does not exist: Order_No__c at line 25 column 48
can you post full code ?
Then why wouldn't step 1 and 2 be replaced with...?