You need to sign in to do that
Don't have an account?

System.LimitException :Moving trigger from sandbox to production
Hi All,
I have written a bulkified trigger and a test class for the same with 100% test coverage in sandbox. When I try to move it to Production, I get this error.
TestBC_AverageAmountNew.ideaBatchTest() | Class | 29 | 1 | Failure Message: "System.LimitException: Too many SOQL queries: 101", Failure Stack Trace: "Trigger.BookingQuota: line 29, column 1" |
The above class "TestBC_AverageAmountNew" has been written for a batch class. I am unable to move trigger from sandbox to production.
How can I rectify the problem? Please help.
Thanks,
Alok
Thanks Vishal for your detailed reply. Now i resolved the issue. What i did is that I put all the code inside test.start() and test.stop(). I was inserting opportunities one by one in my test classes. What i have done now is that I have modified that and created a list of all opportunities and inserted the list so the insertion inside a test class is only once now.
Something like this:
Test.start()
Opportunity Oppty1=new opportunity(...);
Opportunity Oppty2=new opportunity(...);
Opportunity Oppty3=new opportunity(...);
Let say list is prb.
prb.add(oppty1);
prb.add(oppty2);
prb.add(oppty3);
insert prb1;
Test.Stop();
This has resolved my issue and I am able to migrate now.
Thanks!
All Answers
Hi Alok,
Have a check on production to see if you have any workflows, triggers etc... that are causing you to get into a loop? If you are running an 'after update' trigger then things can go round-and-round-and-round... Also, look at the quantity of data and see if there is a big difference between sandbox and production.
Difficult to give you a diagnosis without some code (or design) to consider.
Rich.
Hi Richie,
Please check the code below:
trigger BookingQuota on Opportunity (before update, before insert)
{
Set<Id> OwnerIds = new Set<Id>();
for (Opportunity oppty : Trigger.new)
{
OwnerIds.add(oppty.OwnerID);
}
Set<String> usrNameSet = new Set<string>();
for(User usr : [Select Id,Name from User where Id In:OwnerIds])
{
usrNameSet.add(usr.name);
}
List<Opportunity> oppty1=New List<Opportunity>();
/**************************************** Creates the map of users who are in the set OwnerIDs ********************************************/
Map<Id,User> UserMap = new Map<Id,User>([Select id,Name from User where Id in:OwnerIds]);
/***************************************** Creates the map for the records of Booking_Quota__c which are in the set UserNameSer ********************************************/
Map<String,Booking_Quota__c> bookingQuota= New Map<String,Booking_Quota__c>([Select Sales_Person_Name_new__c,Type__c,Annual_Booking_Quota__c from Booking_Quota__c where Sales_Person_Name_new__c In:usrNameSet]);
for(Opportunity Oppty:Trigger.New)
{
for(Booking_Quota__c bok : bookingQuota.Values())
{
if(UserMap.get(oppty.ownerid).name == bok.Sales_Person_Name_new__c && oppty.type==bok.Type__c)
{
oppty.Booking_Quota__c=bok.Annual_Booking_Quota__c;
}
}
}
Hope this helps to understand the problem.
Thanks,
Alok
Hi Alok,
To me your trigger looks fine; perhaps it is your test that's the problem?
Try using a Test.StartTest() after you have setup your data to reset the limits. You could see if adding SeeAllData=true attribute to the testmethod gives a better result.
Good luck!
Rich.
Hi Ritchie,
This is my test class.
@isTest(SeeAllData=True)
public With sharing class Test_BookingQuota
{
static testMethod void ideaBatchTest()
{
Booking_Quota__c book1= new Booking_Quota__c( Annual_Booking_Quota__c =2500080, Sales_Person_Name_new__c='Joan Meyer',Type__c='Non-Recurring');
insert book1;
Booking_Quota__c book2= new Booking_Quota__c( Annual_Booking_Quota__c =2500000, Sales_Person_Name_new__c='Joan Meyer',Type__c='Recurring');
insert book2;
Opportunity Oppty123 = new opportunity(Name='Test1111',StageName='P - Proposal Delivered', amount=60000, type='Non-Recurring',CloseDate=date.newInstance(2012,05,10), OwnerId='00560000001AiiB');
insert oppty123;
}
}
Can you suggest any changes or how to incorporate Test.StartTest() method?
Thanks,
Alok
I faced a similar situation, and on checking with it I came to a point that there was no issue with the code, however the Production Org and it's data was huge, there were multiple triggers which were getting called in a way that insertion of A record would call B, trigger on B would create a record C and C will again call something else..
I won't say I handled it in an efficient way, but since I had no time to make changes in the whole Production code, I fixed it using the below method :
Before every query, I would check if it is running through a "Test method", if yes then I would check with the number of queries using Limits.getQueries and make sure it doesn't query if it is equal to 100.
something like this:
if(Test.isRunningTest() && Limits.getQueries() < 100)
{
List<Account> lstAccounts = [Select Id From Account Where Id IN : someSet];
}
else if(!Test.isRunningTest())
List<Account> lstAccounts = [Select Id From Account Where Id IN : someSet];
Thanks Vishal for your detailed reply. Now i resolved the issue. What i did is that I put all the code inside test.start() and test.stop(). I was inserting opportunities one by one in my test classes. What i have done now is that I have modified that and created a list of all opportunities and inserted the list so the insertion inside a test class is only once now.
Something like this:
Test.start()
Opportunity Oppty1=new opportunity(...);
Opportunity Oppty2=new opportunity(...);
Opportunity Oppty3=new opportunity(...);
Let say list is prb.
prb.add(oppty1);
prb.add(oppty2);
prb.add(oppty3);
insert prb1;
Test.Stop();
This has resolved my issue and I am able to migrate now.
Thanks!
Hello,
Please help me in the similar issue.
I try to deploy a trigger and its test class in production but I have this error :
Failure Message: "System.LimitException: Too many SOQL queries: 101", Failure Stack Trace: "Trigger.CaseTeamTrigger: line 14, column 1" ...
The code coverage of this trigger is 100%.
I think this is due to another test class for another trigger but the both (the 2 triggers and the 2 test classes) are on the same object (Case).
The other trigger is already deployed.
How can I solve this problem ?
Thank you.
Here is my code:
trigger CaseTeamTrigger on Case (after insert, after update) {
Map<Id, CaseTeamMember> membersToAdd = new Map<Id, CaseTeamMember>();
List<Case> cases = [Select Id,OwnerId,RecordTypeId,RecordType.Name
from Case where id IN :Trigger.newMap.keySet()];
for (Case c : cases)
{
if(c.RecordType.Name =='Legal' || c.RecordType.Name =='Security/Quality Request'){
membersToAdd.put(c.Id, new CaseTeamMember(ParentId = c.Id, MemberId = UserInfo.getUserId()));
}
if (!membersToAdd.isEmpty()) {
try {
CaseTeamRole caseTeamRole = [SELECT Id FROM CaseTeamRole WHERE Name = 'Creator' LIMIT 100];
for (CaseTeamMember ctm : membersToAdd.values()) {
ctm.TeamRoleId = caseTeamRole.Id;
}
for (CaseTeamMember ctm : [SELECT Id, MemberId, ParentId
FROM CaseTeamMember
WHERE ParentId IN :membersToAdd.keySet()
AND MemberId = :UserInfo.getUserId()
ORDER BY ParentId])
{
if (membersToAdd.containsKey(ctm.ParentId)) {
membersToAdd.remove(ctm.ParentId);
}
}
if (!membersToAdd.isEmpty()) {
insert membersToAdd.values();
}
} catch (System.QueryException qe) {}
}
}
}