Restrict specific characters in all input Text fields using apex trigger in Salesforce

Restrict specific characters in all input Text fields using apex trigger in Salesforce

, ,

Introduction:

Sometimes you may get a requirement to restrict specific characters in input fields. In such cases, Apex triggers come in handy.

Salesforce is a powerful customer relationship management (CRM) tool that can store a vast amount of sensitive information. As a best practice, it’s essential to ensure that data is protected from malicious attacks and data breaches.

In this blog post, we’ll be exploring a specific Apex trigger -OpportunityValidationTrigger.

The trigger validates the data in the Opportunity record before insertion or update. It checks for disallowed characters in the input text and text area fields. The trigger adds an error message to the Opportunity record if it finds any disallowed characters.

Trigger Explanation:
  • The ‘OpportunityValidationTrigger’ trigger validates the data of an Opportunity record before insertion or update. It fires on the ‘before insert’ and ‘before update’ events. This process ensures data validity.
  • The trigger uses a regular expression pattern to match disallowed characters: ‘[<>&’”]’.
  • The trigger gets the map of Opportunity fields and their descriptions using the “Opportunity.getSObjectType().getDescribe().fields.getMap()” method.
  • The trigger loops through all new Opportunity records.
  • For each Opportunity record, the trigger loops through all Opportunity fields.
  • For each field, the trigger checks if it is a string (text) or text area field.
  • If the field is a string or text area field, the trigger checks if it has a value.
  • If the field has a value, the trigger first checks the value. It uses the regular expression pattern to determine if there is a disallowed character.
  • The trigger adds an error message to the Opportunity record if it finds a disallowed character in the field value.
  • The trigger adds the label of the field where the disallowed character was found to the error message.
Sample Trigger Code :

Here is a sample trigger code, to restrict specific characters in 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;
}
}
}
}
}
}

For more helpful articles please visit – https://thesalesforcedev.in/

Leave a comment