You need to sign in to do that
Don't have an account?
willj
Too many SOL queries even with no queries in loop - please help
Getting too many soql queries 101 even though no queries in loop. how do I overcome this? very frustrating.
code is long so divided into two posts.
trigger trac_Lead on Lead (before insert, before update, after insert, after update) { /***************** * Before Insert * *****************/ if(trigger.isInsert && trigger.isBefore) { // Perform SIC mappings for(Lead l : trigger.new) { if (l.sic_lookup__c != null) { l.Industry = l.sic_description__c; } } // Query campaigns and rates List<Campaign> campaignList = new List<Campaign>(); for(Campaign i : DBHelper.getCampaignListQuery()){ if(i.IsActive==true){ campaignList.add(i); } } List<User> userList = DBHelper.getUserDetailQuery(); for (Lead l : trigger.new) { for(User u : userList){ if(u.Id == UserInfo.getUserId()){ if(u.Account_ID__c != null) { for(Account a : DBHelper.getAccountQuery()){ //check if accountid on user detail is valid if(a.Id == u.Account_ID__c){ l.Payment_Advisor__c = u.Account_ID__c; } } } } } // Inject default rates to app from campaign if (l.Campaign__c != null && l.Product_Group__c != null && l.Processor__c != null) { Campaign matchingCampaign; for(Campaign c : campaignList){ if(c.Id==l.Campaign__c) { matchingCampaign = c; break; } } Id selectedRate; if(l.Processor__c == 'FD') { if(l.Product_Group__c == 'Mobile') selectedRate = matchingCampaign.FD_Mobile_Rate__c; else if(l.Product_Group__c == 'In-Store') selectedRate = matchingCampaign.FD_Instore_Rate__c; else if(l.Product_Group__c == 'Online') selectedRate = matchingCampaign.FD_Online_Rate__c; } else if(l.Processor__c == 'Chase') { if(l.Product_Group__c == 'Mobile') selectedRate = matchingCampaign.Mobile_Rate__c; else if(l.Product_Group__c == 'In-Store') selectedRate = matchingCampaign.Rate__c; else if(l.Product_Group__c == 'Online') selectedRate = matchingCampaign.Online_Rate__c; } else if(l.Processor__c == 'PsiGate') { if(l.Product_Group__c == 'Mobile') selectedRate = matchingCampaign.PsiGate_Mobile_Rate__c; else if(l.Product_Group__c == 'In-Store') selectedRate = matchingCampaign.PsiGate_Instore_Rate__c; else if(l.Product_Group__c == 'Online') selectedRate = matchingCampaign.PsiGate_Online_Rate__c; } for(Rate__c r : DBHelper.getRateListQuery()){ if(r.Id==selectedRate) { l.Account_Setup_Fee__c = r.Account_Setup_Fee__c; l.Amex_JCB_Fee_del__c = r.Amex_JCB_Fee_del__c; l.Annual_Fee__c = r.Annual_Fee__c; l.Chargeback__c = r.Chargeback_del__c; l.Cross_Border_Fee__c = r.Cross_Border_Fee__c; l.Deconversion_Cancellation_Fee_per_loc__c = r.Deconversion_Cancellation_Fee_per_loc__c; l.Discover_Rate__c = r.Discover_Rate__c; l.Discover_Rate_Amount__c = r.Discover_Transaction_Fee__c; l.Interac_Fee__c = r.Interac_Fee__c; l.MC_VISA_Authorization__c = r.MC_VISA_Authorization__c; l.MasterCard_Cross_Border_Transaction_Fee__c = r.MasterCard_Cross_Border_Transaction_Fee__c; l.MasterCard_Rate__c = r.MasterCard_Rate__c; l.MasterCard_Rate_Amount__c = r.MasterCard_Transaction_Fee__c; l.Monthly_Account_Fee__c = r.Monthly_Account_Fee__c; l.Monthly_Minimums__c = r.Monthly_Minimums__c; l.Non_Qualifying_Surcharge_Fee__c = r.Non_Qualifying_Surcharge_Fee__c; l.Other_Monthly_Fees__c = r.Other_Monthly_Fees__c; l.VISA_Debit_Rate__c = r.VISA_Debit_Rate__c; l.VISA_Debit_Rate_Amount__c = r.VISA_Debit_Transaction_Fee__c; l.VISA_MasterCard_Card_Brand_Fee__c = r.VISA_MasterCard_Card_Brand_Fee__c; l.Visa_Rate__c = r.VISA_Rate__c; l.VISA_Rate_Amount__c = r.VISA_Transaction_Fee__c; l.Voice_Authorization__c = r.Voice_Authorization__c; l.Wireless_Terminal_Setup_Fee__c = r.Wireless_Terminal_Setup_Fee__c; break; } } } } } /**************** * After Insert * ****************/ if(trigger.isInsert && trigger.isAfter) { // add campaign mappings List<CampaignMember> newCMs = new List<CampaignMember>(); List<Web_Profile__c> newWPs = new List<Web_Profile__c>(); for (Lead l : trigger.new) { if(UserInfo.getUserId() != '005U0000000NmC0'){ newWPs.add(new Web_Profile__c(Applicant__c = l.id, Email__c = l.Email)); } newCMs.add(new CampaignMember(leadid = l.id, campaignid = l.campaign__c)); } insert newCMs; try { insert newWPs; } catch (DmlException de) { for (Lead l : trigger.new) { l.addError('Duplicate Email with an existing lead.'); } } } /***************** * Before Update * *****************/ if(trigger.isUpdate && trigger.isBefore) { Set<Id> leadIds = new Set<Id>(); for (Lead lp : trigger.new) { leadIds.add(lp.id); } List<Lead_Product__c> leadProductQuery = null; leadProductQuery = [SELECT lead__c, product__r.isActive, Card_Types__c, Doing_Business_As_DBA__c, Quantity__c, Term__c, Product__c, product__r.purchase_price__c, product__r.rental_price__c, product__r.lease_price__c, Batch_Processing__c, Customer_Profile_Management__c, Recurring_Billing__c, Shopping_Cart_Option__c, Virtual_Terminal__c, Auto_Close_Hour__c, Auto_Close_Minute__c, Auto_Close__c, Cash_Back_Enabled__c, Invoice_Number__c, QSR_Flag__c, Server_Clerk_ID__c, Tip_Option__c, Connection_Type__c, DNS1__c, DNS2__c, Gateway_Address__c, IP_Address__c, Request_Type__c, Service_Option__c, Special_Instructions__c, Subnet_Mask__c, Add_to_Existing_NetConnect__c, Application_Type__c, Existing_NetConnect_ID__c, Software_Name_Version__c, TID_s__c, VAR_Comm_Method__c, VAR_Name__c, Rental_Cost__c, Rental_Price__c, Lease_Price__c, Purchase_Price__c, Buy_Rate__c FROM Lead_Product__c WHERE lead__c IN :leadIds]; Id LEAD_RT_UNCLAIMED; Id LEAD_RT_MANUAL; for(RecordType rt : DBHelper.getRecordTypes()) { if(rt.SobjectType=='Lead' && rt.name=='Unclaimed Lead'){ LEAD_RT_UNCLAIMED = rt.id; } if(rt.SobjectType=='Lead' && rt.name=='Manually Entered'){ LEAD_RT_MANUAL = rt.id; } } List<CampaignMember> newCMs = new List<CampaignMember>(); Set<String> delCMKeys = new Set<String>(); List<Lead_Product__c> lps = new List<Lead_Product__c>(); for(Lead_Product__c i : leadProductQuery){ if(leadIds.contains((Id)i.lead__c)){ lps.add(i); } } // check that lead has products Set<Id> leadsWithProducts = new Set<Id>(); for(Lead_Product__c i : lps) { if(!leadsWithProducts.contains((Id)i.lead__c)){ leadsWithProducts.add((Id)i.lead__c); } } // check for lead products that have inactive products Set<Id> leadsWithInactiveProducts = new Set<Id>(); for(Lead_Product__c i : lps) { if(!leadsWithInactiveProducts.contains((Id)i.lead__c) && i.product__r.isActive == false){ leadsWithInactiveProducts.add((Id)i.lead__c); } } // Query campaigns and rates List<Campaign> campaignList = new List<Campaign>(); for(Campaign i : DBHelper.getCampaignListQuery()){ if(i.IsActive==true){ campaignList.add(i); } } for (Lead l : trigger.new) { // assign record type based on whether owner is queue or a user if(((String)l.ownerid).startsWith('00G')) { // queue l.recordtypeid = LEAD_RT_UNCLAIMED; } else if (l.recordtypeid == LEAD_RT_UNCLAIMED){ // user, needs RT update l.recordtypeid = LEAD_RT_MANUAL; } // lead is about to be converted if (l.isConverted && !trigger.oldMap.get(l.id).isConverted) { if(!leadsWithProducts.contains(l.id)) { l.addError(system.label.noLeadProducts); } else if(leadsWithInactiveProducts.contains(l.id)) { l.addError(system.label.leadProductWithInactiveProduct); } l.status_date_review_and_sign__c = Datetime.now(); } if (l.sic_lookup__c != null) { l.Industry = l.sic_description__c; } // Find leads where the campaign has been changed if(l.campaign__c != trigger.oldMap.get(l.id).campaign__c) { newCMs.add(new CampaignMember(leadid = l.id, campaignid = l.campaign__c)); if (trigger.oldmap.get(l.id).campaign__c != null) { delCMKeys.add(((String)trigger.oldmap.get(l.id).campaign__c).substring(0,15) + ((String)l.id).substring(0,15)); } } // Update rates if campaign, processor, or product group changed Lead oldLead = Trigger.oldMap.get(l.ID); if (l.Campaign__c != oldLead.Campaign__c || l.Product_Group__c != oldLead.Product_Group__c || l.Processor__c != oldLead.Processor__c) { Campaign matchingCampaign; for(Campaign c : campaignList){ if(c.Id==l.Campaign__c) { matchingCampaign = c; break; } } Id selectedRate; if(l.Processor__c == 'FD') { if(l.Product_Group__c == 'Mobile') selectedRate = matchingCampaign.FD_Mobile_Rate__c; else if(l.Product_Group__c == 'In-Store') selectedRate = matchingCampaign.FD_Instore_Rate__c; else if(l.Product_Group__c == 'Online') selectedRate = matchingCampaign.FD_Online_Rate__c; } else if(l.Processor__c == 'Chase') { if(l.Product_Group__c == 'Mobile') selectedRate = matchingCampaign.Mobile_Rate__c; else if(l.Product_Group__c == 'In-Store') selectedRate = matchingCampaign.Rate__c; else if(l.Product_Group__c == 'Online') selectedRate = matchingCampaign.Online_Rate__c; } else if(l.Processor__c == 'PsiGate') { if(l.Product_Group__c == 'Mobile') selectedRate = matchingCampaign.PsiGate_Mobile_Rate__c; else if(l.Product_Group__c == 'In-Store') selectedRate = matchingCampaign.PsiGate_Instore_Rate__c; else if(l.Product_Group__c == 'Online') selectedRate = matchingCampaign.PsiGate_Online_Rate__c; } for(Rate__c r : DBHelper.getRateListQuery()){ if(r.Id==selectedRate) { l.Account_Setup_Fee__c = r.Account_Setup_Fee__c; l.Amex_JCB_Fee_del__c = r.Amex_JCB_Fee_del__c; l.Annual_Fee__c = r.Annual_Fee__c; l.Chargeback__c = r.Chargeback_del__c; l.Cross_Border_Fee__c = r.Cross_Border_Fee__c; l.Deconversion_Cancellation_Fee_per_loc__c = r.Deconversion_Cancellation_Fee_per_loc__c; l.Discover_Rate__c = r.Discover_Rate__c; l.Discover_Rate_Amount__c = r.Discover_Transaction_Fee__c; l.Interac_Fee__c = r.Interac_Fee__c; l.MC_VISA_Authorization__c = r.MC_VISA_Authorization__c; l.MasterCard_Cross_Border_Transaction_Fee__c = r.MasterCard_Cross_Border_Transaction_Fee__c; l.MasterCard_Rate__c = r.MasterCard_Rate__c; l.MasterCard_Rate_Amount__c = r.MasterCard_Transaction_Fee__c; l.Monthly_Account_Fee__c = r.Monthly_Account_Fee__c; l.Monthly_Minimums__c = r.Monthly_Minimums__c; l.Non_Qualifying_Surcharge_Fee__c = r.Non_Qualifying_Surcharge_Fee__c; l.Other_Monthly_Fees__c = r.Other_Monthly_Fees__c; l.VISA_Debit_Rate__c = r.VISA_Debit_Rate__c; l.VISA_Debit_Rate_Amount__c = r.VISA_Debit_Transaction_Fee__c; l.VISA_MasterCard_Card_Brand_Fee__c = r.VISA_MasterCard_Card_Brand_Fee__c; l.Visa_Rate__c = r.VISA_Rate__c; l.VISA_Rate_Amount__c = r.VISA_Transaction_Fee__c; l.Voice_Authorization__c = r.Voice_Authorization__c; l.Wireless_Terminal_Setup_Fee__c = r.Wireless_Terminal_Setup_Fee__c; break; } } } } // add new campaign members try { insert newCMs; } catch (DmlException de) { // existing member... } // delete old campaign members List<CampaignMember> oldCMs = [SELECT id FROM CampaignMember WHERE key__c IN :delCMKeys]; if (oldCMs.size() > 0) { delete oldCMs; } }
You have to understand testing procedure
for(Integer i=0;i<9;i++) {
Rate__c rate = new Rate__c(
this will cause 10 DMLS you need to make a list of rate and insert it after loop
instead of inserting leadproducts seperatly make a list and do a single insert
Lead_Product__c lp = new Lead_Product__c(lead__c = l.id, product__c = p.id, quantity__c = 2, term__c = 'Purchase', purchase_price__c = 1, lease_price__c = 2, rental_price__c = 3);
insert lp;
Lead_Product__c lp2 = new Lead_Product__c(lead__c = l.id, product__c = p.id, quantity__c = 2, term__c = 'Lease', purchase_price__c = 1, lease_price__c = 2, rental_price__c = 3);
insert lp2;
Lead_Product__c lp3 = new Lead_Product__c(lead__c = l.id, product__c = p.id, quantity__c = 2, term__c = 'Rent - Chase', purchase_price__c = 1, lease_price__c = 2, rental_price__c = 3);
insert lp3;
Lead_Product__c lp4 = new Lead_Product__c(lead__c = l.id, product__c = p2.id, quantity__c = 1, term__c = 'Purchase', purchase_price__c = 1, lease_price__c = 2, rental_price__c = 3);
insert lp4;
Same for leads
Lead l = new Lead(Processor__c = 'Chase', Payment_Advisor__c = pa.id, Product_Group__c = 'In-Store', LeadSource = 'Self', company = 'Test Lead', lastname = 'leadlast', campaign__c = c.id, ownerid = qid, sic_lookup__c = sic1.id, Basis_Points_del__c = 1);
insert l;
Lead l2 = new Lead(Processor__c = 'Chase', Payment_Advisor__c = pa.id, Product_Group__c = 'Online', LeadSource = 'Self', company = 'Test Lead', lastname = 'leadlast', campaign__c = c.id, ownerid = qid, sic_lookup__c = sic1.id, Basis_Points_del__c = 1);
insert l2;
Same for SIC
SIC__c sic1 = new SIC__c(name = '1', description__c = 'industry1');
insert sic1;
SIC__c sic2 = new SIC__c(name = '2', description__c = 'industry2');
insert sic2;
All Answers
second half.
Just try using test.start() / test.stop() for each insert,update or delete
Please mark it as solved if above is what you need
You meant putting those in my trigger test class? They are already in there...test class attached. Is it my trigger or test class problem? My guess is the error for too many queries 101 refers to the test class, not trigger right? if it's trigger, it should be 21?..please shed some light on how to tackle this problem. Thanks
You have to understand testing procedure
for(Integer i=0;i<9;i++) {
Rate__c rate = new Rate__c(
this will cause 10 DMLS you need to make a list of rate and insert it after loop
instead of inserting leadproducts seperatly make a list and do a single insert
Lead_Product__c lp = new Lead_Product__c(lead__c = l.id, product__c = p.id, quantity__c = 2, term__c = 'Purchase', purchase_price__c = 1, lease_price__c = 2, rental_price__c = 3);
insert lp;
Lead_Product__c lp2 = new Lead_Product__c(lead__c = l.id, product__c = p.id, quantity__c = 2, term__c = 'Lease', purchase_price__c = 1, lease_price__c = 2, rental_price__c = 3);
insert lp2;
Lead_Product__c lp3 = new Lead_Product__c(lead__c = l.id, product__c = p.id, quantity__c = 2, term__c = 'Rent - Chase', purchase_price__c = 1, lease_price__c = 2, rental_price__c = 3);
insert lp3;
Lead_Product__c lp4 = new Lead_Product__c(lead__c = l.id, product__c = p2.id, quantity__c = 1, term__c = 'Purchase', purchase_price__c = 1, lease_price__c = 2, rental_price__c = 3);
insert lp4;
Same for leads
Lead l = new Lead(Processor__c = 'Chase', Payment_Advisor__c = pa.id, Product_Group__c = 'In-Store', LeadSource = 'Self', company = 'Test Lead', lastname = 'leadlast', campaign__c = c.id, ownerid = qid, sic_lookup__c = sic1.id, Basis_Points_del__c = 1);
insert l;
Lead l2 = new Lead(Processor__c = 'Chase', Payment_Advisor__c = pa.id, Product_Group__c = 'Online', LeadSource = 'Self', company = 'Test Lead', lastname = 'leadlast', campaign__c = c.id, ownerid = qid, sic_lookup__c = sic1.id, Basis_Points_del__c = 1);
insert l2;
Same for SIC
SIC__c sic1 = new SIC__c(name = '1', description__c = 'industry1');
insert sic1;
SIC__c sic2 = new SIC__c(name = '2', description__c = 'industry2');
insert sic2;
it worked. thank you so much.
One follow up question from a theoretical perspective: what if either in a trigger or a test class, the method requires sequential transaction actions(meaning static queries would not work because data would be out of date by the time it is used) to be done to say 200 objects. It does not matter how efficient you write the queries, the best case on the number of queries required would be 200, right? Wouldn't that hit the 100 query limit? How do we overcome this?
Response is highly appreciated.
Well in such a case we go for batch apex so that we can handel soql limits