How can I use Database.DeleteResult
for custom functionality.
I'd like to be able to construct it, set and get its fields.
Salesforce Stack Exchange is a question and answer site for Salesforce administrators, implementation experts, developers and anybody in-between. It only takes a minute to sign up.
Sign up to join this communityHow can I use Database.DeleteResult
for custom functionality.
I'd like to be able to construct it, set and get its fields.
You can construct almost any arbitrary type of data by using JSON.deserialize(String, Type)
. The following example constructs a successful delete operation:
Database.DeleteResult dr =
(Database.DeleteResult)JSON.deserialize('{"success": true, "id": "001000000000000"}', Database.DeleteResult.class);
You can also construct a failed operation using the same means; simply specify an array of errors to return. Note that if the errors array is passed, even if the array contains no values, success is automatically reverted to false.
You cannot modify the data inside of the Database.DeleteResult object itself, but you can choose to serialize it, modify the contents, then deserialize it. I suggest using "live" examples by actively deleting an item then debugging the results from JSON.serialize so you can get a sense of the proper JSON format.