Você está na página 1de 34

Apex Concepts

November 17, 2009 TCS Confidential

Agenda
What is Apex

Why Apex?
When To Use Apex Apex Framework

Traditional Code v/s Apex code


Invoking Apex & Tools for writing Apex Apex

Dos & Donts in Apex


Governor Limits in Apex Deploying Apex Scripts

November 17, 2009

What Is Apex
Apex is an object-oriented programming language that allows developers to execute flow and transaction control statements on the Force.com platform server. Apex allows developers to add business logic to most system events, including button clicks, related record updates and UI displays. Apex scripts can be initiated by Web service requests and from triggers on objects. Apex is based on familiar Java idioms, such as variable and expression syntax, block and conditional statement syntax, loop syntax, object and array notation, pass by reference, etc. Apex is interpreted, executed, and controlled entirely by the Force.com platform. Apex never needs to be rewritten when other parts of the Force.com platform are upgraded. Because the compiled code is stored as metadata in the platform, it always gets automatically upgraded with the rest of the system. It runs natively on the Salesforce servers, making it more powerful, and faster than non-server code, such as JavaScript/AJAX. Apex provides built-in support for unit test creation and execution.

November 17, 2009

Agenda
What is Apex Why Apex? When To Use Apex Apex Framework

Traditional Code v/s Apex code


Invoking Apex & Tools for writing Apex Apex

Dos & Donts in Apex


Governor Limits in Apex Deploying Apex Scripts

November 17, 2009

Why Apex?
Performance cost of making multiple round trip to accomplish common business transaction. Efficient Transactional control. Record locking to avoid concurrency and dead lock. Complied at Force.com platform, no processing at client browser to improve performance. Compiled apex code talks with meta data instead of database. Metadata also stores information about relations and dependencies between records. Extends salesforce functionality beyond standard functions and in the way you want.

November 17, 2009

Agenda
What is Apex

Why Apex?
When To Use Apex Apex Framework

Traditional Code v/s Apex code


Invoking Apex & Tools for writing Apex Apex

Dos & Donts in Apex


Governor Limits in Apex Deploying Apex Scripts

November 17, 2009

When To Use Apex


Use Apex if you want to : You need to apply complex business logic to rows of data being saved by any means. You need to create additional Web Services API functionality for exposing logic either within Salesforce or to external applications. You need to call out to external Web Services and process the results. You need to handle incoming or outgoing emails in ways more complex than the declarative functionality.

November 17, 2009

Agenda
What is Apex

Why Apex?
When To Use Apex Apex Framework

Traditional Code v/s Apex code


Invoking Apex & Tools for writing Apex Apex

Dos & Donts in Apex


Governor Limits in Apex Deploying Apex Scripts

November 17, 2009

Apex Framework

November 17, 2009

Apex Framework
When a developer writes and saves an Apex script, 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, perhaps by clicking a button or accessing an s-control, 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.

November 17, 2009

Apex Capabilities
DML calls to insert, update, and delete records. Inline SOQL or SOSL statements for retrieving records. Looping Control Structures that help with bulk processing. A record locking syntax that prevents record update conflicts. Custom public API calls. Send and receive emails. Web Services or XML request/response integrations. Warnings and errors to prevent objects referenced by Apex from being modified. A full testing and deployment framework.

Apex Limitations
It is not a general purpose language like C# or Java. Spawn threads Access Java Libraries

November 17, 2009

Agenda
What is Apex

Why Apex?
When To Use Apex Apex Framework Traditional code v/s Apex code Invoking Apex & Tools for writing Apex Apex

Dos & Donts in Apex


Governor Limits in Apex Deploying Apex Scripts

November 17, 2009

Traditional code v/s Apex code

November 17, 2009

Multi-Tenant Data and Code


Unique Schema and data Unique code and logic

Shared query engine (Apex DB)

Multi-Tenant Database

Multi-Tenant Code

Shared code execution (Apex VM)

November 17, 2009

Agenda
What is Apex

Why Apex?
When To Use Apex Apex Framework

Traditional Code v/s Apex code


Invoking Apex & Tools for writing Apex Apex

Dos & Donts in Apex


Governor Limits in Apex Deploying Apex Scripts

November 17, 2009

Tools for Writing Apex


Salesforce.com User Interface.

November 17, 2009

Invoking Apex Execution


Apex scripts can be executed from any of the below; Triggers on standard and custom objects. Apex classes which can be invoked from triggers, scontrols, web service API and other classes.

Tools for Writing Apex


Salesforce.com User Interface. Eclipse IDE with Force.com plugin.

November 17, 2009

Agenda
What is Apex

Why Apex?
When To Use Apex Apex Framework

Traditional Code v/s Apex code


Invoking Apex & Tools for writing Apex Apex

Dos & Donts in Apex


Governor Limits in Apex Deploying Apex Scripts

November 17, 2009

Classes
A class is a template or blueprint through which objects can be created. Classes contain variable and methods. Class methods and variables can be invoked by other apex scripts. Class syntax consists of An access modifier Optional definition modifiers (virtual, abstract, etc.) The keyword class The unique name Optional extensions or implementations
private | public | global [virtual | abstract | with sharing | without sharing | (none) ] class ClassName [implements InterfaceNameList | (none) ] [extends ClassOrInterfaceName | (none) ] { // Class Body }

November 17, 2009

Classes
Static variable and methods Final With sharing and without sharing Constructor Special method used to create (or instantiate) an object out of a class definition Do not have explicit return types Can be overloaded Class access modifiers Global, Public and Private Variable and Method access modifiers Interface and implementation Inheritance Virtual Class: This class allows for extensions and overrides. Abstract Class: This class contains abstract methods and is designed to be extended. Override: This method overrides base class methods

November 17, 2009

Data Types
All variables and expressions have one of the following data types: A primitive (Integer, Boolean, String, Date, Datetime, Id, Decimal, Long, ) Enum sObjects (Account, Contact, ) A collection (List, Set or Map) An object created from user or system defined classes Null Methods can return values of these type or return no value (void).

November 17, 2009

SOQL Queries
Similar to the SELECT command in SQL and allows to specify the source object and a list of fields along with an optional WHERE clause. SOQL statements evaluate to list of sObjects, a single sObject, or an Integer Example: Case[] accList = [Select CaseNumber from Case where Stage = Closed]; SELECT * is not allowed. It is required that you specify each field that you want to query. Referencing parent or child records is also possible in SOQL.

SOSL Queries
SOSL allows searching multiple fields for multiple objects with a single query. SOSL statements evaluate to a list of List of sObjects where each list contains the search results for a particular sObject type, returned in the same order as listed in the query. Example: List<List<SObject>> searchList = [FIND 'map*' IN ALL FIELDS RETURNING Account (id, name), Contact, Opportunity, Lead];

November 17, 2009

Loop Statements
Apex supports five types of procedural loops: do {statement} while {Boolean_Condition}; while {Boolean_Condition} statement; for ( Initialization; Boolean_exit_Condition; increment ) statement; Eg: for(i=0; i<10; i++) for ( variable : array_or_set ) statement; Eg: for(Account acc : accList) for ( variable: [inline_soql_query] ) statement; Eg: for(Account acc : [Select Name from Account]) All loops allow for the following commands: break: exits the loop. continue: skips to the next iteration in the loop.

November 17, 2009

Apex Script Example

November 17, 2009

Triggers
Trigger is an apex script that occurs when a data manipulation language (DML) event occurs on a specific sObject. DML Events include: insert, update, delete, undelete The trigger can be before event or after event trigger Before event triggers are normally used to validate or update data before they are saved to the database. After event triggers are normally used to affect changes in other records that may be related to this record. Trigger syntax trigger <triggerName> on <ObjectName> (<triggerEvents>) { // Trigger body } Eg: trigger checkDuplicateContact on Contact (before insert, before update) { } Trigger context variables: isInsert, isBefore, New Old Newmap OldMap
November 17, 2009

Agenda
What is Apex

Why Apex?
When To Use Apex Apex Framework

Traditional Code v/s Apex code


Invoking Apex & Tools for writing Apex Apex Script Example Dos and Donts in Apex Governor Limits in Apex Deploying Apex Scripts

November 17, 2009

Dos & Donts


Dos
Handle dynamic bulk processing Use Set,Map,List objects Query database using Set/Map variables

Donts
Hardcoding of Ids and values Extensive use of traditional arrays Query database inside loops

November 17, 2009

Dos
Create reusable code in Apex classes which can be called from triggers.Due to this,all business logic is maintained in Apex class section in salesforce,which results in easy understanding of business. Creation of dummy records in testmethods.

Donts
Scattering of code in triggers resulting in difficulty to understand the business logic in the Org.

Rely on existing database records in testmethods.

November 17, 2009

Agenda
What is Apex

Why Apex?
When To Use Apex Apex Framework

Traditional Code v/s Apex code


Invoking Apex & Tools for writing Apex Apex Script Example

Dos & Donts in Apex


Governor Limits in Apex Deploying Apex Scripts

November 17, 2009

Governor Limits
Apex runs in an multi-tenant environment and therefore the runtime engine needs to ensure that scripts do not monopolize resources. Governors are responsible for enforcing these limits and tracking the resource statistics as set by Salesforce. Unlike the API, these limits are transaction-based, not time-based. If a script exceeds the limit, a runtime exception is thrown that cannot be handled. This error will be seen by the end user. There is currently no way to enlarge limits or turn of the governors for any org. Limits are different depending on the context of the code execution. The context is always decided by the entry point of the code.

November 17, 2009

Governor Limits

November 17, 2009

Agenda
What is Apex

Why Apex?
When To Use Apex Apex Framework

Traditional Code v/s Apex code


Invoking Apex & Tools for writing Apex Apex Script Example

Dos & Donts in Apex


Governor Limits in Apex

Deploying Apex Scripts

November 17, 2009

Deploying Apex Scripts


Code development done in Developer/Sandbox Org needs to be deployed to the Production Org. Mandatory requirements for deploying Apex code, 1. Apex testmethods to check logical flow of code. 2. Code coverage of Apex class/trigger should be above 75%. Two ways to deploy 1. Using Salesforce Ant tool 2. Using Eclipse IDE. 3. Change Set.

November 17, 2009

Thank You

November 17, 2009

Você também pode gostar