ForAs explained here order of the exception , I believe you are initializing error = ''
result and errorRecCount
in wrong place. It should be inside the for loop like this:upsert records will be always same.
for (orderInteger ordindex := upsertOrderList0; index < lsr.size(); index++) {
error = '';
errorRecCount = 0;
if(lsr[index].isSuccess() && lsr[index].isCreated()){
if (lsr[index].isCreated()) {
successRecCount++;
}
orderIdSet.add(lsr[index].getId());
} else {
hasErrors = true;
for (Database.Error err : lsr[index].getErrors()) {
errorRecCount++;
Order_Integration__c ordInt = new Order_Integration__c();
ordInt.Id = upsertOrderList[index].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);
}
}
}
}
}
For the exception , I believe you are initializing error = ''
and errorRecCount
in wrong place. It should be inside the for loop like shown above.