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
deepu.sandeepdeepu.sandeep 

Too Many Query Rows 50001

Hi,

This is my Trigger for Updating field on Opportunity when Account field updates


trigger OpportunityNotesUpdate on Account (after insert, after update) {

list<Account> Acc = new list<Account>();

Acc=[select id, Partnership_Notes__c, (select id, Notes__c, Accountid from Opportunities) from Account];

list<Opportunity> Opp = new list<Opportunity>();
for(Account accnt : Acc)
{
for(Opportunity Oppty : accnt.Opportunities )
{
if(accnt.Partnership_Notes__c!=Oppty.Notes__c)
{
Oppty.Notes__c=accnt.Partnership_Notes__c;
Opp.add(Oppty);
}

}
}

if(!Opp.isEmpty())
Update Opp;
}

This is my Test class for above trigger


@isTest(SeeAllData=TRUE)
private class TestOpportunityNotesUpdate {

static testMethod void OppNotesUpdate() {
Account acc = new Account(Name='Coca Cola', Type='Business',Partnership_Notes__c='Sample Notes');
insert Acc;
Opportunity Opp= new Opportunity(Name='Coca Cola',Accountid=acc.Id,Opp_Country__c='Kenya',StageName='Confirmed',CloseDate=Date.today(),Notes__c='SampleNotes2');
insert Opp;
Opp.Notes__c=Acc.Partnership_Notes__c;
update opp;
}
}

 

I'm getting the following test failure

 

system.limitexception too many query rows 50001 at line 15 i.e at query row

 

can anyone help me out in this issue

 

 

Thanks in Advance
Sandeep

 

 

testrest97testrest97

This is beacuse you are querying all accounts

 

Acc=[select id, Partnership_Notes__c, (select id, Notes__c, Accountid from Opportunities) from Account];

 

Don't you have any conditions??

 

Kamatchi Devi SargunanathanKamatchi Devi Sargunanathan

Hi,

 

I remember that, I also got the same error while writing test classes. And that is because of the following line in trigger. I highlighted that line, please check it out.

 

trigger OpportunityNotesUpdate on Account (after insert, after update) {

list<Account> Acc = new list<Account>();

Acc=[select id, Partnership_Notes__c, (select id, Notes__c, Accountid from Opportunities limit 1) from Account limit 1]; //There should be always used will limit condition when an object is directly Queried. Because direct queries should //always have only one row. Otherwise, if you want to hold multilple rows, then go for a list and loop the object to get the //values or each records.

list<Opportunity> Opp = new list<Opportunity>();
for(Account accnt : Acc)
{
for(Opportunity Oppty : accnt.Opportunities )
{
if(accnt.Partnership_Notes__c!=Oppty.Notes__c)
{
Oppty.Notes__c=accnt.Partnership_Notes__c;
Opp.add(Oppty);
}

}
}

if(!Opp.isEmpty())
Update Opp;
}

 

 

 

Hope so this helps you...!

Please mark this answer a Solution and please give kudos by clicking on the star icon, if you found this answer as helpful.

deepu.sandeepdeepu.sandeep

Hi TestRest,

 

I dont have any conditions here for query, also there are no 50000 records in my org.

 

If i kept limit 1 triggerr will not work bcoz my Account has multiple Oppportunities need to update based on Account.

Dhaval PanchalDhaval Panchal
Use batch apex to process more than 50K records.
testrest97testrest97

I don't think you need to query all the accounts each time, I am not sure if you are doing it right or I did not understand the requirement. From my understanding your trigger should look like this:

 

trigger OpportunityNotesUpdate on Account (after insert, after update) {
list<Account> Acc = new list<Account>();
Map<id,account> accMap=new Map<id,account>();
list<Opportunity> Opp = new list<Opportunity>();
for(Account accnt : trigger.new)
{
accMap.put(accnt.id,accnt);
}
if(accMap.size()>0){
for(Opportunity Oppty : [select id, Notes__c, Accountid from Opportunities where accountid in:accMap.keyset()])
{
if(accMap.get(oppty.accountid).Partnership_Notes__c!=Oppty.Notes__c)
{
Oppty.Notes__c=accMap.get(oppty.accountid).Partnership_Notes__c;
Opp.add(Oppty);
}

}
}
if(!Opp.isEmpty())
Update Opp;
}