Responsive Advertisement

Switch for your debugs using custom settings in Salesforce

Adventures in Salesforce

👉 Custom Settings, the config file of Salesforce, can make your code look cool 😎.
We always target for configurable things. If I code for something, I don't want to change it frequently, but maintain a config file or some setting from where I can tweak the behavior. That is true for all platforms. So today, I will share, how I keep my debug statements in apex and turn it on and off.

Custom Settings

Custom Settings, as the name suggests, are settings you want to expose to the user. Salesforce custom settings are similar to custom objects. You can custom data sets for a user, or a profile or for the whole organization. As per Salesforce documentation

Custom settings are similar to custom objects. Application developers can create custom sets of data and associate custom data for an organization, profile, or specific user. All custom settings data is exposed in the application cache, which enables efficient access without the cost of repeated queries to the database. Formula fields, validation rules, flows, Apex, and SOAP API can then use this data.

As this data is stored in the org cache, it is easy to access. There are two types of custom settings in Salesforce.

1. List Custom Settings

The list type custom settings are static data sets available for the organization. For example, you want to use a custom numbering scheme for account names based on their country. Store all the number formats in a list type custom setting for each country and use them when an account is inserted. As per Salesforce documentation

A type of custom setting that provides a reusable set of static data that can be accessed across your organization. If you use a particular set of data frequently within your application, putting that data in a list custom setting streamlines access to it. Data in list settings does not vary with profile or user but is available organization-wide. Examples of list data include two-letter state abbreviations, international dialing prefixes, and catalog numbers for products. Because the data is cached, access is low-cost and efficient: you don't have to use SOQL queries that count against your governor limits.

2. Hierarchy Custom Settings

Unlike list type custom settings, hierarchy custom settings can have different values for different users, profiles. Consider adding a custom number to the opportunities to identify who created it. That custom number is an employe code that your organization users. Store them in a hierarchy custom setting. Create one setting for each employee who creates an opportunity. As per Salesforce documentation

A type of custom setting that uses a built-in hierarchical logic that lets you “personalize” settings for specific profiles or users. The hierarchy logic checks the organization, profile, and user settings for the current user and returns the most specific, or “lowest,” value. In the hierarchy, settings for an organization are overridden by profile settings, which, in turn, are overridden by user settings.

From the types, you might have got an idea which type I am going to use for my switch logic.

Custom Setting

Logger Class

Instead of using the normal System.debug, I wrapped it inside a Logger class. The logger class will be instantiated for the current class.

Logger Constructor

Whenever the logger is instantiated, all logger settings are fetched and stored in the logger config map.

Log Method

The log method checks if the level in the logger config is enabled or not. If enabled, it will print to console with the specified level. If not, it will ignore the debug statement.
But because the whole logic is based on the logging level, I did not expose the generic log method. Instead, I exposed a separate method for each logging level. That way developer is not required to pass the logging level, but the method will take care of that. And the method name describes the logging purpose.

Logging Methods

I then ran a small anonymous script from VS Code, and below is the result.

Anonymous Apex Result

As the output, you will not see the text "This will not be printed", because I have a custom setting for the debug level not enabled.

Custom Setting for Debug level

Cool right! Share your way of logging 😊. Let's share our adventures with the world.

Post a Comment

0 Comments