SonarSource Rules
  • Products

    In-IDE

    Code Quality and Security in your IDE with SonarQube Ide

    IDE extension that lets you fix coding issues before they exist!

    Discover SonarQube for IDE

    SaaS

    Code Quality and Security in the cloud with SonarQube Cloud

    Setup is effortless and analysis is automatic for most languages

    Discover SonarQube Cloud

    Self-Hosted

    Code Quality and Security Self-Hosted with SonarQube Server

    Fast, accurate analysis; enterprise scalability

    Discover SonarQube Server
  • SecretsSecrets
  • ABAPABAP
  • AnsibleAnsible
  • ApexApex
  • AzureResourceManagerAzureResourceManager
  • CC
  • C#C#
  • C++C++
  • CloudFormationCloudFormation
  • COBOLCOBOL
  • CSSCSS
  • DartDart
  • DockerDocker
  • FlexFlex
  • GitHub ActionsGitHub Actions
  • GoGo
  • HTMLHTML
  • JavaJava
  • JavaScriptJavaScript
  • JSONJSON
  • JCLJCL
  • KotlinKotlin
  • KubernetesKubernetes
  • Objective CObjective C
  • PHPPHP
  • PL/IPL/I
  • PL/SQLPL/SQL
  • PythonPython
  • RPGRPG
  • RubyRuby
  • RustRust
  • ScalaScala
  • ShellShell
  • SwiftSwift
  • TerraformTerraform
  • TextText
  • TypeScriptTypeScript
  • T-SQLT-SQL
  • VB.NETVB.NET
  • VB6VB6
  • XMLXML
  • YAMLYAML
Apex

Apex static code analysis

Unique rules to find Bugs, Vulnerabilities, Security Hotspots, and Code Smells in your APEX code

  • All rules 97
  • Vulnerability2
  • Bug26
  • Security Hotspot3
  • Code Smell66
Filtered: 1 rule found
unpredictable
    Impact
      Clean code attribute
        1. Email messages should be sent in batches

           Bug

        Email messages should be sent in batches

        intentionality - complete
        reliability
        Bug
        • unpredictable
        • governor-limits

        Why is this an issue?

        More Info

        Salesforce Governor Limits do not allow more than 10 calls to Messaging.sendEmail in a single transaction. There is a good chance that calling this method in a loop will reach that limit and fail. You can instead send a batch of emails with a single call to Messaging.sendEmail.

        This rule raises an issue when a call to Messaging.sendEmail is found in a loop.

         

        Noncompliant code example

        trigger MyWelcomeTrigger on Contact (after insert) {
            List<Id> toIds = new List<Id>();
            for (Contact contact : trigger.new)
            {
                if(contact.Email != null)
                {
                    Messaging.SingleEmailMessage mail = new Messaging.SingleEmailMessage();
                	String[] toAddresses = new String[] { contact.email };
                	mail.setToAddresses(toAddresses);
                	mail.setSubject('Welcome');
                	mail.setPlainTextBody('Welcome');
                	Messaging.sendEmail(new Messaging.SingleEmailMessage[] { mail });  // Noncompliant
                }
        }
        

        Compliant solution

        trigger MyWelcomeTrigger on Contact (after insert) {
            List<Id> toIds = new List<Id>();
            for (Contact contact : trigger.new)
            {
                if(contact.Email != null)
                {
                    toIds.add(contact.Id);
                }
            }
            string templateName = 'Welcome Email Template';
            EmailTemplate template = [select Id, Name from EmailTemplate where name = :templateName];
            Messaging.MassEmailMessage mail = new Messaging.MassEmailMessage();
            mail.setTargetObjectIds(toIds);
            mail.setTemplateId(template.Id);
            Messaging.sendEmail(new Messaging.MassEmailMessage[] { mail });
        }
        
          Available In:
        • SonarQube CloudDetect issues in your GitHub, Azure DevOps Services, Bitbucket Cloud, GitLab repositories

        © 2008-2025 SonarSource SA. All rights reserved.

        Privacy Policy | Cookie Policy | Terms of Use