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
RossGRossG 

Too many soql queries error in trigger code

Hi,

I've got a new bit of coe for my lead trigger.  This should just push a custom object related to the lead, upon conversion, to the generated contact.

I'm hittinga too many soql queries on deployment and was wondering if anyone sees how I could optimize this bit of code to get around that.  The code works fine in sandbox in testing but does throw this error when deploying to prod.  Here's the
trigger leadTtrigger on Lead (before update) {   
 if (Trigger.isBefore) {
    
        // BEGIN PUSH PingOne Log records to contact on lead convert //
        
        if(Trigger.isUpdate){
        
       
        Set<Id> leadIds = new Set<Id>();
        Map<Id,Id> LeadIdContactId = new Map<Id,Id>();
    
        for (Integer i = 0; i < Trigger.new.size(); i++){
            if (Trigger.new[i].IsConverted == true && Trigger.old[i].isConverted == false){
                 leadIds.add(Trigger.new[i].id);
                 LeadIdContactId.put(Trigger.new[i].id,Trigger.new[i].ConvertedContactId);
            }
        }
       
        List< PingOne_Log__c > CustomObjectsToUpdate = new List< PingOne_Log__c >();              
        List< PingOne_Log__c > customObjectEntries = [select Contact__c, Lead__c from PingOne_Log__c where lead__c in :leadIds];       
       
    
        for (Lead lead : [select id,ConvertedContactId from Lead where id in: leadIds])  {
                 for (PingOne_Log__c CustomObject : customObjectEntries) {
                    if (CustomObject.Lead__c == lead.Id) {
                        CustomObject.contact__c = LeadIdContactId.get(lead.id);
                        CustomObjectsToUpdate.add(CustomObject);
                     }
                }
         }
    
         if(CustomObjectsToUpdate.size()>0)
             update CustomObjectsToUpdate;  
         } 

}

code:


Ramesh KallooriRamesh Kalloori
try the below.

trigger leadTtrigger on Lead (before update) {   
 if (Trigger.isBefore) {
    
        // BEGIN PUSH PingOne Log records to contact on lead convert //
        
        if(Trigger.isUpdate){
        
       
        Set<Id> leadIds = new Set<Id>();
        Map<Id,Id> LeadIdContactId = new Map<Id,Id>();
    
        for (Integer i = 0; i < Trigger.new.size(); i++){
            if (Trigger.new[i].IsConverted == true && Trigger.old[i].isConverted == false){
                 leadIds.add(Trigger.new[i].id);
                 LeadIdContactId.put(Trigger.new[i].id,Trigger.new[i].ConvertedContactId);
            }
        }
       
        List< PingOne_Log__c > CustomObjectsToUpdate = new List< PingOne_Log__c >();              
        List< PingOne_Log__c > customObjectEntries = [select Contact__c, Lead__c from PingOne_Log__c where lead__c in :leadIds];       
       
    List<Lead> ListLeads=[select id,ConvertedContactId from Lead where id in: leadIds];
        for (Lead lead : ListLeads)  {
                 for (PingOne_Log__c CustomObject : customObjectEntries) {
                    if (CustomObject.Lead__c == lead.Id) {
                        CustomObject.contact__c = LeadIdContactId.get(lead.id);
                        CustomObjectsToUpdate.add(CustomObject);
                     }
                }
         }
    
         if(CustomObjectsToUpdate.size()>0)
             update CustomObjectsToUpdate;  
         } 

}

thanks,
Ramesh
RossGRossG
Thanks Rameesh.  Looks like I still get the error though.
Ramesh KallooriRamesh Kalloori
try this once.

trigger leadTtrigger on Lead (before update) {   
 if (Trigger.isBefore&&Trigger.isUpdate) {
    
        // BEGIN PUSH PingOne Log records to contact on lead convert //
        
        
        
       
        Set<Id> leadIds = new Set<Id>();
        Map<Id,Id> LeadIdContactId = new Map<Id,Id>();
    
        for (Integer i = 0; i < Trigger.new.size(); i++){
            if (Trigger.new[i].IsConverted == true && Trigger.old[i].isConverted == false){
                 leadIds.add(Trigger.new[i].id);
                 LeadIdContactId.put(Trigger.new[i].id,Trigger.new[i].ConvertedContactId);
            }
        }
       
        List< PingOne_Log__c > CustomObjectsToUpdate = new List< PingOne_Log__c >();              
        List< PingOne_Log__c > customObjectEntries = [select Contact__c, Lead__c from PingOne_Log__c where lead__c in :leadIds];       
       
    List<Lead> ListLeads=[select id,ConvertedContactId from Lead where id in: leadIds];
        for (Lead lead : ListLeads)  {
                 for (PingOne_Log__c CustomObject : customObjectEntries) {
                    if (CustomObject.Lead__c == lead.Id) {
                        CustomObject.contact__c = LeadIdContactId.get(lead.id);
                        CustomObjectsToUpdate.add(CustomObject);
                     }
                }
         }
    
}
    if(CustomObjectsToUpdate.size()>0)
      {
             update CustomObjectsToUpdate;  
      }
}

thanks,
Ramesh