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 

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. 
 
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;
      }
     
    }
  }

}

 
venkat-Dvenkat-D
Try below.


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;
      }
     
    }
  }

}
JeffreyStevensJeffreyStevens
I would change the Extended_Warranty__c ordnm = ewrntyMap.get(.....); line - and wrap it with an If  .containsKey.  The get will fail if the combined key is not in the map.
 
// Step 4:....
for(Unit__c newUnit :trigger.new) {
  if(ewrntyMap.containsKey(newUnit.Orger_No__c+'_'+Product_Code__c)) {
    Extended_Warranty__c ordnm = ewrntyMap.get(.....)
  }
....
}

 
BobBob
I'm not clear on Order_No__c+'_'+Product_Code. am i creating a field on the extended warranty object called Order_No__c+'_'+Product_Code? or is that a concatenation of those two fields in my trigger?
venkat-Dvenkat-D
This is just concatenation in the trigger. This will create unique map key to get exact record.
JeffreyStevensJeffreyStevens
I think venky was just concatenating those two fields to make one key in the map.  So the ewrntyMap was built with a key of both fields as the key and the value of the map, the record of the warranty object.  (That was done in Step 2)
BobBob
Okay thank you both so much for helping. When I get my trigger working properly I will mark this post as best answer. 
BobBob
I updated the trigger and received this message. 

Compile Error: Variable does not exist: Order_No__c at line 25 column 48
BobBob
Never mind, I figured it out
venkat-Dvenkat-D
Bob,
can you post full code ?
BobBob
I updated my trigger to the following, but the trigger does nothing.
 
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+'_'+newUnit.Product_Code__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.Product_Code__c, w);
  }

  // Step 4: Get the matching extended warranty in the Map by extended warranty!
  
    
   for(Unit__c newUnit :trigger.new) {
  if(ewrntyMap.containsKey(newUnit.Order_No__c+'_'+newUnit.Product_Code__c)) {
    Extended_Warranty__c ordnm = ewrntyMap.get(newUnit.Order_No__c+'_'+newUnit.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;
      }
     
    }
  }

}

 
venkat-Dvenkat-D
IF both Unit and Extended Warranty has matching fields Order_No__c and Product_Code__c then this trigger should work. can you check data as well once?
BobBob
I apoligize. The API name for the extended warranty object is Name 
BobBob
I want to assign the Extended Warranty name to the Unit lookup field Ext_Warranty__c 
JeffreyStevensJeffreyStevens
You know - I went back and re-read your first post.  So the Order_No__c and the Product_Code__c are BOTH fields on the ExtendedWarranty object - right?

Then why wouldn't step 1 and 2 be replaced with...?
 
// Step1
set<string> ProductCodesToSelect = new set<string>();
set<string> OrderNosToSelect = new set<string>();

for (unit__c u :trigger.new) {
  ProductCodesToSelect.add(u.Product_Code__c);
  OrderNosToSelect.add(u.Order_No__c);
}

// Step 2
list<Extended_Warranty__c> potentialproducts = [SELECT id,name,Order_No__c,Product_Code__c FROM Extended_Warranty__c WHERE Product_Code__c IN :ProductCodesToSelect AND Order_No__c IN :ProductCodesToSelect];

 
JeffreyStevensJeffreyStevens
Ya - I'd put a debug statement after step 2 to make sure that your potentialProducts list has something in it.  (sorry - you called the list potentialProduct, I ususally use a plural name when I return a list - just helps me on remembering if I'm dealing with one or many items).
venkat-Dvenkat-D
Bob,i feel like some issue in API names or relationship data. are you able to figure it out or need further help?
BobBob
I did not get it to work and got frustrated and stop working on it.  My edited code is below and i know it far from being correct. 
 
trigger TriggerUpdateUnitExtWarranty on Unit__c (before insert, before update) {
  
  // Step 1: Create a set of all extended warranties of Units to query
set<string> ProductCodesToSelect = new set<string>();
set<string> OrderNosToSelect = new set<string>();

for (unit__c u :trigger.new) {
  ProductCodesToSelect.add(u.Product_Code__c);
  OrderNosToSelect.add(u.Order_No__c);
}

  
  

  // Step 2: Query for all the Units in Step 1
  list<Extended_Warranty__c> potentialproducts = [SELECT id,Name,Order_No__c,Product_Code__c FROM Extended_Warranty__c WHERE 
  Product_Code__c IN :ProductCodesToSelect AND Name IN :OrderNosToSelect];

  // 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 : potentialproducts) {
    ewrntyMap.put(w.Order_No__c+'_'+w.Product_Code__c, w);
  }

  // Step 4: Get the matching extended warranty in the Map by extended warranty!
  
    
   for(Unit__c u :trigger.new) {
  if(ewrntyMap.containsKey(u.Name+'_'+u.Product_Code__c)) {
    Extended_Warranty__c ordnm = ewrntyMap.get(u.Name+'_'+u.Product_Code__c);
  


  //ordnm is the name of the extended warranty record that is associated with the unit.      
    if (ordnm != null) {
      u.Ext_Warranty__c  = ordnm.Id;
      }
     
    }
  }

}