apex.lang.performance.ncino.operationsinloops.avoidnativedmlinloops.avoid-native-dml-in-loops

profile photo of semgrepsemgrep
Author
unknown
Download Count*

Avoid DML statements inside loops to avoid hitting the DML governor limit. Instead, try to batch up the data into a list and invoke your DML once on that list of data outside the loop.

Run Locally

Run in CI

Defintion

rules:
  - id: avoid-native-dml-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: Avoid DML statements inside loops to avoid hitting the DML governor
      limit. Instead, try to batch up the data into a list and invoke your DML
      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: |
              insert $DATA;
          - pattern: |
              update $DATA;
          - pattern: |
              upsert $DATA;
          - pattern: |
              delete $DATA;
          - pattern: |
              Database.insert($DATA);
          - pattern: |
              Database.update($DATA);
          - pattern: |
              Database.upsert($DATA);
          - pattern: |
              Database.delete($DATA);

Examples

AvoidNativeDmlInLoops.cls

public class AvoidNativeDmlInLoops {
	public void insertInsideLoop() {
		for (Integer i = 0; i < 151; i++) {
			// ruleid: avoid-native-dml-in-loops
			insert account;
			// ruleid: avoid-native-dml-in-loops
			Database.insert(a);
		}
	}

	public void insertInsideLoop2() {
		for (Account a : accounts) {
			// ruleid: avoid-native-dml-in-loops
			insert account;
			// ruleid: avoid-native-dml-in-loops
			Database.insert(a);
		}
	}

	public void insertInsideLoop3() {
		while (someCondition) {
			// ruleid: avoid-native-dml-in-loops
			insert account;
			// ruleid: avoid-native-dml-in-loops
			Database.insert(a);
		}
	}

	public void insertInsideLoop4() {
		do {
			// ruleid: avoid-native-dml-in-loops
			insert account;
			// ruleid: avoid-native-dml-in-loops
			Database.insert(a);
		} while (someCondition);
	}

	public void updateInsideLoop() {
		for (Integer i = 0; i < 151; i++) {
			// ruleid: avoid-native-dml-in-loops
			update account;
			// ruleid: avoid-native-dml-in-loops
			Database.update(a);
		}
	}

	public void updateInsideLoop2() {
		for (Account a : accounts) {
			// ruleid: avoid-native-dml-in-loops
			update account;
			// ruleid: avoid-native-dml-in-loops
			Database.update(a);
		}
	}

	public void updateInsideLoop3() {
		while (someCondition) {
			// ruleid: avoid-native-dml-in-loops
			update account;
			// ruleid: avoid-native-dml-in-loops
			Database.update(a);
		}
	}

	public void updateInsideLoop4() {
		do {
			// ruleid: avoid-native-dml-in-loops
			update account;
			// ruleid: avoid-native-dml-in-loops
			Database.update(a);
		} while (someCondition);
	}

	public void upsertInsideLoop() {
		for (Integer i = 0; i < 151; i++) {
			// ruleid: avoid-native-dml-in-loops
			upsert account;
			// ruleid: avoid-native-dml-in-loops
			Database.upsert(a);
		}
	}

	public void upsertInsideLoop2() {
		for (Account a : accounts) {
			// ruleid: avoid-native-dml-in-loops
			upsert account;
			// ruleid: avoid-native-dml-in-loops
			Database.upsert(a);
		}
	}

	public void upsertInsideLoop3() {
		while (someCondition) {
			// ruleid: avoid-native-dml-in-loops
			upsert account;
			// ruleid: avoid-native-dml-in-loops
			Database.upsert(a);
		}
	}

	public void upsertInsideLoop4() {
		do {
			// ruleid: avoid-native-dml-in-loops
			upsert account;
			// ruleid: avoid-native-dml-in-loops
			Database.upsert(a);
		} while (someCondition);
	}

	public void deleteInsideLoop() {
		for (Integer i = 0; i < 151; i++) {
			// ruleid: avoid-native-dml-in-loops
			delete account;
			// ruleid: avoid-native-dml-in-loops
			Database.delete(a);
		}
	}

	public void deleteInsideLoop2() {
		for (Account a : accounts) {
			// ruleid: avoid-native-dml-in-loops
			delete account;
			// ruleid: avoid-native-dml-in-loops
			Database.delete(a);
		}
	}

	public void deleteInsideLoop3() {
		while (someCondition) {
			// ruleid: avoid-native-dml-in-loops
			delete account;
			// ruleid: avoid-native-dml-in-loops
			Database.delete(a);
		}
	}

	public void deleteInsideLoop4() {
		do {
			// ruleid: avoid-native-dml-in-loops
			delete account;
			// ruleid: avoid-native-dml-in-loops
			Database.delete(a);
		} while (someCondition);
	}

}