[SalesForce] call public void method from Anonymous Windows in Salesforce

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

Best Answer

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).

Related Topic