Blog

Apex Class in Salesforce

  • (4.0)
  • | 2349 Ratings
Apex Class in Salesforce

Apex Intro:

Apex code is one of the strongly-typed programming languages that execute on Force.com platform. Apex is used to write DB triggers, to add business logic for the apps, and to add program controllers in the user interface layer. Apex has narrow integration with DB, and Query Language and web service support, which includes and governs the execution in multiple environments.

Some of the basic features of APEX are mentioned below:

  • Apex syntax is similar to Java and performs stored procedures.
  • Apex allows the developer to include business logic with a record save process.
  • Apex is on-demand, strongly-typed object-oriented programming language, where it is compiled and stored, and run on Force.com platform.
  • Apex is pre-coded to perform unit testing and execute the test created.

Key concepts of Apex programming language:

  1. Class
  2. Function
  3. Object

Note: Whatever we store in AKA APEX Class, it will be permanent.

Apex Class:

In the real world, you will often find many individual entities, all of the same kind. ( Eg: There may be thousands of mobiles, all of the same make and model.). In the programming world, the ‘class’ is just a blueprint of individual objects created.

Class Syntax:

Class ClassName {
Variables
    Functions
}

Class = Variables + Functions.

Note: Class will not have any memory.

Why variables are used?

To save the data.

Why functions are used?

To perform some work/action. 

A sample program of product delivery to the selected person:

Eg:

Class ProductClass
{
//variable decleration
Integer ProductNameOne;
//function to perform calling action.
Void ProductDelivery(string PersonName)
{
//code to make video call for a person with provided duration
Product Quantity = 30;
}
}

Apex Functions:

Apex functions are used to transform data from records, or in providing values to Visualforce attribs. They are used to evaluate Visualforce expression. 

Types of Functions: 

We have different types of functions, of which 2 are popular. They are mentioned below.

Default Functions.

User-defined functions.

Code -  system.debug(“Hello World”);

Apex Default functions: These are the functions which are predefined by salesforce. The syntax and the structure are to be used as below mentioned.

System.debug” - This is a default function for printing the output.

Eg for User-defined functions: These are the functions which can be customized according to the user requirement.

Code:    //function to perform calling action.
Void ProductDelivery(string PersonName)

ProductDelivery” - Custom function defined in the above sample code is a user-defined function.

Apex Objects:

Apex Object is also called a Variable of APEX class. In Computer Science, an object is a “Class in execution” or “Class in reality”.

In the programming world, “object” refers to a real instance of a class where the object can be a combination of variables, functions + some memory.

Eg: integer = age; - (Integer = class & age = object).

Class = Variables + Functions.
Object = Variables + Functions + MEMORY.

<strong style="font-family: -apple-system, BlinkMacSystemFont, 'Segoe UI', Roboto, Oxygen, Ubuntu, Cantarell, 'Open Sans', 'Helvetica Neue', sans-serif;">Syntax For Object Creation:<br /></strong>
ClassName AnyObjectName = New ClassName();

Eg: ProductClass MyObjectProduct = New ProductClass();

Memory is created for object name = “MyObjectProduct”.

How to start working with the Object?

To start working with the object, we need to

  • Call the function.
  • Store data in a variable.

How to call the function?

The function can be called in the below-mentioned process:

[Eg : MyObjectProduct.ProductDelivery(string PersonName);]
ProductDelivery = Custom function / User defined function.
System.debug = Standard function.

Scenario - Sample program for the class, function, & object declarations with output.

  • Create a class ‘CalculatorAPEXClass’ which will give sum & multiplication of 2 numbers.
  • We will get these 2 numbers from the caller of the function.
  • Sum function will return value to the caller.
  • Multiplication function won’t return, it will just print.
  • The result variable you declare in class level, not inside a function.

Code:

Public Class CalculatorAPEXClass {

    //variable declaration at class level
    Private integer Sum;
    Private integer Multiplication;
    //function for sum
    Public integer MakeSum(integer NumA, integer NumB)
{
//sum is class level variable
//NumA is local / function level variable
Sum = NumA + NumB;
Return Sum;
}
//function of multiplication
Public void MakeMultiply(integer NumC, integer NumD)
{
Multiplication = NumC * NumD;
system.debug(‘The Answer = ’ + Multiplication);
} 
} 

Note: Public/Private are called as access specifiers in the program.

Check Out Salesforce Tutorials

In a class, we have two values they are, 

  • Variable - Variable is a private value.
  • Function - Functions is a public value. So, we declare variables by private and we declare class/function by the public.

Steps to create an Apex Class

Login to salesforce org → Click on the Login name → Click on “Developer Console” → File → New → Apex Class → Write the name of the class you want to save on(eg: CalculatorAPEXClass ) → Ok.

Steps to create an Apex Class

NewApexClass 

Then, the apex class is created as shown below. Now, copy the code on the screen available and Save(Ctrl + S).

CalculatorAPEXClass

CalculatorAPEXClass

Note: Class will have no memory which is the biggest drawback. Without memory, the code is useless as we need to create an Object for it.

Creating an Object: In (Ctrl + E) or Anonymous window

//Object Declared
CalculatorAPEXClass MyObject = New CalculatorAPEXClass();

//call sum function
Integer AnswerOfSum = MyObject.MakeSum(20,10);
system.debug(‘What is the value?  = ' + AnswerOfSum);

//call multiply function
MyObject.MakeMultiply(20,10);

OutPut:

16:59:28:002 USER_DEBUG [11]|DEBUG|What is the value?  = 30
16:59:28:002 USER_DEBUG [20]|DEBUG|The Answer  = 200

Checkout Salesforce Interview Questions

Note: To design any code, we need APEX Class.

To execute any code, we need Object.

In Salesforce, most of the time, the function inside the class can be used without using Object or without creating any Object!!

Why Class is Useless?

As class does not have memory, it’s of no use. But if we provide memory, it’s useful. For this, we specifically declare the function/variable as ‘Static’.

What is Static?

When we declare any function as static, we need not generate an object of the class for executing the function. We can execute/call function using the class name directly.

The reason behind this is, as soon as we declare any function/variable as static, we get “special memory” for that entity to perform the work and this way, we can skip creating an object, and complete the work successfully.

Why It’s called Special Memory?

This special memory helps the data to persist/be available across different pages/classes, unlike normal variable. So, we can refer to the value anywhere.

In short, in Salesforce Real-Time Projects, we will create static variables and function most of the time.

Sample code for static class declaration:

Scenario - In this scenario, we have to make a video call using mobile to a particular person.

Syntax to execute: ClassName.FunctionName(Parameter);

Class>>
Public Class AllMobileClass{
//variable decleration
Private static integer DurationOfCall;
Public static void CallAPerson(integer NumToCall){
//code to video call a person
//To execute
AllMobileClass.CallAPerson(9912345678);
}
}

Program #1 - Make changes in such a way that we can bypass Object and still call functions.

Class ClassForSum{
    Integer answer;
    Integer MakeSum(integer a, integer b){
    Answer = a+b;
    Return answer;
}
}

<strong style="font-family: -apple-system, BlinkMacSystemFont, 'Segoe UI', Roboto, Oxygen, Ubuntu, Cantarell, 'Open Sans', 'Helvetica Neue', sans-serif;">Actual Code after changes: (Solution - Make the code into “Static”)<br /></strong>
Class ClassForSum{
    Static Integer answer;
    Static Integer MakeSum(integer a, integer b){
    Answer = a+b;
    Return answer;
}
}

How to call a function?

Syntax: ClassForSum.MakeSum(20,10);

Program#2 - Sum, Multiply below code without object declaration (using static in class function).

Code:

Public Class CalculatorAPEXClassWithoutObject {
    //variable declaration at class level
    Private integer Sum;
    Private integer Multiplication;
    //function for sum
    Public integer MakeSum(integer NumA, integer NumB)
{
//sum is class level variable
//NumA is local / function level variable
Sum = NumA + NumB;
Return Sum;
}
//function of multiplication
Public void MakeMultiply(integer NumC, integer NumD)
{
Multiplication = NumC * NumD;
system.debug(‘The Answer = ’ + Multiplication);
} 
} 

In the next topic, we will discuss in detail about “How to call APEX Class”. Keep following us for more info on Salesforce Development / Programming.

Explore Salesforce Sample Resumes! Download & Edit, Get Noticed by Top Employers!Download Now!

Mindmajix offers different Salesforce certification training according to your desire with hands-on experience on Salesforce concepts

Subscribe For Free Demo

Free Demo for Corporate & Online Trainings.

Arogyalokesh
About The Author

Arogyalokesh is a Technical Content Writer and manages content creation on various IT platforms at Mindmajix. He is dedicated to creating useful and engaging content on Salesforce, Blockchain, Docker, SQL Server, Tangle, Jira, and few other technologies. Get in touch with him on LinkedIn and Twitter.


DMCA.com Protection Status

Close
Close