6

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

8

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');
| improve this answer | |

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.