+ Start a Discussion
Suresh SatyamSuresh Satyam 

Too many query rows: 50001

Hi ,

      I wrote a trigger on my custom object ,it is the child of opportunity object.Opportunity having Account field and My custom object also having Account field.if i update my custom object thus account field is updated with Parent object opportunity accountfield.

The trigger working fine but when i edit record and click on save.Than it shows error i.e,

Apex trigger EM_Key_Player_update_Account caused an unexpected exception, contact your administrator: EM_Key_Player_update_Account: System.LimitException: Too many query rows: 50001



trigger EM_Key_Player_update_Account on PW_Key_Players__c (after insert, after update) {

list<opportunity> opp=[SELECT Id,StageName,accountid FROM Opportunity WHERE RecordTypeId in (SELECT Id FROM RecordType WHERE (Name='IS_Opportunity') OR (Name='EM_IS_Speedi'))];
list<PW_Key_Players__c> lokp=[select id,name,PW_Account__c from PW_Key_Players__c];
list<opportunity> lopp=new list<opportunity>();
for(PW_Key_Players__c okp:trigger.new){
for(opportunity o:opp){

update opp;

I thought this can be achieve by bulk trigger,but i dont no exactly.Could anyone help me out.



Your trigger does indeed need to be bulkified, but the error you're receiving is because you aren't filtering your SOQL query. You can only return a maximum of 50000 rows in a SOQL query.


You need to add a WHERE clause or LIMIT clause to the following line:

list<PW_Key_Players__c> lokp=[select id,name,PW_Account__c from PW_Key_Players__c];


It's either that line or you have more than 50000 opportunities of those 2 record types. If your custom object and opportunity object are related, then you can filter the opportunity SOQL query to just return the opportunities that are related to the records in the trigger.


At the very least, try adding LIMIT 50000 to that query and see if that helps. However, you really should be filtering the query with a WHERE clause.


Also, since your trigger is fired on inserts or updates to the custom PW_Key_Players__c object, I'm not sure why you'd need to query for all of those objects again. You really only need the records that are already provided to you by the trigger, meaning the records that caused the trigger to fire.


if your custom object is child to opportunity, you want to assign pw_account__c value to opportunity when EM_IS_WinAccCu__c field value on child  PW_Key_Players__c is true. are r sure only one child will have this field value true.?