This course is currently a work in progress. Please join the waitlist to receive regular updates.
Data validation in apex triggers
Accurate and up-to-date information is crucial for businesses to maintain a smooth sales process. For example, keeping phone numbers current ensures that the business can easily reach out to customers if any support issues need to be addressed over the phone.
Before Apex triggers are used to automate data validations, it is important to ensure that the data in the system is as accurate as possible. The Apex trigger framework supports an addError()
method, which allows you to pass an error message to display to the user if certain conditions are not met on the records.
Following is an example of preventing duplicate lead creation in apex trigger.
Loading...
Trigger
- The trigger
LeadTrigger
is written onLead
object for thebefore insert
event. - The trigger context variables
Trigger.isBefore
,Trigger.isInsert
are conditionally used to check the execution context before invoking the trigger handlerLeadTriggerHandler
.
Trigger Handler
- The trigger handler
LeadTriggerHandler
implements the business rules for the trigger. - The LeadTriggerHandler gets the records from the trigger context using the
Trigger.new
context variable. - The LeadTriggerHandler use a
for loop collection
to iterate through all lead records to validate if the email is present using an if condition. - The
addError()
method on line10
displays an error on the screen if the record is missing the email address.
Loading...
Loading...