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
JBerndJBernd 

Best practice for multiple field updates from custom lookup object

Hi
As a newbie to Apex, I'm looking for pointers on how to best formulate a bulkified trigger.
The intent of the trigger is to take all trigger.new on the contact object where field1 OR field2 OR field3 OR field4 are empty. 
Then I want to match a set of the zip codes in the above selection of trigger.new contact records to a custom lookup object.
I want to match the zips in my set to zips in the custom lookup.
Then I want to update fields on the contact object with corresponding values for Field1, Field2, Field3, Field4, but only where Field1, Field2, Field3, Field4 are empty. (In other words, I don't want to overwrite a value that already exists in any of these fields on the contact object).

Question:
There could be times when >50k records will be loaded, but most of the time, it will be small numbers with the occasional list load.

How would you best bulkify this?  [Where would you make sets/maps - at what point would you update]

Thanks
JBerndJBernd
PS
This is the trigger I have working, but not sure it's written optimally. I could easily do the if statements in the first section, but I'm wondering best practice to actually do the update.

trigger updateContact1 on Contact (before insert, before update) {

    //Create a set of Zipcodes
    Set<String> setZips = new Set<String>(); 
    //Select new Contacts - set variable. Trigger.new means for those new contacts
    for (Contact c: trigger.new){
        setZips.add(c.MailingPostalCode);
    }
  
    //Collect the Zips in the Zip Chart with the Zips. Get the right fields from Zip Chart in the list
    List<ZipCounty_Chart__c> zLats = [SELECT ID, Field1__c, Zip__c, Field2__c, Field3__c, Field4__c, Field5__c FROM        ZipCounty_Chart__c WHERE Zip__c in :setZips ];
    //Now you have the relevant Zips set datatype for Map and variable and parens signal data will enter
  
    Map<String, Double> zip1 = new Map <String, Double>();
    Map<String, Double> zip2 = new Map <String, Double>();
    Map<String, Double> zip3= new Map <String, Double>();
    Map<String, Boolean> zip4 = new Map <String, Boolean>();
    Map<String, String> zip5 = new Map <String, String>();
   
   
    //Now loop through the set and set new variables value of these recs
    for (ZipCounty_Chart__c myCounty :zLats) {
     
      zip1.put(myCounty.Zip__c, myCounty.Field1__c);
      zip2.put(myCounty.Zip__c, myCounty.Field2__c);
      zip3.put(myCounty.Zip__c, myCounty.Field3__c);
      zip4.put(myCounty.Zip__c, myCounty.Field4__c);
      zip5.put(myCounty.Zip__c, myCounty.Field5__c);
    
    }
   
   //Now loop through this map set and update the contact field when it finds the match to the zip 
    for (Contact updateCont: trigger.new) {
    //Use update variable just set to update specified field on Contact object
   
    updateCont.Field1__c=Double.valueOf(zip1.get(updateCont.MailingPostalCode));
    updateCont.Field2__c=Double.valueOf(zip2.get(updateCont.MailingPostalCode));
    updateCont.Field3__c=Double.valueOf(zip3.get(updateCont.MailingPostalCode));
    updateCont.Field4__c=Boolean.valueOf(zip4.get(updateCont.MailingPostalCode));
    updateCont.Field5__c=String.valueOf(zip5.get(updateCont.MailingPostalCode));  
    }
 
}