Certified Platform Developer II Exam QuestionsBrowse all questions from this exam

Certified Platform Developer II Exam - Question 223


A business requires that every parent record must have a child record. A developer writes an Apex method with two DML statements to insert a parent record and a child record.

A validation rule blocks child records from being created. The method uses a try/catch block to handle the DML exception.

What should the developer do to ensure the parent always has a child record?

Show Answer
Correct Answer: BD

To ensure that every parent record has a child record, the developer should delete the parent record in the catch statement if an error occurs on the child record DML operation. This approach guarantees that if the insertion of the child record fails due to validation rules, the parent record will not be left orphaned. By deleting the parent record in the catch block, the integrity of the data is maintained, ensuring that no parent record exists without a corresponding child record.

Discussion

3 comments
Sign in to comment
M4JK3LSON
Dec 21, 2023

Ugly question again - please tell me how it is possible to ensure that parent record ALWAYS (so in insert scenario too) have at least one child record when inserting child record REQUIRES parent Id to mark it's record as a child of another record. Parent record Id becomes avaliable the earliest in after insert trigger context 🤣

RatnaBOption: D
Mar 2, 2024

D is the correct answer

corpexOption: B
May 8, 2024

A and D won't undo any potencial change to related records when you delete or adderror the parent. C is not useful in this scenario since you need the parent Id to be inserted first. So answer is B