You need to sign in to do that
Don't have an account?
Apex Test Class giving Error: Too many SOQL queries
I have a Apex Test Class that has been running fine. I just trying to deploy a new trigger in my production org on the same object and it caused an error in the test class.
Failure Message: "System.LimitException: Too many SOQL queries: 21", Failure Stack Trace: "Class.ContractTestFactory.ContractsonAccount: line 21, column 27 External entry point"
Here is my test class:
@isTest
private class ContractTestFactory{
private static TestMethod void ContractsonAccount()
{
test.startTest();
Account myAccount=new Account(name='testacct10',recordtypeid='01270000000Dvnf',
lead_converted_date__c=date.today());
insert(myAccount);
Contract myContract = new Contract(recordtypeid='01270000000DwX4',
accountid=myAccount.id,StartDate=date.today());
insert(myContract);
myContract.RPM_Signed_Contract__c=1;
update(myContract);
//List<Account> insertedAccounts = [SELECT Name, RPM_Signed_Contracts__c
//FROM Account];
List<Account> accts = [SELECT id, name FROM account
WHERE name='testacct10'];
System.assertEquals(null,myAccount.RPM_Signed_Contracts__c);
test.stopTest();
}
Any ideas why I might be getting this message now?
Is there any other code running or workflow triggered as part of the this test? Take a look at the log and see what else is being run.
JDL
You can replace the statements -
insert(myContract);
myContract.RPM_Signed_Contract__c=1;
update(myContract);
by
myContract.RPM_Signed_Contract__c=1;
insert(myContract);
That should lead to some reduction in SOQL statements.
Looks like you have a after insert - before insert or after update - before update trigger.
In your trigger, you should check that you dont have SOQL queries inside the for loop as that will hit limitation of 20 SOQL queries.
Before issuing any SOQL query, all the necessary data should be collected which will be part of SOQL query.
If you can post the trigger code it will be helpful to find the root cause for too many soql queries issue.
I actually added two new triggers that might affect this class. They are modified versions of an existing trigger I have so I'm not sure why they would be giving me trouble now. Any ideas?
Here is the first one:
trigger ContractSumonOppTrigger on Contract
(after delete, after insert, after undelete, after update) {
Contract[] cons;
if (Trigger.isDelete)
cons = Trigger.old;
else
cons = Trigger.new;
// get list of opptys
Set<ID> oppIds = new Set<ID>();
for (Contract con : cons) {
oppIds.add(con.Opportunity__c);
}
Map<ID, Contract> contractsForOpps = new Map<ID, Contract>([select Id
,Opportunity__c
from Contract
where Opportunity__c in :oppIds
]);
Map<ID, Opportunity> oppsToUpdate = new Map<ID, Opportunity>([select Id
,Total_Contracts__c
from Opportunity
where Id in :oppIds]);
for (Opportunity opp : oppsToUpdate.values()) {
Set<ID> conIds = new Set<ID>();
for (Contract con : contractsForOpps.values()
) {
if (con.Opportunity__c == opp.Id)
conIds.add(con.Id);
}
if (opp.Total_Contracts__c != conIds.size())
opp.Total_Contracts__c = conIds.size();
}
update oppsToUpdate.values();}
and the second one:
trigger PdgApprvlCtrtonOppTrigger on Contract
(after delete, after insert, after undelete, after update) {
Contract[] cons;
if (Trigger.isDelete)
cons = Trigger.old;
else
cons = Trigger.new;
// get list of opptys
Set<ID> oppIds = new Set<ID>();
for (Contract con : cons) {
oppIds.add(con.Opportunity__c);
}
Map<ID, Contract> contractsForOpps = new Map<ID, Contract>([select Id
,Opportunity__c
from Contract
where Opportunity__c in :oppIds
and Apprvl_Status_Ind__c='RPM Admin Approved']);
Map<ID, Opportunity> oppsToUpdate = new Map<ID, Opportunity>([select Id
,Contracts_Pending_Sig__c
from Opportunity
where Id in :oppIds]);
for (Opportunity opp : oppsToUpdate.values()) {
Set<ID> conIds = new Set<ID>();
for (Contract con : contractsForOpps.values()
) {
if (con.Opportunity__c == opp.Id)
conIds.add(con.Id);
}
if (opp.Contracts_Pending_Sig__c != conIds.size())
opp.Contracts_Pending_Sig__c = conIds.size();
}
update oppsToUpdate.values();}
Ok after a little more research and some trial and error, I know which line is causing the error, but still don't know how to fix it.
Here is the trigger causing my problems (the red portion is the line causing the error):
trigger PdgApprvlCtrtonOppTrigger on Contract
(after delete, after insert, after undelete, after update) {
Contract[] cons;
if (Trigger.isDelete)
cons = Trigger.old;
else
cons = Trigger.new;
// get list of opptys
Set<ID> oppIds = new Set<ID>();
for (Contract con : cons) {
oppIds.add(con.Opportunity__c);
}
Map<ID, Contract> contractsForOpps = new Map<ID, Contract>([select Id
,Opportunity__c
from Contract
where Opportunity__c in :oppIds
and Apprvl_Status_Ind__c='RPM Admin Approved']);
Map<ID, Opportunity> oppsToUpdate = new Map<ID, Opportunity>([select Id
,Contracts_Pending_Sig__c
from Opportunity
where Id in :oppIds]);
for (Opportunity opp : oppsToUpdate.values()) {
Set<ID> conIds = new Set<ID>();
for (Contract con : contractsForOpps.values()
) {
if (con.Opportunity__c == opp.Id)
conIds.add(con.Id);
}
if (opp.Contracts_Pending_Sig__c != conIds.size())
opp.Contracts_Pending_Sig__c = conIds.size();
}
update oppsToUpdate.values();}
Any ideas how I can re-write this to avoid the error? I'm trying to count the number of related contracts where the approval status field is "RPM Admin Approved" and populate that number on the opportunity. Looking for the roll-up summary functionality for a lookup relationship.
Thanks!