Professional Documents
Culture Documents
Agenda
What is Apex
Why Apex?
When To Use Apex Apex Framework
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.
Agenda
What is Apex Why Apex? When To Use Apex Apex Framework
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.
Agenda
What is Apex
Why Apex?
When To Use Apex Apex Framework
Agenda
What is Apex
Why Apex?
When To Use Apex Apex Framework
Apex Framework
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.
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
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
Multi-Tenant Database
Multi-Tenant Code
Agenda
What is Apex
Why Apex?
When To Use Apex Apex Framework
Agenda
What is Apex
Why Apex?
When To Use Apex Apex Framework
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 }
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
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).
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];
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.
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
Donts
Hardcoding of Ids and values Extensive use of traditional arrays Query database inside loops
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.
Agenda
What is Apex
Why Apex?
When To Use Apex Apex Framework
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.
Governor Limits
Agenda
What is Apex
Why Apex?
When To Use Apex Apex Framework
Thank You