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
Med Usa1Med Usa1 

Deploying a trigger : System.LimitException

Hello,

 

I try to deploy a trigger and its test class in production but I have this error :

Failure Message: "System.LimitException: Too many query rows: 50001", Failure Stack Trace:...

 

The code coverage of this trigger is 100%.

 

I think the pb 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 (Opportunity).

The other trigger is already deployed.

 

How can I solve this problem ?

 

Thank you.

Nisse Knudsen.ax1026Nisse Knudsen.ax1026

Could you please post your trigger code?

 

It looks as if you are trying to query more than 50,000 records in your trigger, which exceeds the Governor Limits.

 

Med Usa1Med Usa1

I have solved this error by adding a WHERE close in my query.

But I have another error now :

System.LimitException: Too many SOQL queries: 101

 

Can you help me ?

Nisse Knudsen.ax1026Nisse Knudsen.ax1026

Yes, I think a WHERE clause is good where ever you know before your query that you really want just a reduced set of records.

 

Your next exception is caused by the sum of querys you are doing.

 

This counts as a query:

 

List<Contact> contList = [SELECT Id FROM Contact WHERE FirstName = 'Peter'];

 

 

You should try to avoid making queries inside loops (classic: for-loops!). The "Apex Code Best Practices" will help you to figure out what to do and what better not to do.

 

One hint: If you post your code, it is more helpful for the others to help you. Now we can just do a general guess about your problems.

Med Usa1Med Usa1

My query isn't inside a for loop.

I think the pb comes from another trigger.

 

My trigger code :

trigger MRT_Project_Num_Increment on Opportunity (before insert)
{
   AggregateResult[] groupResults = [SELECT MAX(MRT_Project_Num__c) NumMax FROM Opportunity WHERE Group__c = 'MRT'];

     
    for(Opportunity  opp : trigger.new)
    {
        if((opp.Group__c == 'MRT') && (opp.MRT_Project_Num__c == null))
        {
            
            for (AggregateResult ar : groupResults)
            {
                if (ar.get('NumMax')!=null)
                {
                    opp.MRT_Project_Num__c = (decimal)ar.get('NumMax') + 1;            
                }
                else
                {
                    opp.MRT_Project_Num__c = 10000;
                }
            }
        }    
    }
}

 

and in the log :

 

16:04:34.488 (4488734000)|CODE_UNIT_STARTED|[EXTERNAL]|01qD0000000HoNH|ProjectNumberBUonOpportunity on Opportunity trigger event BeforeInsert for [new]
16:04:34.490 (4490137000)|SOQL_EXECUTE_BEGIN|[357]|Aggregations:0|select Id, Business_Unit__c, BU7__c from Opportunity where Business_Unit__c = 'intra-group' order by BU7__c desc
16:04:34.490 (4490186000)|EXCEPTION_THROWN|[357]|System.LimitException: Too many SOQL queries: 101
16:04:34.490 (4490539000)|FATAL_ERROR|System.LimitException: Too many SOQL queries: 101

 

The other trigger named "ProjectNumberBUonOpportunity" is already deployed for a long time.

I don't understand the pb because the query "select id..." isn't inside a for loop.

 

Have you an idea ?

Thank you.

 

Nisse Knudsen.ax1026Nisse Knudsen.ax1026

Do you have a rough number how many records you are INSERTing at once, so this trigger gets executed?

 

 

Starz26Starz26

the error is coming from the other trigger. whatever you are inserting is causing that trigger to hit limits...

 

Does the other trigger have soql inside loops?

Med Usa1Med Usa1

The other trigger has just one for loops and there isn't SOQL inside the loops.

The query where I have the error in the log report "select id...", this query is in a clause "if".

 

Do you have another idea ?

 

Thank you for your help.

trictric

A far as I know.All the triggers running on a particular object  share governor limits.They will not have different governor limits.Can u pleaee post both the triggers and test clases as well?

 

Moreover,try to use Limits class function to know governor limits being consumed by you code.

 

In the sandbox you can use data loader to upload data activating this trigger and then se if you particular trigger gives you problem or not.?. 

 

Thanks,

Trick

craigmhcraigmh

FYI, aggregate queries count the number of records queried, not returned, for the limits.

 

So you have a COUNT() aggregate query, where 40,000 records have value X, and 30,000 records have value Y, that's not 2 rows, that's 70,000. Something to keep in mind.

 

Also, look at the error.....it's on line 357. So it's definitely occurring outside of the trigger that you posted. The query causing issues is probably somewhere else.

Med Usa1Med Usa1

I can't post all my trigger's code but here is the part with the error line 357 :

 

353    //Stand alone BU 7
354    if(Bu == 'Stand alone' || oldBu == 'Stand alone'){
355        
356        //Get previous  Opportunities ordered by BU7
357        list<Opportunity>  opportunites = [Select Id , Business_Unit__c , BU7__c from Opportunity  where Business_Unit__c ='Stand alone' order by BU7__c DESC ];
358        
359        system.debug('----------------------------opportunities ordered by BU 7 -----------------------------------------------------------'+opportunites);
360        
361        //The first Project on BU7 Stand alone
362        if(opportunites.size() == 0){
363            
364            BU_7_counter = 1;
365            updateBU_7 = '001';
366        }

 

Craigmh, an aggregate query returns all the rows or just the Max or Count ?

I have one in my trigger that makes a Max and it goes inside the for loopsjust one time.

 

Thank you for your help.

craigmhcraigmh

An aggregate query only returns the aggregate results, but ALL of the rows are counted against the limits.

 

What you could do on line 357, since you only need to know if ANY records that meet the criteria exist, is add a LIMIT 1 clause. You only need to know if the result has zero records, or more than zero, so use the LIMIT keyword to limit the results.

 

list<Opportunity>  opportunites = [Select Id , Business_Unit__c , BU7__c from Opportunity  where Business_Unit__c ='Stand alone' order by BU7__c DESC limit 1];

 

Med Usa1Med Usa1

 

Hello,

 

I have added "Limit 1" to the query but there is always the same error.

If i have a look to the log file, there is :

 

...
...
...
13:37:35.375 (5375158000)|DML_BEGIN|[212]|Op:Insert|Type:Opportunity|Rows:1
13:37:35.380 (5380375000)|CODE_UNIT_STARTED|[EXTERNAL]|01qD0000000HoNH|ProjectNumberBUonOpportunity on Opportunity trigger event BeforeInsert for [new]
13:37:35.381 (5381216000)|SOQL_EXECUTE_BEGIN|[357]|Aggregations:0|select Id, Business_Unit__c, BU7__c from Opportunity where Business_Unit__c = 'Stand alone' order by BU7__c desc limit 1
13:37:35.381 (5381454000)|EXCEPTION_THROWN|[357]|System.LimitException: Too many SOQL queries: 101
13:37:35.381 (5381730000)|FATAL_ERROR|System.LimitException: Too many SOQL queries: 101

Trigger.ProjectNumberBUonOpportunity: line 357, column 1
13:37:35.381 (5381752000)|FATAL_ERROR|System.LimitException: Too many SOQL queries: 101

Trigger.ProjectNumberBUonOpportunity: line 357, column 1
13:37:35.284 (5381871000)|CUMULATIVE_LIMIT_USAGE
13:37:35.284|LIMIT_USAGE_FOR_NS|(default)|
  Number of SOQL queries: 101 out of 100 ******* CLOSE TO LIMIT
  Number of query rows: 2146 out of 50000
...
...
...

 

And if I look the text log before, I have messages close to limits:

 

13:37:35.215|CUMULATIVE_LIMIT_USAGE_END

13:37:35.313 (5313708000)|CODE_UNIT_FINISHED|ProjectNumberBUonOpportunity on Opportunity trigger event BeforeInsert for [new]
13:37:35.315 (5315027000)|CODE_UNIT_STARTED|[EXTERNAL]|01qL0000000Ccs0|MRT_Project_Num_Increment on Opportunity trigger event BeforeInsert for [new]
13:37:35.315 (5315448000)|SOQL_EXECUTE_BEGIN|[11]|Aggregations:0|select MAX(MRT_Project_Num__c) NumMax from Opportunity where Group__c = 'MRT'
13:37:35.320 (5320092000)|SOQL_EXECUTE_END|[11]|Rows:1
13:37:35.222 (5320450000)|CUMULATIVE_LIMIT_USAGE
13:37:35.222|LIMIT_USAGE_FOR_NS|(default)|
  Number of SOQL queries: 100 out of 100 ******* CLOSE TO LIMIT
  Number of query rows: 2146 out of 50000
...
...
...

 

or

 

13:37:35.144|CUMULATIVE_LIMIT_USAGE_END

13:37:35.243 (5243068000)|CODE_UNIT_FINISHED|ProjectNumberBUonOpportunity on Opportunity trigger event BeforeInsert for [new]
13:37:35.244 (5244438000)|CODE_UNIT_STARTED|[EXTERNAL]|01qL0000000Ccs0|MRT_Project_Num_Increment on Opportunity trigger event BeforeInsert for [new]
13:37:35.244 (5244855000)|SOQL_EXECUTE_BEGIN|[11]|Aggregations:0|select MAX(MRT_Project_Num__c) NumMax from Opportunity where Group__c = MRT'
13:37:35.249 (5249324000)|SOQL_EXECUTE_END|[11]|Rows:1
13:37:35.151 (5249681000)|CUMULATIVE_LIMIT_USAGE
13:37:35.151|LIMIT_USAGE_FOR_NS|(default)|
  Number of SOQL queries: 98 out of 100 ******* CLOSE TO LIMIT

 

Do you have an idea ?

Thank you.

Med Usa1Med Usa1

In the trigger where there is the error, I have many "if" clause with a query inside and it seems that after each time it runs a query, the Number of SOQL queries increases even if it is different queries.

Is it possible to reset this number ?

 

I have also many validation rules that run. Does it count in the number of queries ?

 

Thank you.

amit_sfdcT-2amit_sfdcT-2

Hi ,

 

Let me know if you have any issues.

 

You can reach me at a.k.gupta@salesforce.com

 

Feel free to contact me any time for Dev issues with details.

 

Regards,

Amit Kumar Gupta

Salesforce.com