First Apex Program | Hello Apex Example
Here, we will learn how to write the Demo program of Apex. We can write a Demo hello Apex program easily after login into Salesforce.
How to open an Apex class in Salesforce?
Click Setup > Develop > Apex Classes
Click New
Write your required Apex code, and click Quick Save/Save button
Let’s create the hello Apex program:
Public class Demo { Public void sayHello() { System.debug('Hello Apex'); } }
How to compile:
Once you click on the Quick Save/Save button then Apex engine automatically compiles your code.
How to run:
Click on Developer Console button on Apex classes page.
This will open a new popup window, cilck Debug from the menu bar and then click “Open Execute Anonymous Window” submenu or press CTRL + E as below in the screen shot.
Now, create an object of Demo class, call the method and then click on Execute button as below:
How to create Object:
Demo d = new Demo(); d.sayHello();
Check the Debug Only checkbox to see the output as below in the screen shot.