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

Aggregrate query has too many rows for assignment
How can I get around the aggregrate query error? When I tried taking out the For loop- the execution then failed. Is there a better way to do it?
global class Batch_ExpDate_PricIn implements Database.Batchable<sObject>,Database.Stateful
{
global Database.QueryLocator start(Database.BatchableContext BC)
{
string manualExpStr = 'Manually Expired'; //Correct Status -11/2/16 MT
string expiredStr = 'Expired';
string query= 'select Id,RecordTypeId,RecordType.Name,Par_Status__c,Effective_date__c,Expiration_Date__c,(select Id,Expiration_Date_Change_To__c,Effective_date__c from Pricing_Inputs__r) from Price_Authorization_Request__c where Par_Status__c !=:manualExpStr and Par_Status__c !=:expiredStr';
return Database.getQueryLocator(query);
}
global void execute(Database.BatchableContext BC, List<Price_Authorization_Request__c> Parlist) {
if(Parlist != null && !Parlist.isEmpty())
{
Map<String,string> maprecTypewithId = new Map<String,String>();
List<Price_Authorization_Request__c> listPARToUpdate = new List<Price_Authorization_Request__c>();
for(RecordType rec : [select id,Name from recordType where SObjectType = 'Price_Authorization_Request__c']) //-->system does not like this
{
maprecTypewithId.put(rec.Name,rec.id);
}
for(Price_Authorization_Request__c parObj : Parlist)
{
if(parObj.Pricing_Inputs__r != null && !parObj.Pricing_Inputs__r.isEmpty())
{
Integer count = 0;
for(Pricing_Input__c PrcInputObj : parObj.Pricing_Inputs__r)
{
if(PrcInputObj.Expiration_Date_Change_To__c != null && PrcInputObj.Expiration_Date_Change_To__c < system.today())
{
count = count + 1;
}
}
if(count ==(parObj.Pricing_Inputs__r).size())
{
parObj.Par_Status__c = 'Expired';
// parObj.Expiration_Date__c=Date.valueOf(System.Today());
if(parObj.RecordType.Name == 'Standard PAR' && maprecTypewithId.get('ReadOnlyStandard PAR') != null)
parObj.RecordTypeId = maprecTypewithId.get('ReadOnlyStandard PAR');
else if(parObj.RecordType.Name == 'Formula PAR' && maprecTypewithId.get('ReadOnlyFormula PAR') != null)
parObj.RecordTypeId = maprecTypewithId.get('ReadOnlyFormula PAR');
listPARToUpdate.add(parObj);
}
}
}
if(!listPARToUpdate.isEmpty())
update listPARToUpdate;
}
}
global void finish(Database.BatchableContext BC)
{}
}
global class Batch_ExpDate_PricIn implements Database.Batchable<sObject>,Database.Stateful
{
global Database.QueryLocator start(Database.BatchableContext BC)
{
string manualExpStr = 'Manually Expired'; //Correct Status -11/2/16 MT
string expiredStr = 'Expired';
string query= 'select Id,RecordTypeId,RecordType.Name,Par_Status__c,Effective_date__c,Expiration_Date__c,(select Id,Expiration_Date_Change_To__c,Effective_date__c from Pricing_Inputs__r) from Price_Authorization_Request__c where Par_Status__c !=:manualExpStr and Par_Status__c !=:expiredStr';
return Database.getQueryLocator(query);
}
global void execute(Database.BatchableContext BC, List<Price_Authorization_Request__c> Parlist) {
if(Parlist != null && !Parlist.isEmpty())
{
Map<String,string> maprecTypewithId = new Map<String,String>();
List<Price_Authorization_Request__c> listPARToUpdate = new List<Price_Authorization_Request__c>();
for(RecordType rec : [select id,Name from recordType where SObjectType = 'Price_Authorization_Request__c']) //-->system does not like this
{
maprecTypewithId.put(rec.Name,rec.id);
}
for(Price_Authorization_Request__c parObj : Parlist)
{
if(parObj.Pricing_Inputs__r != null && !parObj.Pricing_Inputs__r.isEmpty())
{
Integer count = 0;
for(Pricing_Input__c PrcInputObj : parObj.Pricing_Inputs__r)
{
if(PrcInputObj.Expiration_Date_Change_To__c != null && PrcInputObj.Expiration_Date_Change_To__c < system.today())
{
count = count + 1;
}
}
if(count ==(parObj.Pricing_Inputs__r).size())
{
parObj.Par_Status__c = 'Expired';
// parObj.Expiration_Date__c=Date.valueOf(System.Today());
if(parObj.RecordType.Name == 'Standard PAR' && maprecTypewithId.get('ReadOnlyStandard PAR') != null)
parObj.RecordTypeId = maprecTypewithId.get('ReadOnlyStandard PAR');
else if(parObj.RecordType.Name == 'Formula PAR' && maprecTypewithId.get('ReadOnlyFormula PAR') != null)
parObj.RecordTypeId = maprecTypewithId.get('ReadOnlyFormula PAR');
listPARToUpdate.add(parObj);
}
}
}
if(!listPARToUpdate.isEmpty())
update listPARToUpdate;
}
}
global void finish(Database.BatchableContext BC)
{}
}
Hi Michele,
It would help to see your test class and debug log please.
Btw, what is the usecase?
Regards
Mann
I just realised we cannot use sub queries in batch classes because we run into too many rows for assignment when there are large number of child records associated with parent.
Here is the modified code, again i have not tested this
Oh, Sorry I forgot to include your date condition (highligted in bold) so everything was getting expired. Following should fix that.
Hope this helps