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 

Too many SOQL queries: 101 on bulk load

Can anyone help with my trigger. Im trying to do a data load to the object but keep getting this error. I cant seem to see why my trigger is having the issue i thought i bulkified it..

trigger mainShipToAddessTrigger on ShipTo_Address__c (before insert, before update) {
   List<ShipTo_Address__c> slist = new List<ShipTo_Address__c>();
   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);

           c.MailingStreet = s.Address__c;
           c.MailingCity = s.City__c;
           c.MailingState = s.State__c;
           c.MailingCountry = s.Country__c;
           c.MailingPostalCode = s.ZIP__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 = s.Address__c;
           c.OtherCity = s.City__c;
           c.OtherState = s.State__c;
           c.OtherCountry = s.Country__c;
           c.OtherPostalCode = s.ZIP__c;

           update c; 
       }
   }
}


Best Answer chosen by Ivan Winzer
Jim JamJim Jam
You still need to add 'c' to the contactsToUpdate list ie. contactsToUpdate.add(c); ... at the end of each if block. 

All Answers

Jim JamJim Jam
Your update statements are all within the for loop. It would be better to add the contact object 'c' to a list and then update the list at the end, outside the for loop. 
Ivan WinzerIvan Winzer
So something like this.... I commented it out the two updates in the for loop.

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

           c.MailingStreet = s.Address__c;
           c.MailingCity = s.City__c;
           c.MailingState = s.State__c;
           c.MailingCountry = s.Country__c;
           c.MailingPostalCode = s.ZIP__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 = s.Address__c;
           c.OtherCity = s.City__c;
           c.OtherState = s.State__c;
           c.OtherCountry = s.Country__c;
           c.OtherPostalCode = s.ZIP__c;

  //         update c; 
       }
       update contactsToUpdate;
   }
}


Jim JamJim Jam
You still need to add 'c' to the contactsToUpdate list ie. contactsToUpdate.add(c); ... at the end of each if block. 
This was selected as the best answer
Bhawani SharmaBhawani Sharma
after the line 13 and 25, add 
contactsToUpdate.add(c);



statement. and just before line# 31,
update contactsToUpdate;


Ivan WinzerIvan Winzer
Thanks, both answers helped, now only issue i have is my test class is only passing at 41%. This is what i have can you see why. There are other triggers that i probably need to hit but not sure how to get that to happen i thought i was making it so all would fire.

@isTest
public with sharing class Test_ShiptoAddressTrigger {
    @isTest
    public static void testCreateShipToAddress(){
        Account a = new Account(Name = 'Test MainShipToAddressTrigger', RecordTypeID = '01240000000UZ4V');
        insert a;
        
        Contact c = new Contact(AccountID = a.ID, firstName = 'Test MainShipToAddressTrigger', lastName = 'Tester', email = 'fakington@gmail.com');
        insert c;
        
        ShipTo_Address__c s = new ShipTo_Address__c( Contact__c = c.id,
           Address__c = '1919 Fakeone Way',
           City__c = 'Fakington',
           State__c = 'CA',
           Country__c = 'USA',
           ZIP__c = '00000',
           Primary_Billing_Address__c = true,
           Default_Shipping_Address__c = false);
        insert s;
        
        c = [select AccountId,Account.Name from Contact where id = :c.id limit 1];
           System.assertEquals('Test MainShipToAddressTrigger', c.Account.Name);
           System.assertEquals(a.id, c.AccountId);
    }
}

But it still saying that my coverage on a few other triggers is low and im miss hiting some of the triggers or classes.
Jim JamJim Jam
Maybe after inserting 's' (ShipToAddress__c object) and doing your System.asserts, update 's' so Default_Shipping_Address__c = true.
Ivan WinzerIvan Winzer
So after all that work when i try to do an update to the records in my address object im now getting a duplicate error:


system.listexception: duplicate id in list: 003400000cWe1BAAS trigger.mainshiptoaddressTrigger: line 29, column 1

Seems it does not like my update statement....
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);

           c.MailingStreet = s.Address__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 = s.Address__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;
   }  
}