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
Ivan WinzerIvan Winzer 

Emergency too many soql queries error

I have an emergency. I need to update over 50k records on our address object. I have a trigger that was working in sandbox but for some reason is now giving a too many soql query error in prod when i try to do a data load using data loader. Can anyone tell me what ive done wrong i thought i had it bulkfied already...
trigger mainShipToAddessTrigger on ShipTo_Address__c (before insert, before update) { 

   List<ShipTo_Address__c> slist = new List<ShipTo_Address__c>();
   List<Contact> contactsToUpdate = new List<Contact>(); 
   for ( ShipTo_Address__c s : trigger.new ) {  
       if ( s.Primary_Billing_Address__c == true) { 
           Contact c = New Contact(Id = s.Contact__c); 
           system.debug('***** UPDATING:'+c.Id+' '+s.Address__c+' '+s.City__c); 
	//	   Address1_2 = String.Valueof(s.Address__c) + String.ValueOf(' ') + String.Valueof(s.Address2__c);
           c.MailingStreet = String.Valueof(s.Address__c) + String.ValueOf(' ') + String.Valueof(s.Address2__c); 
           c.MailingCity = s.City__c; 
           c.MailingState = s.State__c; 
           c.MailingCountry = s.Country__c; 
           c.MailingPostalCode = s.ZIP__c; 
    		contactsToUpdate.add(c);
    //       update c;              

       } 
       if ( s.Default_Shipping_Address__c == true) { 
           Contact c = New Contact(Id = s.Contact__c); 
           system.debug('***** UPDATING:'+c.Id+' '+s.Address__c+' '+s.City__c); 

           c.OtherStreet = String.Valueof(s.Address__c) + String.ValueOf(' ') + String.Valueof(s.Address2__c); 
           c.OtherCity = s.City__c; 
           c.OtherState = s.State__c; 
           c.OtherCountry = s.Country__c; 
           c.OtherPostalCode = s.ZIP__c; 
     		contactsToUpdate.add(c);
     //      update c;  

       } 
		update contactsToUpdate;
   } 

}

 
Best Answer chosen by Ivan Winzer
Ryan GardnerRyan Gardner
You are updating your contactsToUpdate within your for loop. This is an Apex no-no. You need to move your update statement out of the loop to line 34.

All Answers

Ryan GardnerRyan Gardner
You are updating your contactsToUpdate within your for loop. This is an Apex no-no. You need to move your update statement out of the loop to line 34.
This was selected as the best answer
Ivan WinzerIvan Winzer
Thanks so much Ryan. I didnt realize i had moved it when i pushed it over to production. Updated the code and now its working... You rock...
Ivan WinzerIvan Winzer
Morning Ryan not sure if you will get this but ran into a new issue. When trying to go thru and mass update addresses in the object i get the duplicate in list for line 34 column 1. Seems it believes all my records are now duplicates in the list. Should i change this so its a map ID instead of  list

Ivan
Ryan GardnerRyan Gardner
I think I would do 2 things:
  1. Make a set that prevents me from handling a contact more than once.
  2. Add an else to prevent the possibility of adding the same contact twice if PrimaryBillingAddress = true and DefaultShippingAddress is also true. Not sure if you have rules to prevent that elsewhere, but seems like a good safety measure.
trigger mainShipToAddessTrigger on ShipTo_Address__c (before insert, before update) { 

   List<ShipTo_Address__c> slist = new List<ShipTo_Address__c>();
   List<Contact> contactsToUpdate = new List<Contact>();
   Set<Id> contactsToProcess = new Set<Id>();
   
   for ( ShipTo_Address__c s : trigger.new ) {
       Contact c = new Contact(Id = s.Contact__c);

		if (!contactsToProcess.contains(c.id)){
			
			if ( s.Primary_Billing_Address__c == true) {
			   c.MailingStreet = String.Valueof(s.Address__c) + String.ValueOf(' ') + String.Valueof(s.Address2__c); 
			   c.MailingCity = s.City__c; 
			   c.MailingState = s.State__c; 
			   c.MailingCountry = s.Country__c; 
			   c.MailingPostalCode = s.ZIP__c; 
				contactsToUpdate.add(c);              

			}
			else		
			if ( s.Default_Shipping_Address__c == true) { 
			   c.OtherStreet = String.Valueof(s.Address__c) + String.ValueOf(' ') + String.Valueof(s.Address2__c); 
			   c.OtherCity = s.City__c; 
			   c.OtherState = s.State__c; 
			   c.OtherCountry = s.Country__c; 
			   c.OtherPostalCode = s.ZIP__c; 
				contactsToUpdate.add(c); 
			}
			contactsToProcess.add(c.id);
		}
   }
   update contactsToUpdate;
}

Something like that.