You need to sign in to do that
Don't have an account?
Gaurgk
Hello community member, I am facing heap error in this trigger. Can someone guide me to optimise the code? What all changes is required to fix the heap large size error. Thanks in advance,
public class NSPThresholdHelper {
private static void NSPHelper(list<PriceApproval__c> plist){
system.debug('nsp trigger running');
set<id> ppaId = new set<Id>();
for(PriceApproval__c pa: plist){
if(pa.Country__c != null){
ppaId.add(pa.Id);
}
}
list<PriceApproval__c> ppalist = [select id, name , Country__c, ApprovalStatus2__c, Product__c
from PriceApproval__c where id in: ppaId limit 1];
list<PriceApprovalPrice__c> paplist = [SELECT Id,Name,Product__c,Product__r.TherapeuticArea__c,Product__r.Tier__c,CountryProductSKU__c,
CountryProductSKU__r.id,CountryProductSKU__r.name, CountryProductSKU__r.ProductSKU__r.name,
CountryPriceType__r.name, CountryPriceType__r.Channel__r.name
FROM PriceApprovalPrice__c where PriceApproval__r.id in: ppaId limit 20];
string prodconfname = paplist[0].CountryProductSKU__r.name+'_'+paplist[0].CountryPriceType__r.Channel__r.name;
system.debug('Paplist size: '+paplist.size());
list<ProductConfidential006__c> prodconf = [select id, name, MnIRP__UniqueID__c, MnIRP__FloorType__c from ProductConfidential006__c where MnIRP__FloorType__c= 'Floor'];
// string uniqueId =MnIRP__FloorType__c +'_' +Country__c +'_' +Product2__c +'_' +Channel__c +'_' +CountryProductSKU__c +'_' +CalendarMonth__c.month() +'_' +CalendarMonth__c.year();
string Uniqueprodcon = 'Floor' +'_' +ppalist[0].Country__r.name +'_' +ppalist[0].Product__r.name +'_' +paplist[0].CountryPriceType__r.Channel__r.name +'_' +paplist[0].CountryProductSKU__r.name +'_' +date.today().month() +'_' +date.today().year();
for(integer x=0; x < prodconf.size(); x++){
if(Uniqueprodcon == prodconf[x].MnIRP__UniqueID__c){
system.debug('already existing');
}
}
list<ProductConfidential006__c> prodconlist = new list<ProductConfidential006__c>();
for(PriceApproval__c ppa : plist){
for(integer i=0; i< paplist.size(); i++){
// string Uniqueprodconf = 'Floor' +'_' +ppalist[0].Country__r.name +'_' +ppalist[0].Product__r.name +'_' +paplist[i].CountryPriceType__r.Channel__r.name +'_' +paplist[i].CountryProductSKU__r.name +'_' +date.today().month() +'_' +date.today().year();
if(paplist.size() > 0 && ppalist[0].ApprovalStatus2__c == 'Finalized'){
prodconlist.add(new ProductConfidential006__c(
name = paplist[i].CountryProductSKU__r.name+'_'+paplist[i].CountryPriceType__r.Channel__r.name,
Product2__c = ppalist[0].Product__c,
Channel__c = paplist[i].CountryPriceType__r.Channel__c,
Country__c = ppalist[0].Country__c,
MnIRP__FloorType__c = 'Floor',
// Product_Group__c = pgpm.ParentProductGroup__r.name,
Tier__c = paplist[0].Product__r.Tier__c,
CountryProductSKU__c =paplist[i].CountryProductSKU__c,
ProductSKUNumber__c = '',
CalendarMonth__c = date.today()
));
}
}
}
if(prodconlist.size()>0){
system.debug('prodconfidential size:'+prodconlist.size());
// insert prodconlist;
for(ProductConfidential006__c prodc : prodconlist){
system.debug('names :'+prodc.name);
}
}
}
}
private static void NSPHelper(list<PriceApproval__c> plist){
system.debug('nsp trigger running');
set<id> ppaId = new set<Id>();
for(PriceApproval__c pa: plist){
if(pa.Country__c != null){
ppaId.add(pa.Id);
}
}
list<PriceApproval__c> ppalist = [select id, name , Country__c, ApprovalStatus2__c, Product__c
from PriceApproval__c where id in: ppaId limit 1];
list<PriceApprovalPrice__c> paplist = [SELECT Id,Name,Product__c,Product__r.TherapeuticArea__c,Product__r.Tier__c,CountryProductSKU__c,
CountryProductSKU__r.id,CountryProductSKU__r.name, CountryProductSKU__r.ProductSKU__r.name,
CountryPriceType__r.name, CountryPriceType__r.Channel__r.name
FROM PriceApprovalPrice__c where PriceApproval__r.id in: ppaId limit 20];
string prodconfname = paplist[0].CountryProductSKU__r.name+'_'+paplist[0].CountryPriceType__r.Channel__r.name;
system.debug('Paplist size: '+paplist.size());
list<ProductConfidential006__c> prodconf = [select id, name, MnIRP__UniqueID__c, MnIRP__FloorType__c from ProductConfidential006__c where MnIRP__FloorType__c= 'Floor'];
// string uniqueId =MnIRP__FloorType__c +'_' +Country__c +'_' +Product2__c +'_' +Channel__c +'_' +CountryProductSKU__c +'_' +CalendarMonth__c.month() +'_' +CalendarMonth__c.year();
string Uniqueprodcon = 'Floor' +'_' +ppalist[0].Country__r.name +'_' +ppalist[0].Product__r.name +'_' +paplist[0].CountryPriceType__r.Channel__r.name +'_' +paplist[0].CountryProductSKU__r.name +'_' +date.today().month() +'_' +date.today().year();
for(integer x=0; x < prodconf.size(); x++){
if(Uniqueprodcon == prodconf[x].MnIRP__UniqueID__c){
system.debug('already existing');
}
}
list<ProductConfidential006__c> prodconlist = new list<ProductConfidential006__c>();
for(PriceApproval__c ppa : plist){
for(integer i=0; i< paplist.size(); i++){
// string Uniqueprodconf = 'Floor' +'_' +ppalist[0].Country__r.name +'_' +ppalist[0].Product__r.name +'_' +paplist[i].CountryPriceType__r.Channel__r.name +'_' +paplist[i].CountryProductSKU__r.name +'_' +date.today().month() +'_' +date.today().year();
if(paplist.size() > 0 && ppalist[0].ApprovalStatus2__c == 'Finalized'){
prodconlist.add(new ProductConfidential006__c(
name = paplist[i].CountryProductSKU__r.name+'_'+paplist[i].CountryPriceType__r.Channel__r.name,
Product2__c = ppalist[0].Product__c,
Channel__c = paplist[i].CountryPriceType__r.Channel__c,
Country__c = ppalist[0].Country__c,
MnIRP__FloorType__c = 'Floor',
// Product_Group__c = pgpm.ParentProductGroup__r.name,
Tier__c = paplist[0].Product__r.Tier__c,
CountryProductSKU__c =paplist[i].CountryProductSKU__c,
ProductSKUNumber__c = '',
CalendarMonth__c = date.today()
));
}
}
}
if(prodconlist.size()>0){
system.debug('prodconfidential size:'+prodconlist.size());
// insert prodconlist;
for(ProductConfidential006__c prodc : prodconlist){
system.debug('names :'+prodc.name);
}
}
}
}
Heap size error generally comes in if you exceed storage limit allowed for transaction. it is 6 MB for Synchoronous and 12 MB for asycnhronous transactions. Related article. (https://help.salesforce.com/HTViewSolution?id=000004186&language=en_US#:~:text=Salesforce%20enforces%20an%20Apex%20Heap,stored%20in%20memory%20during%20processing.)
Most of time what we have been doing is :
1. Query only fields data which are needed in SOQL queries.
2. Clear unused collection data from controller if it is no longer needed.
3. Use Transient keyword to store variable for one time use, so it will not be part of view state.
4. Move processing to Asynchronous calls if nothing works, if possible so you get 2X(12 MB) limit.
Thanks