7

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?

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

  • Shesh. Crazy after looking forever I had never seen that Log Inspector article. Thanks. – Joshua Dance Sep 29 '14 at 17:01
  • 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. – Daniel Ballinger Sep 30 '14 at 22:11

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.