0

How can i call public void method from Anonymous Windows in Salesforce for debugging purpose?

Like i have apex class:

public class student{
    {
        public void submit()
        {
            system.debug('This is student class');
        }
    }

and when i am executing this class using below syntax,

student s = new student();
system.debug(s.submit());

getting an error.

Method does not exist or incorrect signature: void debug(void) from the type System

2
  • 1
    Can you edit your post to put at least the declaration of the method you want to call? (and if possible also the class) – Sergio Alcocer May 7 '20 at 8:52
  • 1
    You can call it exactly like you are saying. I guess then you are asking "how to make that call". If so, check the developer console / execute anonymous. (In Lightning: Setting wheel icon->Developer Console -> Debug --> Open execute anonymous window --> put code --> execute. then you can see the log under the logs tab) – Sergio Alcocer May 7 '20 at 9:14
2

Here you can see the example of calling the apex class method from anywhere

Sample Apex class

public class student{
    public void submit(){
        system.debug('This is student class'); 
    }
    public static void staticFun(){
        System.debug('Hello');
    }
}

Non-Static method create an instance of class then call

student objStudent = new student();
objStudent.submit(); // Output: This is student class

Static Method You can call simply via class name

student.staticFun(); // Output: Hello

In Salesforce you can execute your test code or sample code in the anonymous window. To open an anonymous window follow these steps:

  • Open Developer console after clicking on you name
  • In developer console press ctrl+E to open anonymous window
  • In an anonymous window, you can execute code whatever you want. In your scenario, you can create an instance of student class and call method.
  • After writing code click execute in debug log you can see output.

In Lighting, you can open developer console via clicking on gear icon (setting icon).

1
  • Thanks Dheeraj. its resolved now. – Tarique Shamsi May 7 '20 at 10:04
1

Since your Student.submit() method already calls system.debug(), there's no need (nor possibility, since submit() returns void and system.debug() expects to receive a value to print) to call it again. Simply initiate the method that performs the debug internally, and you'll see the result in your debug log.

student s = new student();
s.submit();

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.