Close Menu
SalesforceCodex
    Facebook X (Twitter) Instagram
    Trending
    • How to Build a Generic Modal Window in Lightning Web Component
    • Top 10 Salesforce Flow Features of Salesforce Summer ’25
    • Unlock the Power of Vibe Coding in Salesforce
    • How to Implement Dynamic Queueable Chaining in Salesforce Apex
    • How to Implement Basic Queueable Chaining in Salesforce Apex
    • How to Suppress PMD Warnings in Salesforce Apex
    • Top 10 PMD Issues Salesforce Developers Should Focus on in Apex
    • How to Use Graph API for Outlook-Salesforce Connection
    Facebook X (Twitter) Instagram
    SalesforceCodex
    Subscribe
    Thursday, May 29
    • Home
    • Architecture
    • Salesforce
      • News
      • Apex
      • Integration
      • Books Testimonial
    • Questions
    • Certification
      • How to Prepare for Salesforce Integration Architect Exam
      • Certification Coupons
    • Integration Posts
    • Downloads
    • About Us
      • Privacy Policy
    SalesforceCodex
    Home»Salesforce»Apex»Optimize Code by Disabling Debug Mode

    Optimize Code by Disabling Debug Mode

    Dhanik Lal SahniBy Dhanik Lal SahniJanuary 5, 20224 Comments3 Mins Read
    Facebook Twitter Pinterest LinkedIn Tumblr Email
    Optimizing code by disabling debug mode
    Share
    Facebook Twitter LinkedIn Pinterest Email

    We use debug statements in Salesforce apex to debug code in case of an error thrown in the application. In the case of production org, we normally disable debug log and when we need to analyze the issue, we turned it on. Even debug logs are disabled it will impact our application performance. This post will help in understanding how to optimize code by disabling debug mode in production.

    Let us first see what will be an issue if we have debug lines in our code and how it will impact application performance in case of debug mode is on/off.

    Let us take we have below apex code

    In the above code, we have a wrapper class with three properties. In DebugTest method we are initializing AccountWrapper class with data and each record has 5 contacts. If we check the time required to write this simple debug (although three times #34-36) also, it is taking 1 milliseconds.

    Let us see how much time the application will take when SOQL is added to the method to query with a child object and without any child object.

    Most of the time we write SOQL to query data and to check data we put complete objects into system.debug. We never think about performance impact in the system.

    In this below code, I have tried to query around 46 account records without and with child contacts. This also took around 1 and 2 milliseconds respectively. Even CPU time is also 1 and 2 milliseconds. This is only for 2 debug statements, think twice about how many debug statements we are writing in code classes unknowingly.

    Even debug is not enabled for the user it will take 1 millisecond to write debug statement.

    system.debug. Optimize Code by Disabling Debug Mode

    Now we know why we should avoid debugging statements at least for production org. Let us see how can handle this in configurable ways so that in case debugging is required we will enable it and debug will start writing. In case debugging is disabled the debug line will not be executed.

    To make configurable debug we have to create the below object and class.

    • Create Custom Metadata Type to configure system.debug
    • Create custom logger class to create wrapper over system.debug

    1. Custom Metadata Type to configure the system.debug

    Create custom metadata type Log Setting for enabling/disabling debug log. Add one column Is Log Enabled of checkbox type. Add one record for this and set the value as true for column Is Log Enabled for dev org. It will be set false in production org. In case we need debug lines in production, we have to enable this flag in custom metadata type.

    You can use custom settings as well for this flag but custom metadata type is preferable.

    custom metadata for optimize code in salesforce apex

    2. Custom logger class to create wrapper over system.debug

    Create an apex class Logger to create logger methods. These methods will first check the log is enabled or not. It will only write the log when the log is enabled. In case of the log is not enabled, it will not execute system.debug statement.

    Using this logger class is simple, we have to just replace system.debug(‘message’) to Logger.logInfo(‘message’);

    //For logging info
    Logger.logInfo('accounts:'+accounts);
    //For logging warning message
    Logger.logWarn('accounts:'+accounts.size());
    //For logging error message
    Logger.logError('Error in code');            

    Summary:

    System.Debug is for debugging purposes not for storing object data. So while writing debug statements check how much and which data we should write in debug lines. Use the above-implemented Logger class to avoid writing unwanted debug lines.

    References:

    Optimizing Salesforce Apex Code

    Set Up Debug Logging

    apex Apex logging code optimization Exception logging in apex salesforce salesforce apex system.debug
    Share. Facebook Twitter Pinterest LinkedIn Tumblr Email
    Previous ArticleSending Wrapper object to Apex from LWC
    Next Article Optimize Apex Code by Metadata Caching
    Dhanik Lal Sahni
    • Website
    • Facebook
    • X (Twitter)

    With over 18 years of experience in web-based application development, I specialize in Salesforce technology and its ecosystem. My journey has equipped me with expertise in a diverse range of technologies including .NET, .NET Core, MS Dynamics CRM, Azure, Oracle, and SQL Server. I am dedicated to staying at the forefront of technological advancements and continuously researching new developments in the Salesforce realm. My focus remains on leveraging technology to create innovative solutions that drive business success.

    Related Posts

    By Dhanik Lal Sahni4 Mins Read

    How to Build a Generic Modal Window in Lightning Web Component

    May 26, 2025
    By Dhanik Lal Sahni6 Mins Read

    Top 10 Salesforce Flow Features of Salesforce Summer ’25

    May 11, 2025
    By Dhanik Lal Sahni6 Mins Read

    Unlock the Power of Vibe Coding in Salesforce

    April 30, 2025
    View 4 Comments

    4 Comments

    1. Pingback: Optimize Apex Code by Metadata Caching | SalesforceCodex

    2. JuanGo on March 2, 2022 4:01 am

      I think could be more interesting to use Hierarchy Custom Settings to be able to enable the logs just for one user. There is no need to the penalty the execution of all the org processes when probably are interested to get the logs for one user or profile.

      Reply
      • Dhanik Lal Sahni on March 4, 2022 8:05 pm

        Yes, you can use that to enable only for required users. In this case, a log will be created for that user all-time which you have to take care.

        Thank You,
        Dhanik

        Reply
    3. Pingback: Optimizing Salesforce Apex Code | SalesforceCodex

    Leave A Reply Cancel Reply

    Ranked #1 SALESFORCE DEVELOPER BLOG BY SALESFORCEBEN.COM
    Featured on Top Salesforce Developer Blog By ApexHours
    Recent Posts
    • How to Build a Generic Modal Window in Lightning Web Component
    • Top 10 Salesforce Flow Features of Salesforce Summer ’25
    • Unlock the Power of Vibe Coding in Salesforce
    • How to Implement Dynamic Queueable Chaining in Salesforce Apex
    • How to Implement Basic Queueable Chaining in Salesforce Apex
    Ranked in Top Salesforce Blog by feedspot.com
    RSS Recent Stories
    • How to Connect Excel to Salesforce to Manage Your Data and Metadata February 9, 2025
    • Difference Between With Security and Without Security in Apex January 2, 2025
    • Top Reasons to Love Salesforce Trailhead: A Comprehensive Guide December 5, 2024
    • How to Utilize Apex Properties in Salesforce November 3, 2024
    • How to Choose Between SOQL and SOSL Queries July 31, 2024
    Archives
    Categories
    Tags
    apex (111) apex code best practice (8) apex rest (11) apex trigger best practices (4) architecture (22) Asynchronous apex (9) AWS (5) batch apex (9) batch processing (4) code optimization (8) code review tools (3) custom metadata types (5) design principle (9) file upload (3) flow (15) future method (4) google (6) google api (4) integration (19) integration architecture (6) lighting (8) lightning (64) lightning-combobox (5) lightning-datatable (10) lightning component (30) Lightning web component (62) lwc (51) named credential (8) news (4) optimize apex code (4) Permission set (4) pmd (3) Queueable (9) rest api (23) S3 Server (4) salesforce (141) salesforce apex (46) salesforce api (4) salesforce api integration (5) Salesforce Interview Question (4) salesforce news (5) salesforce question (5) solid (6) tooling api (5) Winter 20 (8)

    Get our newsletter

    Want the latest from our blog straight to your inbox? Chucks us your detail and get mail when new post is published.
    * indicates required

    Ranked #1 SALESFORCE DEVELOPER BLOG BY SALESFORCEBEN.COM
    Featured on Top Salesforce Developer Blog By ApexHours
    Recent Posts
    • How to Build a Generic Modal Window in Lightning Web Component
    • Top 10 Salesforce Flow Features of Salesforce Summer ’25
    • Unlock the Power of Vibe Coding in Salesforce
    • How to Implement Dynamic Queueable Chaining in Salesforce Apex
    • How to Implement Basic Queueable Chaining in Salesforce Apex
    Ranked in Top Salesforce Blog by feedspot.com
    RSS Recent Stories
    • How to Connect Excel to Salesforce to Manage Your Data and Metadata February 9, 2025
    • Difference Between With Security and Without Security in Apex January 2, 2025
    • Top Reasons to Love Salesforce Trailhead: A Comprehensive Guide December 5, 2024
    • How to Utilize Apex Properties in Salesforce November 3, 2024
    • How to Choose Between SOQL and SOSL Queries July 31, 2024
    Archives
    Categories
    Tags
    apex (111) apex code best practice (8) apex rest (11) apex trigger best practices (4) architecture (22) Asynchronous apex (9) AWS (5) batch apex (9) batch processing (4) code optimization (8) code review tools (3) custom metadata types (5) design principle (9) file upload (3) flow (15) future method (4) google (6) google api (4) integration (19) integration architecture (6) lighting (8) lightning (64) lightning-combobox (5) lightning-datatable (10) lightning component (30) Lightning web component (62) lwc (51) named credential (8) news (4) optimize apex code (4) Permission set (4) pmd (3) Queueable (9) rest api (23) S3 Server (4) salesforce (141) salesforce apex (46) salesforce api (4) salesforce api integration (5) Salesforce Interview Question (4) salesforce news (5) salesforce question (5) solid (6) tooling api (5) Winter 20 (8)

    Get our newsletter

    Want the latest from our blog straight to your inbox? Chucks us your detail and get mail when new post is published.
    * indicates required

    Facebook X (Twitter) Instagram Pinterest YouTube Tumblr LinkedIn Reddit Telegram
    © 2025 SalesforceCodex.com. Designed by Vagmine Cloud Solution.

    Type above and press Enter to search. Press Esc to cancel.

    Ad Blocker Enabled!
    Ad Blocker Enabled!
    Our website is made possible by displaying online advertisements to our visitors. Please support us by disabling your Ad Blocker.