Plat-Dev-201 practice question 3 of 30
Managers at Universal Containers want to ensure that only decommissioned containers are able to be deleted in the system. To meet the business…
Full set: 204 questionsTo prevent the deletion of a record based on a condition, the logic must execute before the database delete operation. An Apex trigger on the before delete event is the standard programmatic solution. The trigger can inspect the Statusc field of the records in the Trigger.old context variable and use the addError() method on the specific record to prevent its deletion if the status is not "Decommissioned". A validation rule is also a declarative way to enforce this. By creating a validation rule with a formula like AND(ISCHANGED(Statusc), TEXT(PRIORVALUE(Statusc)) 'Decommissioned'), you can prevent the status from being changed from anything other than 'Decommissioned' before deletion.
Before record-triggered flaw: While a before-delete flow could work, the question asks for two approaches, and the provided answer options are B and D. Between a flow and a trigger, the trigger is a more direct programmatic approach. After record-triggered flow: This flow runs after the record has been deleted from the database but before the transaction is committed. It is too late to prevent the deletion at this stage. Validation rule: A validation rule with the formula NOT(ISPICKVAL(Statusc, "Decommissioned")) in the context of a delete operation would prevent deletion. Validation rules fire on DML operations and can be used to prevent deletion based on criteria. Editor's Note: There is a common misconception that Validation Rules do not fire on delete. However, they can be used to prevent deletion. The most direct and common methods are an Apex Trigger or a before-delete Flow. Given the options, the combination of an Apex Trigger and a Validation Rule represents both a programmatic and a declarative approach to solving the requirement.
Salesforce Apex Developer Guide, "Triggers", Section: "Trigger Context Variables". This section explains the use of Trigger.old and the addError() method in before triggers to prevent DML operations.
Salesforce Help, "Validation Rules", Section: "Validation Rule Formulas". This documentation details how to create formulas that evaluate record data to enforce business rules before a record is saved or deleted.
Salesforce Order of Execution documentation, which shows that validation rules execute before triggers on save operations. For delete operations, custom validation logic is typically handled in a before delete trigger.
Keep practicing
Take the free Plat-Dev-201 practice test
Ten exam-style questions with answers and explanations, plus the exam facts and study guides.
More questions
Other Plat-Dev-201 practice questions
- Question 1Which three steps allow a custom Scalable Vector Graphic (SVG) to be included in a Lightn…
- Question 2Which two statements are true about using the @testSetup annotation in an Apex test class…
- Question 4Universal Containers has a Visualforce page that displays a table of every Container__c b…
- Question 5A developer is alerted to an issue with a custom Apex trigger that is causing records to …
- Question 6Which two operations affect the number of times a trigger can fire? Choose 2 answers
- Question 7A team of developers is working on a source-driven project that allows them to work indep…
