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? Nov 22, 2013 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 :) Nov 22, 2013 at 15:55
  • I'm fairly sure it will work, but sorry have no idea where its documented Nov 22, 2013 at 16:01

1 Answer 1


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. Nov 23, 2013 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/… Nov 23, 2013 at 17:17

You must log in to answer this question.

Not the answer you're looking for? Browse other questions tagged .