Assume we have an Apex class
myClass.cls and we use it on several different Orgs. While the source-code is the same, on some Orgs we use different ApiVersion, e.g. some Orgs use v45.0, others use v42.0 and others use v40.0.
We are using Schema methods to get existent fieldnames dynamically from a different utility-class called
xs.cls currently running on all Orgs on v42.0
Now retrieving a list of available fields with
xs.cls brings a list of fields for v42.0 - fine. Using the fields insinde query in
myClass.cls might break, if the ApiVersion is different. Not always but sometimes. E.g. I'm getting for the standard SObject
Contact a list of fields with Schema function in v42.0 which contains a standard field called
IndividualId which did not exist back in v40.0, hence this would break. Please see this only as an example. I'm not interested in any specific solution for this. The example is just to illustrate the question and make it easier to understand.
So fixing this is easy: just keep all the classes using the same ApiVersion and all will be fine. I know this and this is not the answer I am looking for.
In some cases I would prefer keeping different ApiVersions on purpose.
Therefore I would be interested, if there is a way to determine the "current" ApiVersion at runtime dynamically at any place in my code. Knowing that we have different versions in parallel use, this method should report the current effective ApiVersion even in case when
myClass.cls is using
anyClass.cls is unsing
anyOtherClass.cls with all classes having random ApiVersions?
If I would have an option to "see" on which Version im currently running, I could do whatever is necessary to do inside an
if() statement dynamically.
Currently one idea might be, to brute-force known version differences and provoking version-specific exceptions by catching and handling them, but this is very odd and would make the code hard to read and messy.