Creating APEX Class in Salesforce

Syntax of class:

Private | public | global

“Virtual | abstract | with sharing | without sharing | (none)”

Class implements     | (none)” “extends   | (none)”


// the body of the class


To create an Apex class, go to the following path

Your Name  ---> SetupàApp setup--->Develop  ---> click on Apex classes  ---> click on "New" button.

*In the editor pane, enter the following code Public class Helloworld



Click on Quick save  ---> Saves the Apex Code, making it available to be executed  ---> it makes us to add and modify code.

Save  --> savers the Apex Code, but that classes the class editor and returns to the Apex classes list.

*Add the static method to the above class Now Apex class become. Publish class Helloworld {

public static void sayyou( ) { System.debug ( 'you' );


*Now add a instance method. Now the Apex class became public class Helloworld

{// class creation

Public static void sayyou () { //adding static method to the class


Public void sayMe ( ) { //adding instance method to system debug (‘Me '); the class

Calling a class Method:

We have created the "Helloworld" class follow these steps to call its methods .

  1. Execute the following code in the Developer console to call the Helloworld class's static method.

*** To call a static method, we don't have to create an instance of the class. Helloworld say you ();

Open Raw log, then "You" appears in the USER-DEBUG log.

  1. Now execute the following code in Developer's Console to call the Helloworld classes instance method.

Note:- To call an instance method, we first have to create an instance of the Helloworld class.

Helloworld h= new Helloworld ( );

h.say Me ( );

Open Raw log , then "Me" appears in the USER-DEBUG log.

Inclined towards the profession of the Saleforce? then what is the waiting for.. study

 Salesforce Training and Certification.

Alternation APEX Class Creation:-

We can also create new Apex classes directly in the Developer Console.

  1. Open the Developer Console.

  1. Click the repository tab.

  1. The setup Entity type panel lists the different items. We can view and edit in the Developer Console.

  1. Click on classes, and then click "New" button .

  1. Enter "Message" for the name of the
  2. new class and click "ok" bottom.
  3. Add the following static method to the new class

public static string hellowMessage () {

return ( 'welcome to capital info solutions '):


  1. Click on "Save" button.

Examples 1:-

Public class AccountCreation {

Public Account createAccount(String name){

Account a= new Account();

a.Name = name; insert a;

return a;



Go to the Developer console, and execute the following code

Account Creation ac = new Account Creation();

creating an instance for the above class. ac.CreateAccount('Osmania University');

calling a method


check the account is created or not in the USER-DEBUG Log.

2.The following program is used fetch the Account Name from the account records by passing phone number.

Public class Fetch_Account_ Name_ From_Phone{ Public set<string> Fetch_ ACC_ Name(String P) { set <string> s1= new Set<string>();

List<Account> acc= “select id, Name from Account where phone”; for (Account a:acc)


String; s1.add(s); }

System.debug('xxxxxxxxx' +s1); return s1;



Go to the Developer Console, and execute the following code. FetchAccountName_ From_Phone ft= new FetchAccountName_from_Phone (); ft.FetchAccName ('9052');

In the User-Debug Log all the accounts will come that has the phone number is 9052.

For indepth understanding on Salesforce click on