Find your content:

Search form

You are here

Can I determine the class name of an object instance at run-time?

 
Share

I'm looking for a way to get the name of an object instance at run-time. Is it possible to to determine the class name (as a string) of an instance? i.e. is there an Apex equivalent of the Java myInstance.getClass().getName() or any tricks to achieve just that?


Attribution to: Phil Hawthorn

Possible Suggestion/Solution #1

Voting for the idea Method to get the Type of an Object (also Primitive Type not only SObject) would be a good long term solution.


Attribution to: Daniel Ballinger

Possible Suggestion/Solution #2

You can use something like below:

String.valueOf(this).substring(0,String.valueOf(this).indexOf(':'));

Sources
@pbattisson's Ans
@Matthew's Ans in SF blog


Attribution to: Pradeepkumar'2552866

Possible Suggestion/Solution #3

instanceOf verifies at runtime whether an object is actually an instance of a class...but you would have to write the conditions to check for them all. Also getSObjectType will describe an sObject.


Attribution to: fourq
This content is remixed from stackoverflow or stackexchange. Please visit https://salesforce.stackexchange.com/questions/3385

My Block Status

My Block Content