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
AlSawtoothAlSawtooth 

How can I bulkify this trigger?

trigger UpdateEmployerAddress on Contact (before insert, before update) {
    Set<String> setoforgs = new Set<String>();
    for (Contact inloop : Trigger.new) {
    
    if(inloop.Employer_Lookup__c !=null) {
    
     List<Organization__c> orgs = 
         [SELECT Id, Mailing_Street__c, Mailing_City__c, Mailing_State__c, Mailing_Zip_Code__c, Mailing_Country__c 
          FROM Organization__c 
          WHERE Id = :inloop.Employer_Lookup__c];
             
          if(orgs.size()>0){
          
          for (Organization__c o :orgs){
          
          inloop.cv__Employer_Street__c = o.Mailing_Street__c;
          inloop.cv__Employer_City__c = o.Mailing_City__c;
          inloop.cv__Employer_State__c = o.Mailing_State__c;
          inloop.cv__Employer_Postal_Code__c = o.Mailing_Zip_Code__c;
          inloop.cv__Employer_Country__c = o.Mailing_Country__c;}
                
                
}}}}

AshlekhAshlekh
Hi,

Below code helps you 
trigger UpdateEmployerAddress on Contact (before insert, before update) 
{
    Set<String> setoforgs = new Set<String>();
    for (Contact inloop : Trigger.new) 
	{
		if(inloop.Employer_Lookup__c !=null) 
		{
			setoforgs.add(inloop.Employer_Lookup__c);
		}
	}
	MAp<id,Organization__c> orgs = new Map<id,Organization__c>( [SELECT Id, Mailing_Street__c, Mailing_City__c, Mailing_State__c, 
											Mailing_Zip_Code__c, Mailing_Country__c 
											FROM Organization__c  WHERE Id in :setoforgs]);

    for (Contact inloop : Trigger.new) 
	{
		if(inloop.Employer_Lookup__c !=null) 
		{
             
          if(orgs.size()>0 &&  orgs.containsKey(inloop.Employer_Lookup__c))
		  {
			  Organization__c o = orgs.get(inloop.Employer_Lookup__c);
			  inloop.cv__Employer_Street__c = o.Mailing_Street__c;
			  inloop.cv__Employer_City__c = o.Mailing_City__c;
			  inloop.cv__Employer_State__c = o.Mailing_State__c;
			  inloop.cv__Employer_Postal_Code__c = o.Mailing_Zip_Code__c;
			  inloop.cv__Employer_Country__c = o.Mailing_Country__c;
          }
		}
	}
}

AlSawtoothAlSawtooth
Thank you so much!! This trigger compiles and works - but now my test code is throwing an error (line 21 - expected 'x value', got null). Did I not write this correctly?

@isTest
private class UpdateEmployerAddressTest {

    static testMethod void EmpTestMethod() {
       Organization__c o1 = new Organization__c();
       o1.Name ='Test Org';
       o1.Mailing_Street__c = '123 Main Street';
       o1.Mailing_City__c = 'Brooklyn';
       insert (o1);

       Contact cons = new Contact();
       cons.LastName = 'Tester';
       cons.cv__Employer__c='Test Org';    
       insert(cons);
       
       Contact cons2 = new Contact();
       cons2.LastName = 'Tester2';
       cons2.cv__Employer__c = 'Nope';
       insert(cons2);
       
       System.assertEquals(cons.cv__Employer_City__c, o1.Mailing_City__c);
       System.assertNotEquals(o1.Id, cons2.Employer_Lookup__c);
       System.assertNotEquals(o1.Mailing_City__c, cons2.cv__Employer_City__c);
      }
    }