Below is the logic/Method i have built to eliminate error while inserting case Object. Consider a example when i try insert case object inside try block, if it is throwing an error while inserting, I will find the field name from exception message make that field as null , Continue insert with other field to avoid the data loss(has it was one of my requirement). Even i put loop counter to avoid infinite loop.
Basically my question How many DML statements will be execute? for below scenario ? Consider there are five field with error, Statement upsert caseObjToUpsert; fail to insert for 5 times & code will remove those field value by replacing NULL and 6th attempt insertion will be successful.
How many DML statements is executed? 5 error + 1 Success = 6 DML ? or 1 Success = 1 DML ?
private void errorHandling(case caseObjToUpsert){
Boolean errorInUpsert;
Integer loopCounter=0;
do{
errorInUpsert=false;
loopCounter++;
try{
upsert caseObjToUpsert;
}catch(Exception ex){
errorInUpsert=true;
String searchPattern='\\[(.*?)\\]';
Matcher MatcherValues=pattern.compile(searchPattern).matcher(ex.getMessage());
if (MatcherValues.find()){
caseObjToUpsert.put(MatcherValues.group(1),null);
}
}
}while(errorInUpsert && loopCounter <10);
if(loopCounter==10){
throw (new applicationException('applicationException'));
}
}