Enforce restrictions on record updates using validation within a Trigger.

SoomjeetSahoo
1 min readOct 5, 2023

One of the issues I personally addressed involved the implementation of a record locking mechanism using a validation rule. I will elaborate on this matter in the following section.

Use Case/ Business Scenario

Whenever an Opportunity with an Account of Type “Prospect” is being updated, a validation error must be triggered to restrict this action, taking into account the following scenarios:

  1. Updates to Opportunity via some other trigger must not be restricted.
  2. Few Fields on Opportunity should be available to update.
  3. Should check all fields dynamically.

Code References

OpportunityTrigger
OpportunityTriggerHandler

Please follow this repo for code reference.

For Scenario 1, The static variable “byPassOpportunityTrigger” serves the purpose of bypassing the trigger when an update logic is executed from another process or trigger.

For Scenario 2, To facilitate the bypassing of validation/restriction logic, we are utilizing the variable “FIELDS_TO_BE_IGNORED_FROM_VALIDATION_SET” to store the API names of specific fields. Consequently, the “Description” field on the Opportunity record is one such field that is excluded from this validation process.

For Scenario 3, This code has been designed to validate all the fields, making it capable of accommodating any new fields that may be introduced in the future without requiring any code changes.

Hope this helps you solve your development needs.

--

--

SoomjeetSahoo

Salesforce Dev @ Salesforce with lots of inquisitiveness to learn and grow in Salesforce and Node Domains. Currently 15x Salesforce Certified & 14x Super Badge.