Managing apex and integration errors in salesforce

Customizing Salesforce offers a significant level of flexibility for businesses. While Salesforce provides numerous out-of-the-box features, you may find it necessary to tailor it to suit your specific needs. Developers have the capability to customize the platform, but this customization can occasionally lead to system errors and unexpected behaviors.
A well-designed system should be capable of managing unexpected application flows and notifying the appropriate channels for analysis or resolution of these specific issues. It's possible that these scenarios were never previously considered, but maintaining a log of such incidents can prove highly beneficial for the ongoing operation of a business platform.
In the following sections, we will cover some common strategies you can utilize to ensure you stay ahead of these challenges.

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.

NameDescription
Class nameName of apex class
Method nameApex method where exception happened
Line numberLine where exception triggered
MessageException message

Sample exception log record

apex exception log image

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:

NameDescription
Endpoint urlAPI integration url
HTTP EventGET, POST, PUT, PATCH
HeadersHttp method headers
RequestRequest body
ResponseResponse body
StatusHttp 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.

String

Learn to use the apex string data type to validate and manipulate string variables in salesforce.

Solve
Date

Play with Apex date methods to gain hands-on experience in manipulating date data type in Salesforce.

Solve
List

Practice working with Apex lists to enhance your Salesforce development skills.

Solve
Map

Practice working with apex Maps to handle large and complex data for business requirements.

Solve
Practice apex collection control flow statements
Collections & Control flow

Discover how to utilize various conditional statements and collection types in Apex..

Solve
Practice apex SOQL and DML statements
SOQL & DML

Explore SOQL queries, database manipulation statements (DML), and how to retrieve and update records in Apex.

Solve
Practice apex triggers
Apex Triggers

Master apex triggers by leveraging trigger events and context variables to address real business scenarios.

Solve
Practice apex governor limits
Governor limits

Resolve apex code that hits the governor limits and discover techniques to prevent such occurrences in your projects.

Solve
Practice apex REST API
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
Lightning Web Components

Develop and get hands on experience in creating Lightning Web Components to create modern user interface for salesforce apps.

Solve

To know more click the Get started button below