PDI · Question #78
A developer created a new trigger that inserts a Task when a new Lead is created. After deploying to production, an outside integration chat reads task records is periodically reporting errors…
The correct answer is D. Use the Database method with all or None set to false. When DML operations within a trigger might cause issues for downstream integrations due to partial failures, using Database.insert() with allOrNone set to false allows successful records to be inserted while ignoring failures for problematic ones.
Question
A developer created a new trigger that inserts a Task when a new Lead is created. After deploying to production, an outside integration chat reads task records is periodically reporting errors. Which change should the developer make to ensure the integration is not affected with minimal impact to business logic?
Options
- ADeactivate the trigger before the integration runs.
- BUse a try-catch block after the insert statement.
- CRemove the Apex class from the integration user's profile.
- DUse the Database method with all or None set to false
How the community answered
(26 responses)- A4% (1)
- B8% (2)
- C15% (4)
- D73% (19)
Why each option
When DML operations within a trigger might cause issues for downstream integrations due to partial failures, using `Database.insert()` with `allOrNone` set to `false` allows successful records to be inserted while ignoring failures for problematic ones.
Deactivating the trigger before the integration runs would completely stop the business logic of creating tasks for new leads, which is not a minimal impact solution.
A `try-catch` block around the insert statement would catch the exception, but without specific handling for partial success, it might still prevent any tasks from being created if an error occurs for even one record.
Removing the Apex class from the integration user's profile would prevent the trigger from executing entirely for that user, stopping the business logic of creating tasks and not offering a minimal impact solution for errors.
The `Database.insert(records, allOrNone)` method with `allOrNone` set to `false` (i.e., `Database.insert(myTasks, false)`) allows for partial success in DML operations. This means if some `Task` records fail to insert, the successful `Task` records will still be created, preventing the entire transaction from rolling back and minimizing impact on the business logic while allowing the integration to proceed with valid data.
Concept tested: Apex DML options (partial success)
Source: https://developer.salesforce.com/docs/atlas.en-us.apexcode.meta/apexcode/apex_methods_system_database.htm#apex_System_Database_insert_records_allOrNone_param
Topics
Community Discussion
No community discussion yet for this question.