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
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');