Chapter 12. Basics of Apex and Visualforce Page

In the previous chapter, we discussed different methods to deploy an application to different organizations. We also discussed the method of deploying component metadata using Force.com IDE, Force.com Migration Tools and followed it up with the process of distributing an application using packages.

In this chapter, we will discuss the basics of Apex programming and Visualforce page concepts. We'll cover the following topics:

  • Introducing Apex
  • Apex data types
  • Creating an Apex class
  • Introducing a Visualforce page
  • Understanding the MVC model

Until now, you have used an out-of-the-box feature-that is, point and click development-to customize your Salesforce org. For example, creating the object's structure, relationships between objects, Process Builders, and approval process to customize it according to your business needs. The point and click development method doesn't require any coding skill.

Introducing Apex

Apex is the world's first on-demand programming language that allows you to implement complex business requirements and transactions on the Force.com platform. There are two types of application development in Salesforce and they are as follows:

  • Declarative development
  • Programmatic development

Apex falls under programmatic development. It is a strongly typed object-based language. It remains well connected with your data, schema, and data manipulation using the query and search language. Apex is included in Developer Edition, Lightning Enterprise Edition, Lightning Unlimited Edition and Database.com. You can write Apex classes or triggers in a developer or sandbox organization. As soon as you finish the development of Apex code, you have to write test methods to cover the implemented Apex code. Once you have greater than 75 percent code coverage, you can deploy the particular Apex code to the production organization. The Apex programming language has the following features:

  • It offers data manipulation language (DML) with built-in exception handling.
  • Salesforce Object Query Language (SOQL) and Salesforce Object Search Language (SOSL) are used to query and retrieve the list of sObject records.
  • Apex has a built-in record-locking mechanism to prevent conflict of record updates.
  • It runs in a multitenant environment. This means the same resource is used by multiple Salesforce.com. customers across the globe. It prevents the monopolizing of shared resources using governor limits. If any particular Apex code violates the limits, error messages are displayed.
  • Apex code is stored as metadata. Therefore, they are automatically upgraded with every release. You don't need to rewrite your code when the platform gets updated.
  • Apex is similar to Java syntax and variables. Syntaxes and semantics of Apex are easy to understand and write code.
  • It provides a built-in feature for unit testing and test execution with code coverage.

You can write Apex logic in the following ways:

  • Apex class: An Apex class includes methods that are related to logic implementation. It can be called from a trigger; you can also associate it with a Visualforce page. Also, it can act as a supporting class, for example, API, Helper classes, or Batch Apex.
  • Apex trigger: A trigger is executed in relation to a particular database action. For example, you can create a trigger on the account object that is executed whenever an account record is deleted. Therefore, triggers are implicitly called from a database action.

Apex data types

Variables are used to store data in a programming language. In Apex classes and triggers, we can use variables that contain data values. Variables must be similar to a data type, and a particular variable should hold the values of the same data type, for example, integer data types should store integer values. All variables and expressions can have a data type mentioned as follows:

  • Primitives: It may appear that Apex primitive variables are passed by values, but they actually use absolute references, similar to Java string behavior. They include the following data types: integer, double, decimal, long, date, time, datetime, string, ID, object, blob, or Boolean.
  • sObjects: The sObject represents standard or custom Salesforce objects that store record data in the database. There is also an sObject data type in Apex; it is a programmatic representation of sObjects and their data in the code.
  • Enums: Enum stands for enumerated list and it is an abstract data type that stores one value of a finite set of specified identifiers. To define an Enum, you have to use the enum keyword in the variable declaration and then define the list of values.
  • Collections: A collection represents a group of objects. There are three different types of collections in Apex, and they are:
    • List: A list is an ordered collection of the primitive's data types dignified by its index. Each element in a list contains two sets of information: an index (this is an integer) and a value (the data). The index of the first element is zero. You can define a list as follows:
                    List<DataType> listName = new List<DataType>(); 
                    List<String> sList = new List<String>(); 
      

    • Set: A set is an unordered collection of data of one primitive data type or sObject that must have unique values. You can define a set as follows:
                    Set<DataType> setName = new Set<DataType>(); 
    
                    Set<String> setName = new Set<String>(); 
      

    • Map: A map is an unordered collection of unique keys of one primitive data type and their corresponding values. You can define a map as follows:
                    Map<PrimitiveKeyDataType, DataType> = mapName =    
                        newMap<PrimitiveKeyDataType, DataType>(); 
                    Map<Integer, String> mapName = new Map<Integer, String>(); 
                    Map<Integer, List<String>> sMap = 
                        new Map<Integer,List<String>>(); 
      

  • An object created from user or system-defined classes.
  • Null (for the null constant)

Creating an Apex class

You can create and modify an Apex class through the Setup menu of the Salesforce organization, Force.com IDE, or Developer Console. You can also do this using third-party development tools, for example, MavensMate IDE for Force.com, Welkin Suite IDE, and so on. The best way to interact with Apex classes via the Setup page is by navigating to Setup (gear icon) | Setup | PLATFORM TOOLS | Custom Code | Apex Classes. It will look like the following screenshot:

Creating an Apex class

The preceding screenshot displays the currently available classes in the org. This page allows you to create new classes, edit and delete existing classes, assign security to existing classes, generate an Apex class from a WSDL file, and schedule an Apex class for future run.

Another way to view, create, and modify a class through the Developer Console is by navigating to Setup (gear icon) | Developer Console. It will look like the following screenshot:

Creating an Apex class

Similar to Apex classes, you can create a new Apex trigger from the Setup page, the Force.com IDE, or the Developer Console. When you are using the Setup page, you must navigate to Setup (gear icon) | Setup | PLATFORM TOOLS | Custom Code | Apex Triggers.

..................Content has been hidden....................

You can't read the all page of ebook, please click here login for view all page.
Reset
18.216.149.94