Here i this post i will explain about hoe to create a simple Apex Class?
Apex is strongly typed Object oriented programming language and it will run on Force.com platform.
Here i will give you the info about how to create 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 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 i will tell you how to create class i salesforce?
To create a class in salesforce go to Setup -> Build -> Develop -> Apex Class and click on NEW button and create class there.
now i will create below call there.
public Class CreatingAccount
public Account createAccount(String name) //method to create account
Account acc = new Account();
acc.Name = name;
Above class is to create/insert new account. This is simple example to create an apex class. We will see more later.
Now here i will give you an example to create test class.
What is TEST class? Test class are allow 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 salesoforce to deploy your classes to PRODUCTION.
Here i will explain how to write test class for above class. Later will discuss more about test classes.
public class CreateAccountTest
static testMethod void testInsertAccount()
CreatingAccount ca = new CreatingAccount();
Above class is simple test class. Which covers the code for above defined class.
Now how can know percentage covered by mytest class to my main Apex Calss?
After saving your test class, you will get a button called Run Test. Click on that button, your test class will run.
TO see percent code coverage go to you main class and see the percentage. See the below image for reference.
This is a small example to create class and test class for that. We will more about Apex classes and Test classes later.