SF-PD2 Advanced Developer Fundamentals Practice Question
A developer needs to implement a custom validation rule that involves complex cross-object logic. The logic is too complex for a standard Validation Rule formula. What is the best practice for implementing this logic?
⚠ Common exam trap
Many test-takers mistakenly select declarative tools like custom validation rules or flow-based validation, overlooking that complex cross-object logic often requires programmatic approaches.
Answer choices
Why each option matters
Answer the question above first, then reveal the full breakdown to understand why each option is right or wrong.
Correct answer & explanation
✓
Implement a 'before' trigger and use 'addError()' on the sObject.
Validation rules are great for simple conditions, but when logic spans multiple objects or requires complex arithmetic, Apex triggers are the superior choice. Using an 'Apex Trigger' on the 'before insert' or 'before update' events allows for a complete, programmatic validation. If the validation fails, using the 'addError()' method on the record is the standard way to provide user feedback and prevent the DML operation.
Answer analysis
Option-by-option breakdown
For each option: why learners choose it and why it is or isn't the right answer here.
- ✗
Create a Flow with a decision element to delete the record if invalid.
Why it's wrong here
Deleting a record after it has already been inserted is an anti-pattern. This creates data inconsistency and triggers unnecessary record lifecycle events. Validation should always happen before the record is saved to the database, ensuring that bad data is never committed to the system in the first place.
- ✗
Use a Record-Triggered Flow to update the record status to 'Invalid'.
Why it's wrong here
Updating the record status to 'Invalid' does not prevent the user's intended action or the persistence of the data. Validation must physically block the save operation to ensure data integrity. Flows can be used, but they must use 'Validation' elements to block the operation, not just update status.
- ✓
Implement a 'before' trigger and use 'addError()' on the sObject.
Why this is correct
'addError()' is the designated method for preventing DML operations in Apex. It provides a clean way to block the save and display an error message directly on the UI for the user, effectively handling complex cross-object logic that standard validation formulas cannot accommodate due to their constraints.
- ✗
Use a scheduled job to find and clear invalid records nightly.
Why it's wrong here
Delayed validation is insufficient for enterprise applications. By the time a nightly job runs, the invalid data might have already been processed by other systems or integrated into reports, leading to incorrect business decisions. Validation must be synchronous and immediate to ensure only high-quality data enters the environment.
About these practice questions
One of 226 original SF-PD2 practice questions on Courseiva, each with a full explanation and wrong-answer analysis — not exam dumps or protected exam content. Learn why practice questions differ from exam dumps →
JA
Written and reviewed by Johnson Ajibi, MSc IT Security
Senior Network & Security Engineer · founder of Courseiva
Last reviewed September 2026 · checked against the official Salesforce exam blueprint
This SF-PD2 practice question is part of Courseiva's free Salesforce certification practice question bank. Courseiva provides original exam-style practice questions with explanations, topic-based practice, mock exams, readiness tracking, and study analytics to help learners prepare for the SF-PD2 exam.