Can I pass a generic object to an apex method?

I would create a generic method which can take in input a generic object (not only an SObject)…

For example would create a method like:

public static output MyMethod(Object inputmessage, String Objpamaeter){
    inputmessage.put(Objpamaeter, 'xx');
    output xx= new outputValidation();
    return xx;

where output is an object with string parameteres, while inputmessage is an custom apex object or an SObject…
If an so large generalization is not possible, what solution can be taken to prevent the fragmentation of the architecture?

thanks in advance…



I don’t think you could make such a method with the current reflection support in Apex.

I’d suggest creating an interface that all input classes implement. Then you could just deal with them via that common interface. E.g.

public interface Validatable {
    bool isValid();

The standard sObjects won’t implement this interface, so you might need to wrap them in a wrapper class.

Source : Link , Question Author : Klodj_Meta , Answer Author : Daniel Ballinger

Leave a Comment