Managing apex and integration errors in salesforce
The analysis using apex debug log
Debug logs provide an easy way to understand the sequence of execution of a transaction in Salesforce. It is often used as a way to debug the apex code in the development environment. In order to generate the debug log, you have to manually turn it on. As it is hard to predict when an exception occurs, debug logs may not always be the best choice when dealing with live users. But if you are able to replicate the errors, turning on debug logs can help you analyze the root cause. It may not be the best choice, as we have few more scalable ways to handle transaction errors in the following topics.
Custom object to log apex errors
As a best practice, developers should handle all exceptions that can arise from their code. Unhandled exceptions can result in system failures, disrupt business operations, lead to customer loss, and negatively impact your brand image. Code that effectively handles exceptions informs the user that an error occurred but reassures them that the issue will be addressed, enhancing customer satisfaction and confidence in their decision to return to your services.
One of the common ways to handle exceptions is by logging them. Salesforce offers custom objects, providing a straightforward way for logging exceptions. The exception handling mechanism in Apex can get detailed information about the events that occurred leading to an exception.
Depending on the nature of operations you are performing, you can specify the type of exceptions that may arise from the code. The Apex exception handling mechanism offers a range of predefined exception types, including DMLException, NullPointerException, ListException, and QueryException. If you need to log all exceptions, you can also use the generic 'Exception' type.
The key is to log them into our exception logs. The apex provides the following functions to find more details about exceptions.
getCause
Returns the cause of the exception as an exception object.
getLineNumber
Returns the line number from where the exception was thrown.
getMessage
Returns the error message that displays for the user.
getStackTraceString
Returns the stack trace of a thrown exception as a string.
getTypeName
Returns the type of exception, such as DmlException, ListException, MathException, and so on.
At a minimum, log the class name, message, and line number to identify what triggered the exception and the line number in your code.
Name | Description |
---|---|
Class name | Name of apex class |
Method name | Apex method where exception happened |
Line number | Line where exception triggered |
Message | Exception message |
Sample exception log record

Custom object to log Integration events
Apex provides two main APIs for connecting systems together: REST and SOAP API. Regardless of the API’s chosen, there are scenarios where system integration may not function as expected, leading to integration failures. To prevent the loss of information transmitted from one system to another, it is considered a best practice to log these messages in a centralized location. It can be within Salesforce or any database of choice. This allows for the reprocessing of payloads at any point in the future. Additionally, it serves as a valuable resource for monitoring the frequency of API invocations and analyzing typical patterns of API responses, which can be beneficial for enhancing system reliability.
The benefits of using an integration log table include the following:
Auditing Data
Integration logs provide a valuable audit trail, allowing you to track and review the data shared between systems, ensuring data accuracy and compliance.
Auto-Retry Mechanism
Integration logs can implement an auto-retry mechanism based on your business needs. Failed requests can be automatically retried at a later time, improving data reliability and reducing manual intervention.
Error Notifications
An integration log can set up a notification framework to alert you when a significant number of errors occur within a specific time frame. This proactive approach helps identify and address issues promptly.
API Service Analysis
By utilizing an integration log, you can identify the most frequently used API services within your Salesforce org. This information is valuable for regression testing and prioritizing API upgrades.
The following are some of the information to capture in the integration log object:
Name | Description |
---|---|
Endpoint url | API integration url |
HTTP Event | GET, POST, PUT, PATCH |
Headers | Http method headers |
Request | Request body |
Response | Response body |
Status | Http Status |
Scheduled report to notify errors
As a proactive business unit, IT teams need to resolve errors as quickly as possible without causing significant disruptions to the business. While a scheduled report can be an excellent choice for receiving daily notifications.If business operations demand real-time alerts, exploring alternatives such as using a platform event or creating a trigger on the exception log object to notify a channel, which can then push Slack messages or text notifications to system owners for prompt action.
What is Decodeforce?
Decodeforce is a dedicated platform aimed at helping Salesforce developers improve their Apex coding skills by solving real-world programming challenges
Solve a number of practice scenarios in DML, SOQL, trigger and many more on Decodeforce.

Collections & Control flow
Discover how to utilize various conditional statements and collection types in Apex..
Solve
SOQL & DML
Explore SOQL queries, database manipulation statements (DML), and how to retrieve and update records in Apex.
Solve
Apex Triggers
Master apex triggers by leveraging trigger events and context variables to address real business scenarios.
Solve
Governor limits
Resolve apex code that hits the governor limits and discover techniques to prevent such occurrences in your projects.
Solve
Apex REST API
Get hands-on experience with apex REST APIs to connect with external systems to send /receive data and updates records in Salesforce
Coming soon!
Lightning Web Components
Develop and get hands on experience in creating Lightning Web Components to create modern user interface for salesforce apps.
SolveTo know more click the Get started button below