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
Randall ToepferRandall Toepfer 

APEX test passes in Sandbox but fails when deploying to Production

Why would a "run all tests" pass in Sandbox but when deployed to production I get an error too many SOQL queries?
 
trigger ZD_Opportunity_BeforeInsert_BeforeUpdate on Opportunity (before insert, before update) {
        
    ZD_Opportunity_Utility.createCongaTemplate(Trigger.new, false);
}
 
public static String getCongaTemplateId(String sTemplateName)
    {
        if(sTemplateName == '')
            throw new ZD_Exception('Error getCongaTemplateId param');
                
        if(m_mTemplates == null)
            m_mTemplates = new Map<String, APXTConga4__Conga_Template__c>();
        
        // search Conga object for template name then get id
        String sResult = '';
        APXTConga4__Conga_Template__c oTemplate;
        
        if(m_mTemplates.containsKey(sTemplateName))
        {
            oTemplate = m_mTemplates.get(sTemplateName);
        
        	sResult = '' + oTemplate.Id; 
        }
        else
        {        
            List<APXTConga4__Conga_Template__c> lCongaTemplates = [
                SELECT
                    Id,
                    Name
                FROM
                    APXTConga4__Conga_Template__c
                WHERE
                    APXTConga4__Name__c = :sTemplateName
            ];        
            if(lCongaTemplates.size() == 0)
            {
                // throw new ZD_Exception('Error finding Conga Template ' + sTemplateName);
                sResult = 'xxx';
            }
            else
            {
                oTemplate = lCongaTemplates[0];
                
                m_mTemplates.put(oTemplate.Name, oTemplate);
                        
                sResult = '' + oTemplate.Id; 
            }
        }
        
        return sResult;
    }
    
    public static void createCongaTemplate(List<Opportunity> lOpportunities, Boolean bAfter) {
        Boolean bDataChange = false;
        
        for(Opportunity oOpportunity : lOpportunities)
        {           
            // get all opportunity products
            List<OpportunityLineItem> lLineItems = [
                SELECT 
                	Id, 
                	Product2.Id,
                	Product2.Name,
                	Product2.Family
                FROM 
                	OpportunityLineItem
                WHERE 
                	OpportunityId = :oOpportunity.Id
            ];
            
            // template string
            String sTemplateIds = '';
            String sScheduleA = '';
            String sContract = '';
            String sExhibitA = '';
            String sExhibitB = '';
            String sExhibitC = '';
            String sExhibitD = '';
            String sSpecSheets = '';
            
            // template order: schedule A, contract, exhitbit a - warranty, exhibit b - ppg, exhibit c - consumer protect plan, exhibit d - cancel notice
            
            // get opportunity record type
        	List<RecordType> lOpportunityRecordTypes =  [
                SELECT 
                	Id, 
                	Name, 
                	DeveloperName 
                FROM 
                	RecordType 
                WHERE 
                	Id = :oOpportunity.RecordTypeId
            ];     
            
            if(lOpportunityRecordTypes.size() == 0)
                throw new ZD_Exception('Error opportunity record type');
                        
            // Energy Efficiency
            if(lOpportunityRecordTypes[0].DeveloperName == 'Residential_EE')
            {
                // find schedule A template for EE       
                sScheduleA = ZD_Opportunity_Utility.getCongaTemplateId('EE Schedule A'); 
                
                // find contract template for EE
                sContract = ZD_Opportunity_Utility.getCongaTemplateId('EE Contract'); 
            }
            
            // default to PV Install
            else 
            {
                // find schedule A template for PV Install 
                sScheduleA = ZD_Opportunity_Utility.getCongaTemplateId('PV Install Schedule A');
                
                // find contract template for PV Install
                sContract = ZD_Opportunity_Utility.getCongaTemplateId('PV Install Contract'); 
                
                // setup warranties
                
                // keep track of warranties and spec sheets we have already added
                Set<String> sWarranties = new Set<String>();
                Set<String> sSpecs = new Set<String>();
                
                // loop through each line item and get warranty and spec sheets
                for(Integer ix = 0; ix < lLineItems.size(); ix++)
                {
                    Id oProductId = lLineItems[ix].Product2.Id;
                    
                    // get warranties
                    List<Warranty_Junction__c> lWarranties = [
                        SELECT 
                        	Conga_Template__c 
                        FROM 
                        	Warranty_Junction__c 
                        WHERE 
                        	Product__c = :oProductId
                    ];
                    
                    for(Integer jx = 0; jx < lWarranties.size(); jx++)
                    {
                        String sId = '' + lWarranties[jx].Conga_Template__c;
                        
                        // add each warranty to set
                        if(sWarranties.contains(sId))
                            continue;
                        
                        sWarranties.add(sId);
                    }
                    
                    // get spec sheets
                    List<Spec_Sheet_Junction__c> lSpecSheets = [
                        SELECT 
                        	Conga_Template__c 
                        FROM 
                        	Spec_Sheet_Junction__c 
                        WHERE 
                        	Product__c = :oProductId                        
                    ];
                    
                    for(Integer jx = 0; jx < lSpecSheets.size(); jx++)
                    {
                        String sId = '' + lSpecSheets[jx].Conga_Template__c;
                        
                        // add each warranty to set
                        if(sSpecs.contains(sId))
                            continue;
                        
                        sSpecs.add(sId);
                    }
                }
                
                // add each warranty id to template
                sExhibitA = ZD_Opportunity_Utility.getCongaTemplateId('PV Install Contract - Exhibit A');
                for(String sId : sWarranties)
                {
                    sExhibitA = sExhibitA + ',' + sId;
                }         
                
                // add PPG to template
                sExhibitB = 
                    ZD_Opportunity_Utility.getCongaTemplateId('PV Install Contract - Exhibit B') + ',' +
                    ZD_Opportunity_Utility.getCongaTemplateId('PV Install PPG'); 
                
                // add 25 year consumer protection plan
            	sExhibitC = 
                    ZD_Opportunity_Utility.getCongaTemplateId('PV Install Contract - Exhibit C') + ',' +                    
                    ZD_Opportunity_Utility.getCongaTemplateId('Warranty - SCS 25 Year'); 
                
                // add notice of cancellation
            	sExhibitD = ZD_Opportunity_Utility.getCongaTemplateId('PV Install Contract - Exhibit D');
                
                // add each spec sheet id to template
                for(String sId : sSpecs)
                {
                    if(sSpecSheets.length() == 0)
                    	sSpecSheets = sId;
                    else
                    	sSpecSheets = sSpecSheets + ',' + sId;
                }         
            }
                        
            sTemplateIds = 
                sScheduleA + ',' + 
                sContract + ',' + 
                sExhibitA + ',' +
                sExhibitB + ',' +
                sExhibitC + ',' +
                sExhibitD;
            
            if(sSpecSheets.length() > 0)
                sTemplateIds = sTemplateIds + ',' + sSpecSheets;
            
            String sExistingTemplate = oOpportunity.CongaTemplateIds__c;
            
            if(sExistingTemplate != sTemplateIds)
                bDataChange = true;
            
            // set template field
            oOpportunity.CongaTemplateIds__c = sTemplateIds;            
        }   
        
        if(bAfter && bDataChange)
            update lOpportunities;
    }

 
ManojjenaManojjena
Hi Randall ,
You have issue in createCongaTemplate method as your sandbox have less record so it works . Basically you have SOQL inside for loop .
So it hit the limit you need to remove SOQL from for loop .