Wednesday, June 29, 2016

Apex Syntax

Apex code contains many things which you might be familiar with from Java or other programming languages.

Variable Declaration:
You must declare every variable with data type in Apex like primitive, sObject, collection, Object. Fore example, the code bellow is declared with data type as collection of Account.
List lstAcc;

SOQL:
It will be used to fetch the data from Salesforce object in database. Fore example, The code below is fetching data from Account object and assigning records to variable lstAcc.
lstAcc=[SELECT id, name, description FROM Account];

Loop Statement:
This statement is used for iterating over a collection or iterating over a piece of code for specified number of times. In code bellow shown how to iteration over list of Account (lstAcc).
for(Account a: lstAcc){
a.description='Test loop';
}


Conditional (If-Else) Statements:
you can cause Apex code to execute based on a certain condition, it is decided whether to go for execution or to stop the execution of the particular piece of code. For example, in the code shown below, it is checking whether the list contains records or it is empty.
if(lstAcc.size()>0){
System.debug('>Record size:'+lstAcc.size());
}
else{
System.debug('>list is empty');
}


DML Statement:
Executes the records insert, update, upsert, delete operation on the records in database. For example, the below code is updating list of Account with new field value.
for(Account a: lstAcc){
a.description='Test loop';
}
if(lstAcc.size()>0){
update lstAcc;
}



Saturday, June 25, 2016

How Does Apex Work?

All Apex runs entirely on the Force.com platform, as shown in the following picture of diagram:
Apex is compiled, stored, and runs entirely on the Force.com platform

Following are two sequence of flow of actions when the developer saves the code and when an end user perform some action which invokes the Apex code:

-When a developer writes and saves code to the platform, the platform application server first compiles the code into an abstract set of instructions that can be understood by the Apex runtime interpreter, and then saves those instructions as metadata.
-When an end-user triggers the execution of Apex, could be clicking a button or accessing a Visualforce page, the platform application server retrieves the compiled instructions from the metadata and sends them through the runtime interpreter before returning the result. The end-user observes no differences in execution time from standard platform requests.

Note: Apex does not support some features which a general programming language supports. The following example are some features that Apex does not support:
-Render elements in the user interface other than error messages.
-Customization of standard force.com provided functionality.
-It is not possible to prevent the standard functionality execution.
-Creation of multiple threads.
-Creation of temporary file.

When Should I Use Apex?

Salesforce provides the ability to customize the pre-built applications to fit organization. But, the organization may have complex business processes that are unsupported by the existing functionality. So apex should be used to implement the complex business functionality. Theses includes APEX, Visualforce and the SOAP API.

Apex
Use Apex if you want to:
-Create Web services with integrating external system.
-Create email services. For example, automatically creates contact records based on contact information in messages.
-Perform complex validation over multiple salesforce objects.
-Create complex business processes that are not supported by workflow functionality or flows.
-Create custom transactional logic (logic that occurs over the entire transaction, not just with a single record or object) like using the Database methods to insert, update or delete the records.
-Attach custom logic to another operation, such as saving a record that causes trigger to fire, an action on Visualforce page, or from SOAP API.

Visualforce
Visualforce consists of a tag-based markup language which gives developers a powerful way of building applications and customizing user interface. With Visualforce you can:
-Create wizards and other multi-step processes.
-Create custom flow control through an application.
-Define navigation patterns and data-specific rules for optimal, efficient application interaction.

SOAP API
-Use standard SOAP API calls if you want to add functionality to a composite application that processes only one type of record at a time and does not require any transactional control (setting a Savepoint or rolling back changes).
-Use SOAP API to create, retrieve, update or delete records, such as accounts, leads, and custom objects
-SOAP API also allows you to maintain passwords, perform searches, and much more.

Friday, June 24, 2016

What Is Apex?

Apex is programming language which has been developed by Salesforce.com. It is a strongly type, Object oriented programming language allows developer to write the code and execute the flow and transaction control statement on Salesforce platform (Force.com) server in conjunction calls Force.com API. It looks like Java syntax and acts like database store procedures. Apex enables developer to add business logic to system events, including button clicks, record updates and Visualforce page. Apex can initiated by Web service request and trigger on salesforce objects.

It is available in:
- Salesforce Classic and Lightning Experience - Unlimited, Enterprise, Performance, Develop and Database.com Editions


Apex as a Language, It is:

Integrated
Apex provides built-in support for:
-DML calls, such as INSERT, UPDATE, and DELETE.
-Include built-in DmlException handling.
-Inline SOQL and SOSL queries which return collection of sObject records.
-Looping for bulk processing of multiple records at a time.
-Locking syntax that prevents record update conflicts.
-Custom public Force.com API calls that can be built from stored Apex methods.
-Warnings and errors occurred when a user tries to edit or delete a custom object or field that is referenced by Apex.

Easy to use
-Apex has syntax very similar to Java, such as variable and expression, block and conditional statement, loop, object and array notation, and so on.
-Apex uses syntax and semantics that are easy to understand and encourage efficient use of the Force.com platform.
-Apex produces code that is both succinct and easy to write.

Data focused
-Apex executes multiple query and DML statements into a single unit of work on the Force.com platform server.
-Apex does not attempt to provide general support for rendering elements in the user interface.

Rigorous
-Apex is a strongly-typed language.
-Apex uses direct references to schema objects such as object and field names.
-Apex fails quickly at compile time if any references are invalid.
-It stores all custom field, object, and class dependencies in metadata to ensure they are not deleted while required by active Apex code.

Hosted
-Apex is interpreted, executed, and controlled entirely by the Force.com platform.

Multitenant aware
-Apex runs in a multitenant environment.
-Apex runtime engine is designed to guard closely against runaway code, preventing it from monopolizing shared resources.
-Any code that violates limits fails with easy-to-understand error messages.

Automatically upgradeable
We do not upgrade it manually. Because Apex is upgraded as part of Salesforce releases.

Easy to test
Apex provides built-in support for unit test creation and execution, including test results that indicate how much code is covered, and which parts of your code could be more efficient.

Versioned
-Apex code can be saved against different versions of the Force.com API.
-Apex is included in Performance Edition, Unlimited Edition, Developer Edition, Enterprise Edition, and Database.com.