6

Is it possible to make an apex method wherein you can return different kind of data type (e.g. Integer, String, Double, etc.)?

1 Answer 1

9

You can set return type of the Apex method to Object.

public Object returnVariousDataType(String dataType){
    //write something
    if(dataType == 'String'){
        // return String
        return 'this is a string';
    } else if(dataType == 'Integer'){
        // return Integer
        return 0;
    } else {
        // return other types
        return Date.today();
    }
}

And cast the return value to what you want when you invoke it.

Integer int = (Integer)returnVariousDataType('Integer');
0

You must log in to answer this question.

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