Classes in salesforce

Classes in salesforce DEFAULT

Create an Apex Class


Apex is a strongly typed, object-oriented programming language that allows developers to execute flow and transaction control statements on the Salesforce platform.

If you’re used to Java or .NET development, you’ll find the programming in Apex fairly straightforward. If you’ve never coded before, you might be surprised how easy it is to access and manipulate data using Apex. In this Quick Start, you'll create a simple Apex class to update old Account records.

Create an Apex Class

The Developer Console is where you write and test your code in Salesforce. We’ll take a tour of the Developer Console and Source Code Editor in just a minute.

The first step is to create an Apex class.

  1. If you haven’t already, log in to Trailhead, then launch your Trailhead Playground by clicking Launch at the bottom of this page. This opens your Trailhead Playground in a new tab.

  2. Click Gear icon to access Setup in Lightning Experience. and select Developer Console.

  3. From the File menu, select New | Apex Class.

  4. For the class name, enter and then click OK.

The code in your editor looks like this:

public class OlderAccountsUtility { }

Excellent! Now let's add a method that can find and update older Accounts.


Adding an Apex Class

This method is called , and it is both public and static. Because it is a static method, you don't need to create an instance of the class to access the method—you can just use the name of the class followed by a dot (.) and the name of the method. For more information, see Static and Instance Methods, Variables, and Initialization Code.

This method takes one parameter, a list of Book records, which is assigned to the variable . Notice the in the object name . This indicates that it is a that you created. Standard objects that are provided in the Salesforce application, such as Account, don't end with this postfix.

The next section of code contains the rest of the method definition:

Notice the after the field name . This indicates it is a that you created. Standard fields that are provided by default in Salesforce are accessed using the same type of dot notation but without the , for example, doesn't end with in . The statement takes the old value of , multiplies it by 0.9, which means its value will be discounted by 10%, and then stores the new value into the field. The operator is a shortcut. Another way to write this statement is . See Expression Operators.

  1. Rubber coated spring clips
  2. New halo 5 armor
  3. Milwaukee angle drill
  4. Dos 2 heroes rest
  5. Nvidia vs intel stock

Manage Apex Classes

An Apex class is a template or blueprint from which Apex objects are created. Classes consist of other classes, user-defined methods, variables, exception types, and static initialization code.

Required User Permissions

Available in: Performance, Unlimited, Developer, and Enterprise Editions

Once successfully saved, class methods or variables can be invoked by other Apex code, or through the SOAP API (or AJAX Toolkit) for methods that have been designated with the keyword.

The Apex Classes page enables you to create and manage Apex classes. To access the Apex Classes page, from Setup, enter in the box, then select Apex Classes. For additional development functionality, use the Developer Console.

To create an Apex class, from the Apex Classes page, click New and write your Apex code in the editor.

While developers can write class methods according to the syntax outlined in the Apex Code Developer's Guide, classes can also be automatically generated by consuming a WSDL document that is stored on a local hard drive or network. Creating a class by consuming a WSDL document allows developers to make callouts to the external Web service in their Apex. From the Apex Classes page, click Generate From WSDL to generate an Apex class from a WSDL document.

You can add, edit, or delete Apex using the Salesforce user interface only in a Developer Edition organization, a Salesforce Enterprise Edition trial organization, or sandbox organization. In a Salesforce production organization, you can change Apex only by using the Metadata API call, the Salesforce Extensions for Visual Studio Code, or the Ant Migration Tool. The Salesforce Extensions for Visual Studio Code and Ant Migration Tool are free resources provided by Salesforce to support its users and partners, but are not considered part of our Services for purposes of the Salesforce Master Subscription Agreement.

Once you have created an Apex class, you can perform various actions.

  • Click Edit next to the class name to modify its contents in a simple editor.
  • Click Del next to the class name to delete the class from your organization.
  • If an Apex class has any methods defined as a , you can click WSDL next to the class name to generate a WSDL document from the class contents. This document contains all of the information necessary for a client to consume Apex Web service methods. All class methods with the keyword are included in the resulting WSDL document.
  • Click Security next to the class name to select the profiles that are allowed to execute methods in the class from top-level entry points, such as Web service methods. For classes that are installed in your organization as part of a managed package, this link only displays for those defined as .
  • Click Estimate your organization's code coverage to find out how much of the Apex code in your organization is currently covered by unit tests. This percentage is based on the latest results of tests that you’ve already executed. If you have no test results, code coverage will be 0%.
  • If you have unit tests in at least one Apex class, click Run All Tests to run all the unit tests in your organization.
  • Click Compile all classes to compile all the Apex classes in your organization. If you have classes that are installed from a managed package and that have test methods or are test classes, you must compile these classes first before you can view them and run their test methods from the Apex Test Execution page. Managed package classes can be compiled only through the Compile all classes link because they cannot be saved. Otherwise, saving Apex classes that aren't from a managed package causes them to be recompiled. This link compiles all the Apex classes in your organization, whether or not they are from a managed package.

Create Classes and Objects

Learning Objectives

After completing this unit, you’ll be able to:

  • Describe the relationship between a class and an object.
  • Define a parameter.
  • Explain the differences between return values.

If this is your first stop on the Build Apex Coding Skills trail, you have come too far. Take a step back and go to the Apex Basics for Admins module. If you have already earned the Apex Basics for Admins badge, then you are in the right place. 

In Apex Basics for Admins, you learned about Apex syntax, variables, collections, and conditional statements. You also ran some sample code in the Developer Console. In this module we build on those concepts. Let’s get started.

What Does Object-Oriented Mean?

If you’ve read anything about software development or coding, you may have run across the term object-oriented: object-oriented classes, object-oriented concepts, object-oriented programming. What does object-oriented mean? 

In relation to programming languages, object-oriented means that the code focuses on describing objects. An object can be anything that has unique characteristics, such as a person, an account, or even a flower. Before you can truly understand what object-oriented means, there are two things that you need to understand: classes and objects. 

Follow Along with Trail Together

Want to follow along with an instructor as you work through this step? Take a look at this video, part of the Trail Together series on Trailhead Live. You can find a link to the full session in the Resources section. 


A class is a blueprint. Objects are based on classes. A class defines a set of characteristics and behaviors that are common to all objects of that class.

Think about a flower. It has characteristics, such as color and height, and it has behaviors, such as grow and wilt. In an Apex class, the characteristics are called variables and the behaviors are called methods.

A single flower.

Characteristics (Variables)
Behaviors (Methods)

Imagine a garden. Although roses, lilies, and daisies have different colors and heights, they’re all flowers and they all have color and height.

A muted color flower representing the class, with three smaller flowers below it representing the instances of the class.

Declaring a Class

This code declares (creates) a class:

public class Flower { //body of class }

Classes are declared using four parts: the access modifier, the keyword "class", the class name, and the class body. The body (inside the curly braces ), is where methods and variables of the class are defined. 

Access Modifier

An access modifier is a keyword in a class or method declaration. The access modifier determines what other Apex code can see and use the class or method. Although there are other access modifiers, is the most common. Public classes are available to all other Apex classes within your org.  


Methods are defined within a class. A method describes the behaviors inherited by objects of that class. A class can have one or more methods. The Flower class has three methods (behaviors): , , and .

A method is declared (created) like this:

 public static Integer wilt(Integer numberOfPetals){ //body of method }


When creating methods, you don’t always know every value needed to run the code. Many times, you write a method that does one task, and then write a second method to do a similar task, and so on. In hindsight you realize that all of your methods do nearly the same thing. They’re duplicates with small variations. 

If only there were a way to provide varied input to a single method. But wait! We can use parameters! A parameter is a variable that serves as a placeholder, waiting to receive a value. Parameters are declared similarly to a variable, with a data type followed by the parameter name. In the following declaration, the method has a parameter named . (Although you can use whatever parameter names you like, it’s a good practice to use names that describe the value that the parameter holds.) The parameter expects to receive a value whose data type is integer.

<p>public static void wilt (Integer numberOfPetals){</p><p> //body of method</p><p>}</p>

How do we call (use) the method? In this code sample, the first line calls (uses) the method and passes (sends) the value . The value that you pass is called an argument. The argument (, in this case) is enclosed in parentheses after the method name.

wilt(4); public static void wilt(Integer numberOfPetals){ system.debug(numberOfPetals); }

After a value is passed to a method that includes a parameter, the argument value becomes the value of the parameter. The parameter functions as a variable, so you can manipulate it like any other variable. 

Let's look at the Flower class.

public class Flower { public static void wilt(Integer numberOfPetals){ if(numberOfPetals >= 1){ numberOfPetals--; } } }

In lines 2-6 of the class, the method checks the value of . If that number is greater than or equal to , then the method decrements (reduces) the by one. 

Return Types

Imagine you’re in a restaurant and you want to know if they have a seasonal dish. You ask the waiter, “Do you have the summer salad?” You expect a particular type of response, not a number or a full sentence, but either “yes” or “no.” 

A method declaration must explicitly state its expected return type. In the same way that an argument must match the data type specified by the parameter, a returned variable must match the return type specified by the method declaration. The return type is a specific data type, such as Boolean, string, or Account, or it can be void (nothing), like this:

public static void wilt(Integer numberOfPetals){ system.debug(numberOfPetals); }

When the method return type is , the method does not return a value. To return a value, replace with a different return type. 

For example, here, the method expects a (response) value that’s an integer.

public static Integer wilt (Integer numberOfPetals){ //body of method}

public static Integer wilt(Integer numberOfPetals){ if(numberOfPetals >= 1){ numberOfPetals--; } return numberOfPetals; }

In line 1, the keyword (immediately after ) indicates that the method returns a value that’s an integer. Line 5 uses the keyword, followed by the variable name, to return the resulting value. When a method returns a variable value, the variable data type must match the return type that the method declared. In this case, the method is expected to return an integer value and the variable is an integer. 

Let’s test the , , and methods. First, we finish coding the methods.

Write the Methods

  1. In the Developer Console, click File | New | Apex Class.
  2. Enter Flower for the class name.
  3. Click OK.
  4. In the Flower.apxc class, replace the existing code with this code:
  5. public class Flower { public static Integer wilt(Integer numberOfPetals){ if(numberOfPetals >= 1){ numberOfPetals--; } return numberOfPetals; } public static void grow(Integer height, Integer maxHeight){ height = height + 2; if(height >= maxHeight){ pollinate(); } } public static void pollinate(){ System.debug('Pollinating...'); } }
  6. Click File | Save.

Now that you have a fully defined class, you’re ready to test it. All you need is the class name (), the methods that you want to test ( and ), and each method’s required arguments. Because the grow method calls (uses) the pollinate method, you don’t need to call the pollinate method directly. The method expects an integer value (for the parameter) and it will return an integer value. The and methods don’t return anything. 

Run the Methods

  1. Click Debug | Open Execute Anonymous Window.
  2. In the Enter Apex Code window, paste this code:
  3. Flower.wilt(4); Flower.grow(5, 7);
  4. Select Open log and then click Execute. The Execution Log opens, displaying the result of running your code.
  5. Select Debug Only.

The method includes two parameters, and . 

passes the arguments (height is 5 inches) and (maximum height is 7 inches) to the method. The method adds 2 to the variable (line 9) and then checks the value of the variable. If the value of the height variable is greater than or equal to the value of the maxHeight variable, the grow method calls the pollinate method.

The pollinate method does not return anything because its return type is void.

What happened?

You should see one entry in the Debug log.

Event: USER_DEBUG. Details: [18]|DEBUG|Pollinating…

What do you expect to happen if you use 2 and 6 for the grow parameters? Try it. 

What happens?

Looking at the debug logs it appears to be nothing. What is happening is that setting the height to 2 and the maxHeight to 6 makes the condition in the if statement false, causing the method not to run. 


Remember that the class is a blueprint for creating flowers. It has , , , and variables. These variables are equal to null (no value), but they can have default values.  

An object is an instance of a class. In the same way that kids inherit genetic traits, such as eye color and height, from their parents, objects inherit variables and methods from their classes. 

color = null;

height = null;

maxHeight = null;

numberOfPetals = null;

Flower Class

Muted shade flower.




Yellow flower.
Red flower.
Pink flower.
color = 'yellow';
color ='red';
color = 'pink';
height = 3;
height = 6;
height = 9;
maxHeight = 6;
maxHeight = 8;
maxHeight = 9;
numberOfPetals = 4;
numberOfPetals = 6;
numberOfPetals = 10;

The object is an instance of the class. Each of the three flower objects is a specific flower based on the class. To create an instance named , based on the class, use this syntax:

You can use dot notation to call an object’s methods. Just add after the object name. For example:

Now you know that objects are instances of classes. Within a class, variables describe the object, and methods define the actions that the object can perform. You’ve also worked with parameters to receive passed values in a variable, and return types, which send something (or nothing, depending on the data type) back to a method.



Salesforce classes in

Apex Class – Simple Class to understand Apex

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

Here we give you the info about how to create a class in salesforce.

Below is the example to create a simple class

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.

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.

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


Go to the Developer console, and execute the following code


❮ PreviousNext ❯

Batch Class in APEX - Salesforce Development Course

Apex - Classes

What is a Class?

A class is a template or blueprint from which objects are created. An object is an instance of a class. This is the standard definition of Class. Apex Classes are similar to Java Classes.

For example, InvoiceProcessor class describes the class which has all the methods and actions that can be performed on the Invoice. If you create an instance of this class, then it will represent the single invoice which is currently in context.

Creating Classes

You can create class in Apex from the Developer Console, Eclipse IDE and from Apex Class detail page as well.

From Developer Console

Follow these steps to create an Apex class from the Developer Console −

Step 1 − Go to Name and click on the Developer Console.

Step 2 − Click on File ⇒ New and then click on the Apex class.

Creating Class

From IDE

Follow these steps to create a class from IDE −

Step 1 − Open Eclipse IDE

Step 2 − Create a New Project by clicking on File ⇒ New ⇒ Apex Class.

Step 3 − Provide the Name for the Class and click on OK.

Once this is done, the new class will be created.

From Apex Class Detail Page

Follow these steps to create a class from Apex Class Detail Page −

Step 1 − Click on Name ⇒ Setup.

Step 2 − Search for 'Apex Class' and click on the link. It will open the Apex Class details page.

Creating Apex Class from Detail Page Step1

Step 3 − Click on 'New' and then provide the Name for class and then click Save.

Creating Apex Class from Detail Page Step2

Apex Class Structure

Below is the sample structure for Apex class definition.


private | public | global [virtual | abstract | with sharing | without sharing] class ClassName [implements InterfaceNameList] [extends ClassName] { // Classs Body }

This definition uses a combination of access modifiers, sharing modes, class name and class body. We will look at all these options further.


Following is a sample structure for Apex class definition −

public class MySampleApexClass { //Class definition and body public static Integer myValue = 0; //Class Member variable public static String myString = ''; //Class Member variable public static Integer getCalculatedValue () { // Method definition and body // do some calculation myValue = myValue+10; return myValue; } }

Access Modifiers


If you declare the access modifier as 'Private', then this class will be known only locally and you cannot access this class outside of that particular piece. By default, classes have this modifier.


If you declare the class as 'Public' then this implies that this class is accessible to your organization and your defined namespace. Normally, most of the Apex classes are defined with this keyword.


If you declare the class as 'global' then this will be accessible by all apex codes irrespective of your organization. If you have method defined with web service keyword, then you must declare the containing class with global keyword.

Sharing Modes

Let us now discuss the different modes of sharing.

With Sharing

This is a special feature of Apex Classes in Salesforce. When a class is specified with 'With Sharing' keyword then it has following implications: When the class will get executed, it will respect the User's access settings and profile permission. Suppose, User's action has triggered the record update for 30 records, but user has access to only 20 records and 10 records are not accessible. Then, if the class is performing the action to update the records, only 20 records will be updated to which the user has access and rest of 10 records will not be updated. This is also called as the User mode.

Without Sharing

Even if the User does not have access to 10 records out of 30, all the 30 records will be updated as the Class is running in the System mode, i.e., it has been defined with Without Sharing keyword. This is called the System Mode.


If you use the 'virtual' keyword, then it indicates that this class can be extended and overrides are allowed. If the methods need to be overridden, then the classes should be declared with the virtual keyword.


If you declare the class as 'abstract', then it will only contain the signature of method and not the actual implementation.

Class Variables


[public | private | protected | global] [final] [static] data_type variable_name [= value]

In the above syntax −

  • Variable data type and variable name are mandatory
  • Access modifiers and value are optional.


public static final Integer myvalue;

You will also like:

Lena said that she never wrote, no matter how much she wanted to. But (the first time I found out about such a case!) She had a friend who told. Her that she once had such a thing.

8882 8883 8884 8885 8886