How can I use Database.DeleteResult for custom functionality.

I'd like to be able to construct it, set and get its fields.

  • 1
    Can you elaborate on the context of the question a bit? Is this for unit testing or returning Deleted Results to an external class? Might be better to create a wrapper class of DeletedResults, and perhaps a constructor on the wrapper that takes in a DeleteResult parameter to populate the wrapper? – James Loghry Apr 12 '13 at 14:15

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.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.