[SalesForce] Difference between addError(errorMsg) and addError(exceptionError)

Wondering if anyone can explain the difference between errors and exception errors, or, more specifically, when one should use addError(errorMsg) instead of addError(exceptionError)?

The documentation is a little obtuse to me. I think it also is accidentally explaining the addError(errorMsg) method twice right now, which I'm finding a little confusing.

Don't they both just show an error in the application interface? In my particular case, I don't actually need anything to pop up in the UI, I just need to surface an error so that DataLoader knows why records fail if they fail. Not sure which to use.

Best Answer

If you're using try-catch, consider using addError(Exception) if you want to write smaller code[citation needed], and if you're not, use addError(String). In my experience, properly bulkified code will typically use addError(String).

The addError(Exception) method is typically used when you're writing atomic updates, usually within a Visualforce page, and the addError(String) method is more likely to be used in a trigger so it is properly bulkified (e.g. some records can succeed while others fail).

Both will result in an error being reported to the upper-level API, which might be a user saving a record, a Visualforce page, or an API-enabled tool like the Apex Data Loader. Both have the same general effect (the Data Loader will get an error in the errors*.csv file).

Related Topic