Error Logging in Salesforce

I have recently started a new job where my primary development is with the Salesforce platform. Salesforce is a CRM at the core, but also provides a pretty robust ecosystem of tools that make up a customer success platform. It is great! There are plenty of things that have been done to it to make it more attractive to developers. Lightning is something that comes to mind.

Salesforce provides three different development tools for developers when working with their SAAS solution:

Visualforce — The older UI framework used by Salesforce, that is similar to HTML and allows JavaScript with some native handlers to access Salesforce data.

Lightning — The new UI framework used by Salesforce, component based framework kind of like React, it seems a little bit more decoupled from Salesforce making accessing data to take a little bit more work.

Apex — Salesforce’s custom Object Oriented Programming Language that is similar to the flavor of Java, but with fewer features.

All of these tools used by Salesforce are great, but one thing missing from each of them is a common way of handling errors.


When working with development language and tools outside of a platform, you can pretty much whip up an existing solution to handle errors in minutes. When it comes to Salesforce, there are a lot of creative ways, but it’s a little different when it comes to the tools we mentioned above.

Let’s first start with Apex. One of the first solutions you will see if you start Googling is catching and logging errors by creating a custom Error Log Object. In this way anything thrown from a try catch or inserted directly can be logged and retrieved through a SOQL query.

Custom Error Log Object

There are some cool things you can do. You can now build reports and user interfaces see these errors. Capture user information and specifics about the error, and you can really help your time when a user calls in about an issue.

Unfortunately with Lightning there is no straightforward solution when it comes to logging to a custom object. The inability to query Salesforce Data directly from the controller causes challenges, as it becomes a little bit more difficult to log errors. You can catch handled or unhandled exceptions (if Locker Service is disabled), but you are still responsible for sending that to an Apex Controller to save that to your custom object.

With Visualforce we have a similar issue, but there is both JavaScript and Apex intertwined.

Now there are plenty of error logging solutions out there, Sentry, Bugsnag, Airbrake, but none of these have direct support for Salesforce. These solutions come with some great features that can save time when rolling your own solution with a custom object. Within the next few months I would like to bring one of these solutions over to Salesforce and experiment with how I can make it easier for Salesforce developers to log to a central solution.


We’d love to hear your techniques/strategies when it comes to error handling and logging for Salesforce. Get involved in the conversation on Twitter or Facebook.

Follow our journey by signing up for our newsletter!