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
jmburnsjmburns 

SOQL Limits

Hey guys,

 

Im new to developing in Apex and am running into a problem, I was hoping to find some help!

 

I have a trigger which creates or updates a contact based on information on the parent object (B2B_Lead__c), I am running into the follow error "UpdateContactB2B: System.LimitException: Too many SOQL queries: 101"  Here is my code, I would appreciate any help.

 

 

trigger UpdateContactB2B on B2B_Lead__c (after update)
{
      List<Contact> contacts = new List<Contact>();
     
      if (trigger.isInsert || trigger.isUpdate)
      {
            for (B2B_Lead__c pnc : trigger.new)
                 
            if (pnc.First_Name__c != '' && pnc.Last_Name__c != '')
            {
                  List<Contact> pncContacts = [SELECT Id, FirstName, LastName, Email,
                                                                        Middle_Name__c, MobilePhone, HomePhone,
                                                                        Work_Phone__c, MailingCity, MailingState, MailingStreet,
                                                                        MailingPostalCode, ContactType__c
                                                            FROM Contact WHERE B2B_Lead__c = :pnc.Id];
                       
                  Contact primary = null;
                  Contact secondary = null;
                 
                  // primary contact logic                                                     
                  if (pncContacts != null && pncContacts.size() > 0)
                  {
                        for (Contact c : pncContacts)
                        {
                              if (c.ContactType__c == 'Primary')
                              {
                                    primary = c;
                              }
                             
                              if (c.ContactType__c == 'Co-signer')
                              {
                                    secondary = c;
                              }
                        }
                  }
 
                  if (primary == null)
                  {
                        primary = new Contact(B2B_Lead__c = pnc.Id);
                        primary.FirstName = pnc.First_Name__c;
                        primary.LastName = pnc.Last_Name__c;
                        
                        primary.Email = pnc.Direct_Email__c;
                        
                        primary.Work_Phone__c = pnc.Direct_Phone__c;
                       
                        primary.MailingStreet = pnc.Address__c;
                        primary.MailingCity = pnc.City__c;
                        primary.MailingPostalCode = pnc.Zip_Code__c;
                        primary.MailingState = pnc.State__c;
                        primary.ContactType__c = 'Primary';
                  }
                  else
                  {
                        primary.FirstName = pnc.First_Name__c;
                        primary.LastName = pnc.Last_Name__c;
                       
                        primary.Email = pnc.Direct_Email__c;
                       
                        primary.Work_Phone__c = pnc.Direct_Phone__c;
                        
                        primary.MailingStreet = pnc.Address__c;
                        primary.MailingCity = pnc.City__c;
                        primary.MailingPostalCode = pnc.Zip_Code__c;
                        primary.MailingState = pnc.State__c;
                  }
                       
                  
                       
                  contacts.add(primary);
            }
      }
     
      if (contacts.size() > 0)
      {
            upsert contacts;
      }
}

 

Best Answer chosen by Admin (Salesforce Developers) 
Ankit AroraAnkit Arora

I think by taking this query out of for loop your problem will be resolved :

 

List<Contact> pncContacts = [SELECT Id, FirstName, LastName, Email,
                                                                        Middle_Name__c, MobilePhone, HomePhone,
                                                                        Work_Phone__c, MailingCity, MailingState, MailingStreet,
                                                                        MailingPostalCode, ContactType__c
                                                            FROM Contact WHERE B2B_Lead__c = :pnc.Id];

 Now you need to get all contact related to records of trigger so query will change like this :

 

List<Contact> pncContacts = [SELECT Id, FirstName, LastName, Email,
                                                                        Middle_Name__c, MobilePhone, HomePhone,
                                                                        Work_Phone__c, MailingCity, MailingState, MailingStreet,
                                                                        MailingPostalCode, ContactType__c
                                                            FROM Contact WHERE B2B_Lead__c = : Trigger.new.keySet()];

 Let me know if it works for you, if not then I will provide you the full code.

 

 

Thanks

Ankit Arora

Blog | Facebook | Blog Page

All Answers

Ankit AroraAnkit Arora

I think by taking this query out of for loop your problem will be resolved :

 

List<Contact> pncContacts = [SELECT Id, FirstName, LastName, Email,
                                                                        Middle_Name__c, MobilePhone, HomePhone,
                                                                        Work_Phone__c, MailingCity, MailingState, MailingStreet,
                                                                        MailingPostalCode, ContactType__c
                                                            FROM Contact WHERE B2B_Lead__c = :pnc.Id];

 Now you need to get all contact related to records of trigger so query will change like this :

 

List<Contact> pncContacts = [SELECT Id, FirstName, LastName, Email,
                                                                        Middle_Name__c, MobilePhone, HomePhone,
                                                                        Work_Phone__c, MailingCity, MailingState, MailingStreet,
                                                                        MailingPostalCode, ContactType__c
                                                            FROM Contact WHERE B2B_Lead__c = : Trigger.new.keySet()];

 Let me know if it works for you, if not then I will provide you the full code.

 

 

Thanks

Ankit Arora

Blog | Facebook | Blog Page

This was selected as the best answer
jmburnsjmburns

Thank you very much