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 –
For more helpful articles please visit – https://thesalesforcedev.in/


Leave a comment