I have three class namely A,B,C and each calls other class methods. Now my problem is getting the caller class name in called method without passing parameters to the called method. Is there any way similar to Class.Type used in apex to get the caller class name?

  • You want to get the class name of the class that called the function, or the class of the function being called?
    – sfdcfox
    Nov 25 '13 at 12:33
  • I hope I can get the class name of the current class. But how to get the class name of the caller class? Nov 25 '13 at 12:35

Scope rules prohibit any means of any piece of code seeing any variable or object declared outside its scope, so it is impossible for a callee to know whom the caller is, unless the caller identifies itself with a parameter. This is true in virtually any language, and especially true in most object-oriented languages. The purpose of the object-oriented design is that each individual class should not need to know about the details of any other class that depends on it, but should provide a known API that dependent classes can use.

In real-world terms, the transmission shouldn't need to know about what kind of car it is installed in, but the car can only use transmissions that conform to certain physical dimensions and properties. Similarly, code should generally not rely on knowing anything about the implementations that use that code. This leads to fragile code because of the two-way relationship that develops.

Of course, sometimes this design is necessary because of the intimate relationship of the two classes. In those cases, each class must still identify itself to the other, because Apex Code's dynamic language constructs do not include inspection of the stack or learning whom the caller is, as opposed to JavaScript, which did have a callee parameter for this purpose. This problem has existed basically since the dawn of modern programming, including C++.

Finally, as a side note, you should also realize that a caller might not be a class at all. Triggers are not classes, yet can also call classes. You wouldn't be able to determine the caller in that case, because they aren't normal classes.


eh, Here's a potential way to do it, but it's ugly. Like troll under a bridge ugly. Like troll under a bridge picking it's nose ugly.

Some languages provide methods on Exception to see the stack frame trace, and through it -- or by parsing it, you can see the calling class. Some (awesome) languages even provide this without executing an exception (cf: Ruby's "caller" method). Apex however, I think, is probably limited to accessing this information via an Exception. What's below is pseudo-code, and I make no guarantees as to it's viability or syntactical correctness. It is, however, of admittedly dubious usefulness and I don't know if one should do it, even if one can.

String stackTrace;
try {
  Integer i = 3/0;
} catch (Exception e) {
  stackTrace = e.getStackTraceString();
String[] trace = stackTrace.split('\n');

you can then search for your class name and act on it.

Should you really be doing this?

  • Ugly but never thought this eventhough I was using integer.valueOf('x'); to get code flow when the debug log is big Nov 26 '13 at 5:41
  • I wanted to get the name of the calling class to help in creating unique key names in a caching class. But I don't need a troll picking his nose under a bridge in my code so I'm going to find another way. Nifty trick though. :-)
    – Akrikos
    Aug 6 '15 at 20:21
  • Nice idea, but my trigger that called from page or from dml update at trace show only one line with current trigger :( Jun 7 '16 at 9:01

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.