Salesforce is a unified customer relationship management (CRM) platform that seamlessly integrates business applications, customer service operations, organizational data, and metadata.
According to Salesforce, over 150,000 companies leverage its ecosystem to drive both customer and employee success.
Apex is a strongly typed, object-oriented programming language that enables developers to execute code on Salesforce servers. It helps them to implement custom business logic across various system events.
This Salesforce Apex tutorial explores the Apex programming language in depth, covering its features, capabilities, and practical applications.
Table of Contents:
The Apex language in Salesforce allows developers to implement complex business logic, automate business processes, and enable integrations.
Let’s go through the key pointers about the Apex programming language.
Now that you understand the basics of Apex programming. To better understand how Apex works in real-time scenarios, consider going through a structured Salesforce developer training.
We’ll take a look at the Salesforce Visualforce before diving deep into Apex programming.
Visualforce is a component-based user interface framework of Salesforce.
Visualforce includes about 100 built-in UI components. It has a mechanism that allows developers to create custom components, which are reusable building blocks for user interfaces.
In short, component-based user interfaces are the foundation of Salesforce Visualforce. Visualforce uses a tag-based language comparable to HTML.
An Apex class that enhances the functionality of a standard or custom controller is called a controller extension.
You use controller extensions:
You can use Salesforce controller extensions with standard and custom controllers.
Below are the real-time scenarios that show how to use controller extensions.
Apex Class
public class MyControllerExtensionExample5Mar{
public static string message{get;set;}
public static string VariableForStoringAccountName {get; set; }
public MyControllerExtensionExample5Feb(ApexPages. StandardController stdController) {
//write any code which u want to execute at the start
}
public static void ShowGreeting() {
message = 'Welcome to Extension demo' ;
}
public void mysave() {
Account NewAccount = New Account();
NewAccount. Name = VariableForStoringAccountName;
NewAccount. industry = 'Chemical';
insert NewAccount;
}
}
<apex:page standardController="Account" extensions="MyControllerExtensionExample">
<apex:form >
<apex:pageBlock title="Page Block 1">
<apex:pageBlockSection title="Page Block Section 1 | Custom Controller Example" Columns="2">
<apex:pageBlockSectionItem ><Apex:commandButton value="Greeting" reRender="id1" Action="{!ShowGreeting}"/></apex:pageBlockSectionItem>
<apex:pageBlockSectionItem ><Apex:outPutLabel id="id1"> {!message} </Apex:outPutLabel> </apex:pageBlockSectionItem>
</apex:pageBlockSection>
</apex:pageBlock>
<apex:pageBlock title="Page Block 2">
<apex:pageBlockSection title="Page Block Section 2 | Standard Controller Example" Columns="2">
<apex:pageBlockSectionItem >New Company:<apex:inputField value="{!Account.name}" required="False" /></apex:pageBlockSectionItem>
<apex:pageBlockSectionItem ><apex:commandButton value="Standard Save" action="{!save}"/></apex:pageBlockSectionItem>
</apex:pageBlockSection>
</apex:pageBlock>
<apex:pageBlock title="Page Block 3">
<apex:pageBlockSection title="Page Block Section 3 | Using Custom code For Save" Columns="2">
<apex:pageBlockSectionItem >New Company:<apex:inputText value="{!VariableForStoringAccountName}" /></apex:pageBlockSectionItem>
<apex:pageBlockSectionItem ><apex:commandButton value="Custom Save" action="{!mysave}"/></apex:pageBlockSectionItem>
</apex:pageBlockSection>
</apex:pageBlock>
</apex:form>
</apex:page>



A custom controller is an Apex class that executes the logic of a Visualforce page without a standard controller. Use custom controllers to run Visualforce pages completely in system mode.
Custom controllers in Salesforce allow data manipulation and custom logic for Visualforce pages. They are used to fetch and manage data displayed on Visualforce pages.
You can use a custom controller when you need to:
Types of Custom Controller:
Create a Custom Controller Apex Class:
Apex provides many utility and system classes for developing custom controller logic. To create a custom controller class, you need to follow the steps below:
Creating a Visualforce page that uses a Custom Controller:
<apex : page : controller="EmailidListController">
<apex : form>
< Apex: page block: Email Id List id =" Email Id_List">
</apex : pageBlock>
</apex : form>
</apex : page>
Controller Methods:
Below are the controller methods used in Salesforce Apex.
In summary, custom controllers implement the logic of Visualforce pages.
Standard controllers can be used with many objects, including Asset, Case, Contact, Product, Contract, and more.
Visualforce uses Model-View-Controller (MVC) techniques to display data. They can be described as follows:
Accessing data with a Standard controller:
Each standard controller has a getter method that gives the record mentioned by the 'name' query string argument in the page URL.
This method enables the related page markup to reference the fields of the selected record using the {!object} syntax, where the object related to the controller is written in lowercase.
Example::
If a page uses an account standard controller, it uses (!account.name) to determine the value of the name field.
Standard Controller Actions:
The standard controller Actions are as follows:
Styling Pages Through the Standard Controller:
The page for a standard controller systematically inherits the style used for regular Salesforce pages for the specified object.
Pick the object that looks to be chosen. You can overrule the page styling that uses the standard controller through the 'tab style' property of the <Apex: page> tag.
Example:
<apex: page StandardController=" MindMajix" tabstyle="Python training">
</apex : page>
Validation Rules and Standard Controllers:
If a user enters data on a Visualforce page that accesses a standard controller and that data triggers a validation rule fault, the fault can be displayed on the Visualforce page.
If the validation rule error is at the top of the page, you will use the <Apex:pageMessage> or <Apex:message> component within the <Apex:page> to display the error.
An exception is an abnormal event that typically occurs during program execution. It affects the normal program flow.
Exception Handling is a feature in Salesforce that allows us to handle unexpected exceptions or errors in our code without interrupting the flow.
Prototypes:
They return the type name (list and sequence of input declarations). One basic reason you need interfaces is that multiple inheritance is possible only through them.
Syntax for interface:
Interface name {
Prototypes
}
Class child implements name {
Features are mandatory to define the prototyped methods in the interface
}
Example:
Global interface calc {
Void add ();
Void sub ();
}
Class opers implements calc {
Return a+b;
}
Public integer sub () {
Return a- b ;
}
}
Public class test {
Public static test method void main () {
Opers op1 = new opers ();
Op1.add (5,4);
System. debug (' subtraction is ' +sub);
}
}
Collections:
It stores large volumes of information. There are three types of collections in Salesforce.
SOQL:
You can use Salesforce Object Query Language (SOQL) to retrieve the data by using a 'select' statement. You can search only for Salesforce objects in SOQL.
SOSL:
You can use Salesforce Object Search Language (SOSL) to retrieve the data from the database. You can use the FIND statement for this.
Collection Program:
Public with sharing class call test 1 {
Public static test method void main () {
List < string > stud_names = new list < string > ();
Stud_names. Add ('Ashok');
Stud_names. Add ('Yadav');
Stud_names. Add ('Buddi');
For (string str: stud_names) {
System. Debug ('name; ' +str);
}
List prog_books =new list ();
Prog_books = <
Book_c Sb = new book_c ();
Sb. name = 'apex prog';
Sb.price _ c = 200;
Sb. Author_c = 'krishna';
Try {
Insert prog_books;
}
Catch (exception e) {
System.debug ('exceeded limit');
}
Prog-books = <
For (Book_c b: prog_books){
System.debug (b. price_c);
System.debug (b. author_c);
}
}
}
Triggers are apex code that executes in response to database events.
Trigger trigger name on object name (trigger_events){
Code_block
}
There are two types of triggers.
Context Variables:
The context variables provide runtime information about the trigger and the database. These variables are listed below.
Example:
Trigger t1 on food_c (before insert) {
Last < food_c> fc = trigger. New ;
For (food_c f:fc)
{
If (f. price_c <300)
{
f. add error ('do not eat too much');
}
}
}
Example:
Trigger t1 on student_c (before insert, after insert, before update, after update) {
+ List < student_c> b= trigger. New
For (student_c S : b) {
If (S. name_c == 'harishnath') {
S. add error ('do not allow this student');
}
}
}
Apex is a strongly typed, object-oriented programming language that allows developers to execute flow and transaction control statements on the Salesforce platform server.
Apex Code can be initiated by Web service requests and from triggers on objects.
Apex is generally of two types:
There is an Apex interface that is schedulable.
Example:
Schedulable {
Void executes (schedulable context sc)
}
Class time bomb implements schedulable {
Void executes (schedulable context Sc) {
System.debug (' I love the world');
}
}
Batch Apex operates over small batches of records, covering your entire record set. It breaks the process down into manageable chunks.
The Interface used for a batch Apex is Database.Batchable, where the database is a namespace or class containing the Batchable interface.
An interface is a class in which the method signatures are present, but the body of the method is absent.
The following are the methods of an interface.
In Salesforce, Triggers are known as Apex Triggers. The triggers are unique. A trigger in Salesforce is Apex code that you can use to run operations before and after a record is updated.
As per the execution order, you have two types of triggers:
While using this event, you can execute the code block before you insert a new record. Data is committed to the database after the Insert event.
When using this event, execute the code block first. After that, you will insert the record.
You can use the before trigger to perform the logic on the same object. You can use the ‘before event’ trigger if you need to update the same record in the trigger.
In the before trigger, the records are not committed to the database. Therefore, you will skip the DML.
When the record is committed to the database, i.e., the record ID is created, our trigger records it in read-only mode. You cannot update the values.
Description:
Description
Well! This Salesforce Apex tutorial might have given you the skills to develop Salesforce applications in real-world scenarios.
1. Is it easy to learn Salesforce Apex?
Ans: Yes, learning Salesforce Apex is quite easy. Gaining some knowledge of OOP concepts will help you learn Apex programming more quickly.
2. When should I use Salesforce Apex?
Ans: You should use Salesforce Apex when tools like Process Builder or Flow Builder are insufficient for developing complex Salesforce solutions.
3. How long will it take to learn Salesforce Apex?
Ans: You can learn Salesforce Apex language in 2-3 weeks. If you work through more hands-on exercises after the training, it will help you become a skilled Salesforce developer more quickly.
4. Is Apex similar to other programming languages?
Ans: Yes, Apex is similar to Java programming. Java developers can easily learn Apex programming.
5. Can I get any Salesforce Apex learning resources?
Ans:
Salesforce Apex empowers you to build customizable Salesforce applications. We hope you have gained sufficient mastery of Salesforce Apex concepts to help streamline business processes and enhance productivity.
If you want to explore more about Salesforce Apex, you can join MindMajix’s Salesforce developer training. By the end of the training, you will become a skilled Apex developer with the capacity to build high-quality Salesforce applications.
Public class EmailidListController {
Code
}
Our work-support plans provide precise options as per your project tasks. Whether you are a newbie or an experienced professional seeking assistance in completing project tasks, we are here with the following plans to meet your custom needs:
| Name | Dates | |
|---|---|---|
| Salesforce Training | May 12 to May 27 | View Details |
| Salesforce Training | May 16 to May 31 | View Details |
| Salesforce Training | May 19 to Jun 03 | View Details |
| Salesforce Training | May 23 to Jun 07 | View Details |