First your core logic of checking if a record is success or not is incorrect. . As it will always create junk Order_Integration__c
records for each upsertOrderList
.
As per your code lets say if first result in lsr
is not successful, so the code will go ahead iterate on all upsertOrderList
and create records for all these in else condition.
For checking if a upsert has passed successfully or not check this
For the exception , I believe you are initializing error = ''
and errorRecCount
in wrong place. It should be inside the for loop like this:
for (order ord : upsertOrderList) {
error = '';
errorRecCount = 0;
The issue is as you have initialized String
at top and concatenating it always, so there might be scenario in your batch where that String
is growing beyond 12MB of heap limit.