This question may asked in many posts and blogs but this became nightmare for me now. Please help me to overcome this issue Issue is
Apex Heap Size Limit Exception while Saving a Record
We have trigger in Asset Object to fire whenever it is updated. Apex class is called inside the trigger. So the logic is below
public override void beforeUpdate() {
setUpdatedAssetLastValidatedFields();
}
public static void setUpdatedAssetLastValidatedFields() {
Transient String logicHistory;
for (Asset assetProcess : (List<Asset>) Trigger.new) {
Asset oldAsset = (Asset) Trigger.oldMap.get(assetProcess.id);
logicHistory = assetProcess.assetLastValidatedLogicHistory__c +
'Timestamp = ' + System.now() + '\n' +
'User ID = ' + UserInfo.getUserId() + '\n' +
'User Name = ' + UserInfo.getUserName() + '\n' +
'INPUTS:\n' +
'old Install_Street1__c = ' + oldAsset.Install_Street1__c + '\n' +
'old Install_Street2__c = ' + oldAsset.Install_Street2__c + '\n' +
'old Install_City__c = ' + oldAsset.Install_City__c + '\n' +
'old Install_State_Province__c = ' + oldAsset.Install_State_Province__c + '\n' +
'old Install_Zip_Code__c = ' + oldAsset.Install_Zip_Code__c + '\n' +
'old Install_Country__c = ' + oldAsset.Install_Country__c + '\n' +
'new Install_Street1__c = ' + assetProcess.Install_Street1__c + '\n' +
'new Install_Street2__c = ' + assetProcess.Install_Street2__c + '\n' +
'new Install_City__c = ' + assetProcess.Install_City__c + '\n' +
'new Install_State_Province__c = ' + assetProcess.Install_State_Province__c + '\n' +
'new Install_Zip_Code__c = ' + assetProcess.Install_Zip_Code__c + '\n' +
'new Install_Country__c = ' + assetProcess.Install_Country__c + '\n' +
'LOGIC DECISIONS:\n';
assetProcess.assetLastValidatedLogicHistory__c = logicHistory.right(131072);
}
So the problem is, when I have above 50 records to update, then the string 'logicHistory' is keep building the heap size. So is there any workaround to overcome this?.
I am already using Transient keyword in the string variable but it is not reducing the size.
assetProcess.assetLastValidatedLogicHistory__c
second option is declare the string inside for loop.