We have 2 objects : Order_Integration__c, Order. We are running the batch Copying order integration table data to order table.
Requirement : If any error occurs while creating order. That respective Error Information, need to capture under respective "Order Integration Table". Order Integration and order Linked through External Id.
For Doing that : I had written below Logic :
Code Snippet:
for (Database.upsertResult res : lsr) { //Lsr is inserted orderList
for(order ord:upsertOrderList){ //Lsr is complete
orderIntegrtaion TableList
if (res.isSuccess()) {
}
else{
for(Database.Error err : res.getErrors()){
error+= err.getStatusCode() + ': ' + err.getMessage();
error+= 'Fields: '+err.getFields();
error+='\n';
Order_Integration__c ordInt = new Order_Integration__c();
ordInt.Id=ord.Order_Integration_Ids__c;
ordInt.Status_Description__c = error;
ordIntegrationList.add(ordInt);
}
}
}
}
Exception Received:
System.LimitException: Apex heap size too large: 17542735.
Seems Like Its because of for with for Loop Iteration. If I avoid ,for within for how can I achieve this requirement.
Completed Code:
for(Order_Integration__c ord:scope){
order ord = new order();
ord.Order_Integration_Ids__c = orderIntegrate.Id;
--- so on -----
upsertOrderList.add(ord);
}
//Upsert orderList -> creating OrderRecord List
if (upsertOrderList.size() > 0) {
Database.upsertResult[] lsr =
Database.upsert(upsertOrderList,order.fields.External_Id__c,false);
Integer recordid = 0;
error = '';
integer count = 0;
for (Database.upsertResult res : lsr) {
for(order ord:upsertOrderList){
if (res.isSuccess()) {
if(res.isCreated()){
successRecCount++;
}
orderIdSet.add(res.getId());
}
else{
hasErrors = true;
for(Database.Error err : res.getErrors()){
errorRecCount++;
Order_Integration__c ordInt = new Order_Integration__c();
ordInt.Id=ord.Order_Integration_Ids__c;
error+= err.getStatusCode() + ': ' + err.getMessage();
error+= 'Fields: '+err.getFields();
error+='\n';
ordInt.Status_Description__c = error;
ordIntegrationList.add(ordInt);
if(err.getStatusCode() == system.StatusCode.REQUIRED_FIELD_MISSING){
if(errorMap.containsKey(err.getMessage())){
count = errorMap.get(err.getMessage());
errorMap.put(err.getMessage(),++count);
}else{
errorMap.put(err.getMessage(),1);
}
}
}
}
}
}
}
if(!ordIntegrationList.isempty()){
map<id,Order_Integration__c> errorDescriptionMap = new map<id,Order_Integration__c>();
//put all the values from the list to map.
errorDescriptionMap.putall(ordIntegrationList);
if(errorDescriptionMap.size()>0){
update errorDescriptionMap.values();
}
}
error = ''
be insidefor(order ord:upsertOrderList){
as you are creatingOrder_Integration__c
object while iterating upsertOrderList. Basically the issue is you Stringerror
is going beyond 12MB limit.