0

I wanted to ask if there a problem doing upsert and delete in the same try-catch block, or maybe it better to separate them.

for example :

try {
 Schema.SObjectField oCode = Opportunity.Fields.newCode__c;
 Database.upsert(ListA, oCode, false);

 Database.delete(ListB);
} catch (Exception e) {
 System.debug(e.getMessage());
}

I have two Lists -

  • ListA - to upsert records based on an external Id.

  • ListB - To Delete records - not sure if I can delete the record base on the external Id, Or maybe I should make a soql query to get they Ids and then delete them.

  • You mentioned dml Exception. Did you try using future? – Pavan tej May 13 at 11:59
  • Try to separate out your ListB question into a separate post, or edit your post to ask the ListB question more directly – Brian Miller May 13 at 12:00
3

This pattern:

} catch (Exception e) {
 System.debug(e.getMessage());
}

is pernicious and should virtually never be used.

You gain nothing by catching and system-logging an exception. It's an exception for a reason: something bad happened, something that might impact the integrity of your data. Using this pattern will make debugging and testing your application much more difficult, and has real potential to negatively impact your database.

You should write exception handlers in a few primary cases:

  • You have written code and logic to handle - meaning to get back to a valid state and/or present necessary error messages - a specific type of error.

    You write a specific exception handler for that class of error (e.g., a DMLException) and write code to handle that exception, perhaps by presenting an error message to the user in a Visualforce page, or re-throwing an AuraHandledException, or updating a record to indicate it hasn't be processed correctly.

  • You know that a particular exception may be thrown to indicate an error condition that you cannot otherwise check for, so you use the exception as a diagnostic and change your logic path accordingly.
  • You know that an exception may be thrown for an error that's not very meaningful but leaves the database in a valid state, such as some DMLExceptions when inserting duplicate share table entries. (This is one situation when it's acceptable to just ignore a specific exception, but not the generic Exception class).

You should remove this exception handler entirely unless you are planning to write actual logic to handle the fault path.

Actual Answer

I wanted to ask if there a problem doing upsert and delete in the same try-catch block.

Not as such. In practice, it depends on the exception handling logic. Are you prepared to meaningfully handle an exception thrown by either operation using the same code? In many situations, you will be if you're presenting an error to the user:

try {
    Schema.SObjectField oCode = Opportunity.Fields.newCode__c;
    Database.upsert(ListA, oCode, false);

    Database.delete(ListB);
} catch (DMLRException e) {
    throw new AuraHandledException(e.getMessage());
}

This would be fine in a Lightning server-side controller where you want to handle the exception by showing a Toast message to the user from the JavaScript controller.

  • Thank you, Yes sure. This is just an example - I'm not going to keep it with system.debug.. What I actually do is return a set of a string with a unique text field and return it to the user as a response. I need to handle both of the delete and the upsert the same way. so you answered my question. Thank you. – Salvation May 13 at 12:45

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.