apex.lang.performance.ncino.operationsinloops.avoidsoslinloops.avoid-sosl-in-loops

profile photo of semgrepsemgrep
Author
unknown
Download Count*

Database class methods, DML operations, SOQL queries, SOSL queries, Approval class methods, Email sending, async scheduling or queueing within loops can cause governor limit exceptions. Instead, try to batch up the data into a list and invoke the operation once on that list of data outside the loop.

Run Locally

Run in CI

Defintion

rules:
  - id: avoid-sosl-in-loops
    min-version: 1.44.0
    severity: ERROR
    languages:
      - generic
    metadata:
      category: performance
      references:
        - https://developer.salesforce.com/docs/atlas.en-us.apexcode.meta/apexcode/apex_gov_limits.htm
      technology:
        - salesforce
      license: Commons Clause License Condition v1.0[LGPL-2.1-only]
    message: Database class methods, DML operations, SOQL queries, SOSL queries,
      Approval class methods, Email sending, async scheduling or queueing within
      loops can cause governor limit exceptions. Instead, try to batch up the
      data into a list and invoke the operation once on that list of data
      outside the loop.
    patterns:
      - pattern-either:
          - pattern-inside: |
              for (...) {
                ...
              }
          - pattern-inside: |
              while (...) {
                ...
              }
          - pattern-inside: |
              do {
                ...
              } while (...);
      - pattern-either:
          - pattern: |
              $OBJECTS = ... Search.query(...)
          - pattern: |
              $OBJECTS = ... [FIND...IN ALL FIELDS RETURNING...]

Examples

AvoidSoslInLoops.cls

public class AvoidSoslLoops {
	public void SoslInsideLoop() {
		for (Integer i = 0; i < 151; i++) {
			// ruleid: avoid-sosl-in-loops
			List<Object> objects = (List<Object>) Search.query(
				'soslString'
			)[0];
			// ruleid: avoid-sosl-in-loops
			List<List<SObject>> searchList = [FIND 'map*' IN ALL FIELDS RETURNING Account (Id, Name), Contact, Opportunity, Lead];
		}
	}

	public void SoslInsideLoop2() {
		for (Account a : accounts) {
			// ruleid: avoid-sosl-in-loops
			Object[] objects = (Object[]) Search.query(
				'soslString'
			)[0];
			// ruleid: avoid-sosl-in-loops
			List<List<SObject>> searchList = [FIND 'map*' IN ALL FIELDS RETURNING Account (Id, Name), Contact, Opportunity, Lead];
		}
	}

	public void SoslInsideLoop3() {
		while (someCondition) {
			// ruleid: avoid-sosl-in-loops
			List<Object> objects = (List<Object>) Search.query(
				'soslString'
			)[0];
			// ruleid: avoid-sosl-in-loops
			List<List<SObject>> searchList = [FIND 'map*' IN ALL FIELDS RETURNING Account (Id, Name), Contact, Opportunity, Lead];
		}
	}

	public void SoslInsideLoop4() {
		do {
			// ruleid: avoid-sosl-in-loops
			Object[] objects = (Object[]) Search.query(
				'soslString'
			)[0];
			// ruleid: avoid-sosl-in-loops
			List<List<SObject>> searchList = [FIND 'map*' IN ALL FIELDS RETURNING Account (Id, Name), Contact, Opportunity, Lead];
		} while (someCondition);
	}
}