An Apex Class is a blueprint for creating objects that encapsulate data and behaviors of your business requirements. Built using Salesforce-specific programming language Apex, these classes enable developers to write custom logic and perform automation/extension of the capabilities within your existing Salesforce applications.

Apex Classes can have methods, variables and constructors for creating reusable code in Apex. They are responsible for the robust development of complex applications, including integration with enterprise system and implementing business rules to make sure Salesforce works efficiently and meets unique enterprises need.

Learn Apex Class Basics?

Apex is a strongly typed Object-oriented programming language and it will run on Force.com platform. Here we give you the info about how to create a class in Salesforce.

Below is the example to create a simple class

public class MyFirstApexClass {
    // body of the Apex class. Here we can define variables and methods
}

This is the simple class definition. Generally in Salesforce to define a class you must use “class” keyword followed by access specifier. here access specifier and ‘class’ and class names are mandatory for every class in Salesforce.

Creating Apex class in Salesforce – Step by Step tutorial

To create a class in Salesforce go to Setup -> Build -> Develop -> Apex Class and click on NEW button and create class there.

Apex class

now we will create below call there.

public Class CreatingAccount {
	public Account createAccount(String name) { //method to create account
		Account acc = new Account();
		acc.Name = name;
		return acc;
	}
}

Above class is to create/insert new account. This is a simple example to create an Apex class. We will see some more example going forward.

What is a Test Apex Class?

A Test Class in Salesforce Apex is a special class that tests the accuracy, reliability and performance of your Apex code. These play an essential role in that everything works as expected, especially when you make changes or deploy to different environments.

Before you can deploy your Apex code to a production environment, Salesforce requires that it be covered by test methods and at least 75% the ratio of your Apex code must be covered by test method.

  • In Salesforce, Test classes are very important to deploy your code to PRODUCTION.
  • You need to cover at least 75% ( Average coverage of all classes) code coverage by using test methods in Salesforce to deploy your classes to PRODUCTION.

Here we will explain how to write a Test class for the above class. Later will discuss in detail about this test classes.

Text Apex Class Example

@isTest
public class CreateAccountTest {
	static testMethod void testInsertAccount() {
		CreatingAccount ca = new CreatingAccount();
		ca.createAccount('TestclassAcc1');
	}
}

Above class is a simple test class, which covers the code for the above class defined.

Now, how can we know the percentage covered by mytest class to my main Apex Class?

After saving your test class, you will get a button called Run Test. Click on that button, your test class will run.

To see the percentage of code coverage, go to your main class and you can see the percentage under Active column. See the below image for reference.

apex class

This is a small example to create class and test class for that. We will more about Apex classes and Test classes later.

Alternation of Apex Class Creation

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

  • Open the Developer Console.
  • Click the repository tab.
  • The setup Entity type panel lists the different items. We can view and edit in the Developer Console.
  • Click on classes, and then click “New” button .
  • Enter “Message” for the name of the new class and click “ok” bottom.
  • Add the following static method to the new class public static string
 hellowMessage () {

               return ( 'Welcome to salesforetutorial.com');

             }
  • Click on “Save” button.

Example:

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
system.debug(ac);
check the account is created or not in the USER-DEBUG Log.