Hot Posts

Basic Apex - Overview

Basic Apex - Overview

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

Using syntax that looks like Java and acts like database stored procedures, Apex enables developers to add business logic to most system events, including button clicks, related record updates, and Visualforce pages. Apex code can be initiated by Web service requests and from triggers on objects.

How Apex Works in Salesforce

Note : It’s a case insensitive language.

  • Apex syntax looks mostly like a Java programming language.
  • Apex allows developers to write business logic to the record save process.
  • Apex has built in support for unit test creation and its execution.

Apex is a programming language developed by Salesforce specifically for building applications on the Salesforce platform. Here are some characteristics of the Apex language:

  1. Object-oriented:​ Apex is an object-oriented language, which means it supports concepts such as classes, objects, inheritance, and polymorphism. Developers can define custom classes, create objects from those classes, and interact with them using various object-oriented programming principles.
  2. Strongly typed:​ Apex is a strongly typed language, meaning that variables and expressions must have a specific data type that is checked at compile-time. This helps catch potential errors early and promotes code reliability.
  3. Easy to Use: ​Apex is easy to use as it uses Java-like syntax, which is quite easy to understand and code. It uses simple loop syntax, block and conditional statement syntax, object and array annotation, similar to Java.
  4. Integrated: ​Apex provide support of DML operations like Insert, Delete, Update and DML exception handling. Furthermore, it supports looping that allows the processing of multiple records at a time. In addition, it has support for SOQL and SOSL query handling, which return a set of subject records.
  5. Event-driven programming:​Apex provides support for event-driven programming through triggers and event handlers. Triggers are pieces of code that automatically execute in response to specific events, such as record insertion, update, or deletion in the Salesforce platform.
  6. Batch processing:​ Apex supports batch processing, which allows developers to process large sets of data asynchronously in smaller manageable chunks. This feature is particularly useful when dealing with large volumes of records in Salesforce.
  7. Web services integration: ​Apex allows developers to integrate with external web services by making outbound HTTP requests or by implementing web services that can be invoked by external systems. This enables integration with other applications, systems, or services.
  8. Governor limits: ​Apex enforces governor limits, which are predefined limits on resources such as CPU usage, database queries, and data storage. These limits help ensure the stability and performance of the Salesforce platform by preventing excessive resource consumption.
  9. Multitenant Environment: ​Apex runs in a multitenant environment like other lighting platforms, which means only a single instance runs on the server and serves multiple tenants.
  10. Versioned: You can save your code against different versions of the API. This enables you to maintain behavior.
  11. Easy to Test: ​Apex provides built-in support to create and run the unit test. It includes the test result so that the user can get to know how much code is covered. In addition, it ensures the executing of the whole code before any platform upgrades.

This tutorial is targeted for Salesforce programmers beginning to learn Apex. This will bring you to an Intermediate level of expertise in Apex programming covering all the important aspects of Apex with complete hands-on code experience.

Apex OOPs Concepts

Object means a real world / run time entity such as marker, car, table, chair etc. Object-Oriented Programming is a methodology or way to design a program using classes and objects. It eases the software development and its maintenance by providing some beautiful concepts as followed.

  • Classes
  • Objects
  • Encapsulation
  • Polymorphism
  • Inheritance
  • Abstraction

Object

Any real world entity that has state and behavior is known as an object. For example: marker, book, table, chair, mouse, car etc.

Technically, we can say object is an instance of a class or in other words you can say it is an implementation of a class.

Class

Class is a concept or prototype or template i.e. it is a logical entity.

Technically, we can say that we create an individual object of a class.

Encapsulation

It is a binding of code and data together into a single unit known as encapsulation. For example, a capsule, it is wrapped with different types medicines into single unit.

An apex class is the example of encapsulation.

Polymorphism

One name many forms known as polymorphism. Real world example of polymorphism: A person at the same time can have different characteristic. Like a woman at the same time is a mother, a wife, an employee and a daughter etc.

In Apex, we use method overloading and method overriding to achieve polymorphism.

Inheritance

When one class acquires all the properties and behaviors of super/parent class it is known as inheritance. It provides code reusability as well as we can used to achieve runtime polymorphism.

Note: Without inheritance we cannot achieve runtime polymorphism.

Abstraction

Hiding internal complexity and showing functionality is known as abstraction. For example: Car Drive, we don’t know the internal processing.

In Apex, we use abstract class and interface to achieve abstraction.

  • Customized applications
  • Tailored organization processes
  • Collaboration with external systems
  • Custom logic
  • Complex validation

Salesforce Application Anatomy

Apex syntax is similar to the syntax used in programming languages like Java. Here are some key components of the Apex syntax:

  1. Comments:​ Single-line comments start with //.
    Multi-line comments start with /* and end with */.
  2. Declarations:​ Class declaration: public class ClassName { }
    Method declaration: public returnType methodName(parameterList) { }
    Variable declaration: dataType variableName [= initialValue];
  3. Access Modifiers:​ public: Accessible from anywhere.
    private: Accessible only within the same class.
    protected: Accessible within the same class and subclasses.
    global: Accessible across all namespaces.
  4. Data Types:​ Primitive types: Integer, Double, Boolean, String, Date, DateTime, Time, etc.
    Object types: sObject, List, Map, Set, custom classes, etc.
  5. Control Structures:​ Conditional statements: if, else if, else.
    Looping statements: for, while, do-while.
  6. Exception Handling:​ try-catch blocks for handling exceptions.
    throw statement to throw custom exceptions.
  7. Collections:​ List: An ordered collection of elements.
    Set: An unordered collection of unique elements. Map: A collection of key-value pairs.
  8. SOQL (Salesforce Object Query Language):​ Used to query data from the Salesforce database.
    Syntax: SELECT field1, field2 FROM ObjectName WHERE condition.
  9. DML (Data Manipulation Language):​ Used to insert, update, delete, or retrieve records in the Salesforce database.
    Statements: insert, update, delete, undelete, upsert, merge, Database.query, etc.
  10. Trigger Syntax:​ Triggers are used to execute custom code before or after specific database events.
    Syntax: trigger TriggerName on ObjectName (triggerEvents) { }

Post a Comment

0 Comments