Apex Class

Here in this post, we will learn how to create a simple Apex Class?

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.

Now we will learn how to create a class in Salesforce?

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.

Test Class

Now here we will give you an example to create a test class.

What is TEST class? A test class is allowed to create test methods weather your functionality is working or not.

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.

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