8

I have a very slow Visualforce page. In order to find out why, I am adding a lot of System.debug() calls to the various methods on the controller to get timestamps etc to see what is so slow. So far I have code like this

public Boolean methodUsedByVisualForce() {
    System.debug('Start methodUsedByVisualForce');
    //lots of potentially slow work here
    System.debug('End methodUsedByVisualForce');
}

However, instead of findind and typing the method name, I would love to just be able to quickly add code like:

String CurrentMethodName = //code to get current method name
System.debug('Start methodName');

Php has a __METHOD__ function that will return the current method. Does apex have something similar or a way to get that info?

I have don't lots of searching and reading, finding lots of solutions that are close, but not enough. For example I can get the current class name with this code:

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

I can use this class and example to get on what edition of Saleforce, or if it is in a sandbox.

But what I want is the current method name. How would I do this?

3
  • 2
    There is no way in APEX to get the method name without an exception occurring. Commented Sep 26, 2014 at 21:39
  • That means that you could start and end your methods with something like try { throw exception; } catch { debug trace; }. Commented Sep 27, 2014 at 8:18
  • doesn't the debug log include statements such as ENTER METHOD [name] and EXIT METHOD [name] by default?
    – Dominic
    Commented Sep 29, 2014 at 8:23

1 Answer 1

3

If you take a closer look at the debug logs you will find something like the following lines:

11:39:33.337 (3337420923)|METHOD_ENTRY|[28]|01pd0000002ukZM|ClassName.methodName([Method Signature])
11:39:33.337 (3337420923)|METHOD_EXIT|[28]|01pd0000002ukZM|ClassName.methodName([Method Signature])

So the system already logs the method names and together with the timestamps also the duration of the execution.

I would suggest, that you use the Log Inspector to drill down and see, which operation does take so long.

2
  • Shesh. Crazy after looking forever I had never seen that Log Inspector article. Thanks. Commented Sep 29, 2014 at 17:01
  • 1
    Just a note that you will need to have your ApexCode Logging level set to Debug or lower for these to appear. Also, the value in the square brackets is the line number. Commented Sep 30, 2014 at 22:11

You must log in to answer this question.

Not the answer you're looking for? Browse other questions tagged .