3

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'));
        }

    }
4

As Santanu says, it would be six DML statements; a failed DML is still a DML. However, your main problem is that you're not checking for all field errors at once.

Here's a better design:

Database.upsertResult sr;
Integer retryCounter = 0;
do {
  sr = Database.upsert(caseObjToUpsert, false);
  for(Database.Error dmlError: sr.getErrors()) {
    for(String field: dmlError.getFields()) {
      caseObjToUpsert.put(field, null);
    }
  }
} while(retryCounter++ < RETRY_LIMIT && !sr.isSuccess());
if(!sr.isSuccess()) {
  throw new ApplicationException('Application Exception.');
}

Instead of stopping on the first error, we actually try to process as many errors as possible all at once. This means if five fields have errors, there should still only be two DML (1 error and 1 success). There's still different types of errors that could occur, like record-level errors, so you might want to think about handling those separately.

I feel like 10 retries is likely too many; if you get past 3 or 4, it's unlikely you're going to save. I would probably set RETRY_LIMIT to 5 as a maximum, and probably check StatusCode against a list of known possible errors that you can resolve. For example, if the error is StatusCode.REQUIRED_FIELD_MISSING, setting the value to null won't help you, so you could abort early in this case.

3

Error in DML during insert is part of your DML count.

5 error + 1 Success = 6 DML

Since you are processing each record for DML, it will be best to buliky it.

I would suggest to use Database.upsert(records, false) and then process those failed records and finally insert those corrected records in a single goal.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.