3

I have an Apex method that takes a list of SObjects, a source field name, and a destination fieldname. For each SObject in the list, it looks at the source field, does some work, and populates the destination field using the calculated value. The method signature looks like this:

public static void SyncCodes(list<SObject> objs, String SourceField, String DestField)

I can use this method with different SObjects that have slightly different field names for the same types of fields. Works well. Note that the method does no DML - it just reads a field (using .get()) and writes a field (using .put()).

But now I need to use this method to calculate values for an SObject that has no destination field. I just want to get the answers. I could refactor the code, but I'd like to find another solution.

What feels like the right answer is to create a wrapper object and pass that to the method. However, wrapper object usually means an (inner) Apex class, which is not an SObject. So the primary question: is it possible to write a method that accepts either an SObject or an instance of an Apex Class? What is the data type of the param? Is there an interface that SObject implements, which I can also implement in my class?

I should mention that I have a workaround: since the method does no DML, I can just use an existing SObject type that has source and destination fields as my wrapper class. Just create them in memory and discard when finished. But this got me wondering about passing generic instances of apex classes around, and interchanging them with SObjects.

  • 1
    Use List<Object> instead of SObject? – Phil Hawthorn Nov 22 '13 at 15:51
  • @PhilHawthorn I thought there was something like Object, but I sure can't find the documentation. I'll play with that and see what works, but if you know of a doc link for Object, please post it as an answer I can upvote :) – Jason Clark Nov 22 '13 at 15:55
  • I'm fairly sure it will work, but sorry have no idea where its documented – Phil Hawthorn Nov 22 '13 at 16:01
5

Instead of using List<SObject> you could use List<Object>.

Then, in your SyncCodes method, you can use the instanceof operator to act accordingly, for example:

for( Object obj : objs ) {
    if( obj instanceof SObject ) {
        // do one thing
    } else {
        // do another
    }
}
  • Note that for apex types there's no generic .get and .put methods, so you'd need to cast to a concrete type (or implement a common interface) to be able to change properties. – ca_peterson Nov 23 '13 at 0:56
  • 1
    Was hoping to avoid instanceof, but as @ca_peterson points out, you can't .get or .put to an Object; as far as I can tell the only thing you can do with an Object is to cast to another type. Also found this question, which arrives at the same conclusion: salesforce.stackexchange.com/questions/11835/… – Jason Clark Nov 23 '13 at 17:17

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.