I'm doing some polymorphic programming, and I decided that I wanted to be able to gracefully handle partial saves using SaveResult without blocking the user experience. The code looks roughly like this:
static void logDmlErrors(SObject[] src, SObject[] dest, SaveResult[] results, String message) {
// Transform results into Chatter error messages, send them to admins
}
I then went about writing the utility methods that would use this function:
public static void insertRecords(SObject[] src, SObject[] dest, String message) {
logDmlErrors(src, dest, Database.insert(dest, false), message);
}
Everything went well, until I came across the delete and undelete methods, which instead use DeleteResult and UndeleteResult. Both of those two classes contain the exact same methods as SaveResult, and yet don't appear to inherit from a common ancestor. This means that I can't just cast to a super-type to get to this information.
Thankfully, Summer 16 apparently introduced support for serializing (and deserializing) these classes as JSON, so I do have a fairly expensive (but accurate) workaround if I want proper polymorphic behavior, but I thought I'd throw it out there and see if someone has an alternative solution to this problem.
logDmlErrors
method signature, useObject[] results
instead ofSaveResult[] results
? Not offering type safety but best of the bad choices? Theninstanceof
for the 3 result types and 3 copies of the same lines of code - but I guess that is the code duplication you are trying to avoid. (No common ancestor exposed that I can see.)