+ Start a Discussion
amruta_dhumalamruta_dhumal 

system exception:too many query rows 50001

Hi There,

 

While doing deployment,I got an error.

here is my trigger code:

 

 

trigger FrameworkOptyTrigger on Opportunity(after delete, after insert,after update) {
Opportunity[] cons;
if (Trigger.isDelete)
cons = Trigger.old;
else
cons = Trigger.new;

// get list of opty
Set<ID> optyIds = new Set<ID>();

for (Opportunity con : cons) {
optyIds.add(con.Framework_Agreement__c);
}
try{

List<Opportunity> childopty= new List<Opportunity>([select Id,Framework_Agreement__c from Opportunity where Framework_Agreement__c in :optyIds]);

List<Opportunity> parentToUpdate= new List<Opportunity>([select Id,Number_of_Opportunities__c,Total_Sales__c,Total_Opportunity_Sum__c from Opportunity where Id in :optyIds]);

//Use aggregate soql to get total sum of all opportunity
AggregateResult[] groupedResults=[select Framework_Agreement__c ,sum(Amount)fm from Opportunity where Framework_Agreement__c in :optyIds group by Framework_Agreement__c];

//Use aggregate soql to get total sum of all Won opportunity

AggregateResult[] groupedResults1=[select Framework_Agreement__c ,sum(Amount)ar from Opportunity where Framework_Agreement__c in :optyIds and StageName='Closed Won' group by Framework_Agreement__c];

for (Opportunity o: parentToUpdate) {
Set<ID> opId= new Set<ID>();
for (Opportunity con : childopty) {
if (con.Framework_Agreement__c== o.Id)
opId.add(con.Id);
}

if (o.Number_of_Opportunities__c != opId.size())
o.Number_of_Opportunities__c = opId.size();

for(integer i=0;i<groupedResults.size();i++){
o.Total_Opportunity_Sum__c =(Decimal)groupedResults[i].get('fm');}

for(integer j=0;j<groupedResults1.size();j++){
o.Total_Sales__c = (Decimal)groupedResults1[j].get('ar');}
}

//update parent opportunity
update parentToUpdate;
}catch(Exception e){}

}

 

getting error on line :- List<Opportunity> childopty= new List<Opportunity>([select Id,Framework_Agreement__c from Opportunity where Framework_Agreement__c in :optyIds]);

 

any help would be appreciated,I've to deploy it on prod in urgent basis.

 

Amruta

 

 

Best Answer chosen by Admin (Salesforce Developers) 
amruta_dhumalamruta_dhumal

Issue is resolved now, due to other team's test classes problem,I was getting error.

Now they have fixed their issues.....and I deployed it successfully.

 

Thanks all for response.

 

Amruta

All Answers

Praful GadgePraful Gadge

Hi Amruta,

Plese try with giving Limit 5000 to the query where you are getting this exception as there is limit of 5000 for size of list.

Regards,

amruta_dhumalamruta_dhumal

Unfortunately,noe getting an error on below line:

 

 

trigger FrameworkOptyTrigger on Opportunity(after delete, after insert,after update) {
Opportunity[] cons;
if (Trigger.isDelete)
cons = Trigger.old;
else
cons = Trigger.new;

// get list of opty
Set<ID> optyIds = new Set<ID>();
for (Opportunity con : cons) {
optyIds.add(con.Framework_Agreement__c);
}
try{
List<Opportunity> childopty= new List<Opportunity>([select Id,Framework_Agreement__c from Opportunity where Framework_Agreement__c in :optyIds Limit 5000]);

List<Opportunity> parentToUpdate= new List<Opportunity>([select Id,Number_of_Opportunities__c,Total_Sales__c,Total_Opportunity_Sum__c from Opportunity where Id in :optyIds Limit 5000]);

//Use aggregate soql to get total sum of all opportunity
AggregateResult[] groupedResults=[select Framework_Agreement__c ,sum(Amount)fm from Opportunity where Framework_Agreement__c in :optyIds group by Framework_Agreement__c ];

//Use aggregate soql to get total sum of all Won opportunity

AggregateResult[] groupedResults1=[select Framework_Agreement__c ,sum(Amount)ar from Opportunity where Framework_Agreement__c in :optyIds and StageName='Closed Won' group by Framework_Agreement__c ];

for (Opportunity o: parentToUpdate) {
Set<ID> opId= new Set<ID>();
for (Opportunity con : childopty) {
if (con.Framework_Agreement__c== o.Id)
opId.add(con.Id);
}

if (o.Number_of_Opportunities__c != opId.size())
o.Number_of_Opportunities__c = opId.size();

for(integer i=0;i<groupedResults.size();i++){
o.Total_Opportunity_Sum__c =(Decimal)groupedResults[i].get('fm');}

for(integer j=0;j<groupedResults1.size();j++){
o.Total_Sales__c = (Decimal)groupedResults1[j].get('ar');}
}

//update parent opportunity
update parentToUpdate;
}catch(Exception e){}

}

 

What I've to do here?

 

Amruta

Shiv ShankarShiv Shankar
I would like to suggest you to write batch apex class.
amruta_dhumalamruta_dhumal

can you pls help me in the code,not aware of batch apex class.

 

Amruta

amruta_dhumalamruta_dhumal

Issue is resolved now, due to other team's test classes problem,I was getting error.

Now they have fixed their issues.....and I deployed it successfully.

 

Thanks all for response.

 

Amruta

This was selected as the best answer