html code

Enforce limitations On Specific Characters Across All Input Fields Using An Apex Trigger In Salesforce

Opening Statement:

At times, there might be a need to limit specific characters within input fields, making Apex triggers a valuable solution for such scenarios.

Salesforce stands as a robust customer relationship management (CRM) platform capable of storing extensive and sensitive information. As a best practice, safeguarding data against malicious attacks and breaches remains crucial.

This blog post delves into a specific Apex trigger – OpportunityValidationTrigger. This trigger serves to validate data within the Opportunity record before insertion or update, focusing on detecting disallowed characters within text and text area fields. If any forbidden characters are detected, the trigger adds an error message to the Opportunity record.

Trigger Overview:

  • The ‘OpportunityValidationTrigger’ trigger verifies the data within an Opportunity record before insertion or update, triggered by the ‘before insert’ and ‘before update’ events to ensure data accuracy.
  • It utilizes a regular expression pattern ‘[[<>&’”]]’ to identify disallowed characters.
  • By employing the “Opportunity.getSObjectType().getDescribe().fields.getMap()” method, the trigger retrieves a map of Opportunity fields along with their descriptions.
  • It iterates through all new Opportunity records and subsequently through each field within these records.
  • For every field, the trigger verifies if it represents a string (text) or text area field.
  • In the event of a non-empty field, the trigger examines if the value contains any disallowed character using the specified regular expression pattern.
  • If a disallowed character is identified within the field value, the trigger appends an error message to the Opportunity record.
  • Moreover, the trigger includes the label of the field where the disallowed character was detected within the error message.

Example Trigger Code:

Below is an example of trigger code designed to limit specific characters within input fields:

/**
 * OpportunityValidationTrigger
 * 
 * This trigger is responsible for validating the Opportunity record data before insert or update. 
 * The trigger checks for disallowed characters in the Opportunity's string and text area fields. 
 * If a disallowed character is found, an error message is added to the Opportunity record.
 */
trigger OpportunityValidationTrigger on Opportunity(before insert, before update) {
 
   // Regular expression to match disallowed characters
   Pattern p = Pattern.compile('[<>&\'"]');
 
   // Get the map of Opportunity fields and their descriptions
   Map < String, SObjectField > fieldMap = Opportunity.getSObjectType().getDescribe().fields.getMap();
 
   // Loop through all new Opportunity records
   for (Opportunity opp: Trigger.new) {
      // Loop through all Opportunity fields
      for (String fieldName: fieldMap.keySet()) {
         // Get the field's description
         SObjectField field = fieldMap.get(fieldName);
         // Check if the field is a string (text) or text area
         if (field.getDescribe().getType() == Schema.DisplayType.String || field.getDescribe().getType() == Schema.DisplayType.TextArea) {
            // Check if the field has a value
            if (opp.get(fieldName) != null) {
               // Check if the field value contains a disallowed character
               if (p.matcher(String.valueOf(opp.get(fieldName))).find()) {
                  // Add error message to Opportunity record if a disallowed character is found
                  opp.addError('Disallowed character found in ' + field.getDescribe().getLabel());
                  break;
               }
            }
         }
      }
   }
}