Close Menu
SalesforceCodex
    Facebook X (Twitter) Instagram
    Trending
    • The Ultimate Guide to Data Cleanup Techniques for Salesforce
    • How to Leverage Model Context Protocol (MCP) to Enhance Salesforce AI
    • Top Mistakes Developers Make in Salesforce Apex Triggers
    • Introducing Agentforce3 to Salesforce Developers
    • The Ultimate Guide to Apex Order of Execution for Developers
    • How to Handle Bulkification in Apex with Real-World Use Cases
    • How to Confidently Manage Transactions in Salesforce Apex
    • Building a Dynamic Tree Grid in Lightning Web Component
    Facebook X (Twitter) Instagram
    SalesforceCodex
    Subscribe
    Friday, August 1
    • Home
    • Salesforce Platform
      • Architecture
      • Apex
      • Lightning Web Components
      • Integration
      • Flows & Automation
      • Best Practices
      • Questions
      • News
      • Books Testimonial
    • Industries
      • Artificial Intelligence
    • Hire Me
    • Certification
      • How to Prepare for Salesforce Integration Architect Exam
      • Certification Coupons
    • Downloads
      • Salesforce Release Notes
      • Apex Coding Guidelines
    • About Us
      • Privacy Policy
    • Contact Us
    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 Sahni7 Mins Read

    The Ultimate Guide to Data Cleanup Techniques for Salesforce

    August 1, 2025
    By Dhanik Lal Sahni6 Mins Read

    How to Leverage Model Context Protocol (MCP) to Enhance Salesforce AI

    July 28, 2025
    By Dhanik Lal Sahni7 Mins Read

    Top Mistakes Developers Make in Salesforce Apex Triggers

    July 25, 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
    SFBenTopDeveloper
    Ranked #4 Salesforce Developer Blog by ApexHours.com
    ApexHoursTopDevelopers
    Categories
    Archives
    Tags
    apex (116) apex best practices (5) apex code best practice (10) apex code optimization (6) Apex logging (4) apex rest (11) apex trigger best practices (6) architecture (22) Asynchronous apex (9) AWS (5) batch apex (10) best code practice (4) code optimization (9) custom metadata types (5) design principle (9) flow (16) google (6) google api (4) integration (19) integration architecture (6) lighting (8) lightning (66) lightning-combobox (5) lightning-datatable (10) lightning component (32) Lightning web component (64) lwc (53) named credential (8) news (4) optimize apex (5) optimize apex code (6) optimize apex trigger (5) Permission set (4) Queueable (9) queueable apex (4) rest api (23) salesforce (150) salesforce apex (52) salesforce api integration (5) Salesforce Interview Question (5) 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

    MailChimp

    Expert Salesforce Developer and Architect
    Ranked #1 SALESFORCE DEVELOPER BLOG BY SALESFORCEBEN.COM
    Featured on Top Salesforce Developer Blog By ApexHours
    Recent Posts
    • The Ultimate Guide to Data Cleanup Techniques for Salesforce
    • How to Leverage Model Context Protocol (MCP) to Enhance Salesforce AI
    • Top Mistakes Developers Make in Salesforce Apex Triggers
    • Introducing Agentforce3 to Salesforce Developers
    • The Ultimate Guide to Apex Order of Execution for Developers
    Ranked in Top Salesforce Blog by feedspot.com
    RSS Recent Stories
    • Top 10 Salesforce CRM Trends to Watch in 2025 July 18, 2025
    • Discover the Top 10 Salesforce AppExchange Apps to Boost Productivity July 10, 2025
    • Top 20 Salesforce Data Cloud Interview Questions & Answers for Admins June 5, 2025
    • 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
    Archives
    Categories
    Tags
    apex (116) apex best practices (5) apex code best practice (10) apex code optimization (6) Apex logging (4) apex rest (11) apex trigger best practices (6) architecture (22) Asynchronous apex (9) AWS (5) batch apex (10) best code practice (4) code optimization (9) custom metadata types (5) design principle (9) flow (16) google (6) google api (4) integration (19) integration architecture (6) lighting (8) lightning (66) lightning-combobox (5) lightning-datatable (10) lightning component (32) Lightning web component (64) lwc (53) named credential (8) news (4) optimize apex (5) optimize apex code (6) optimize apex trigger (5) Permission set (4) Queueable (9) queueable apex (4) rest api (23) salesforce (150) salesforce apex (52) salesforce api integration (5) Salesforce Interview Question (5) 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.