8.Apex Concepts v1.0

34
Apex Concepts November 17, 2009TCS Confidential

Transcript of 8.Apex Concepts v1.0

Page 1: 8.Apex Concepts v1.0

Apex Concepts

November 17, 2009TCS Confidential

Page 2: 8.Apex Concepts v1.0

November 17, 2009

Agenda

When To Use Apex

Apex Framework

Traditional Code v/s Apex code

Invoking Apex & Tools for writing Apex

Apex

Do’s & Don’t’s in Apex

Governor Limits in Apex

Deploying Apex Scripts

Why Apex?

What is Apex

Page 3: 8.Apex Concepts v1.0

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.

Page 4: 8.Apex Concepts v1.0

November 17, 2009

Agenda

When To Use Apex

Apex Framework

Traditional Code v/s Apex code

Invoking Apex & Tools for writing Apex

Apex

Do’s & Don’t’s in Apex

Governor Limits in Apex

Deploying Apex Scripts

What is Apex

Why Apex?

Page 5: 8.Apex Concepts v1.0

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.

Page 6: 8.Apex Concepts v1.0

November 17, 2009

Agenda What is Apex

Apex Framework

Traditional Code v/s Apex code

Invoking Apex & Tools for writing Apex

Apex

Do’s & Don’t’s in Apex

Governor Limits in Apex

Deploying Apex Scripts

Why Apex?

When To Use Apex

Page 7: 8.Apex Concepts v1.0

November 17, 2009

When To Use ApexUse 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.

Page 8: 8.Apex Concepts v1.0

November 17, 2009

Agenda

When To Use Apex

What is Apex

Traditional Code v/s Apex code

Invoking Apex & Tools for writing Apex

Apex

Do’s & Don’t’s in Apex

Governor Limits in Apex

Deploying Apex Scripts

Why Apex?

Apex Framework

Page 9: 8.Apex Concepts v1.0

November 17, 2009

Apex Framework

Page 10: 8.Apex Concepts v1.0

November 17, 2009

•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 Framework

Page 11: 8.Apex Concepts v1.0

November 17, 2009

• 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 Capabilities

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

Page 12: 8.Apex Concepts v1.0

November 17, 2009

Agenda

When To Use Apex

Apex Framework

What is Apex

Invoking Apex & Tools for writing Apex

Apex

Do’s & Don’t’s in Apex

Governor Limits in Apex

Deploying Apex Scripts

Why Apex?

Traditional code v/s Apex code

Page 13: 8.Apex Concepts v1.0

November 17, 2009

Traditional code v/s Apex code

Page 14: 8.Apex Concepts v1.0

November 17, 2009

Multi-Tenant Data and Code

Multi-Tenant Database Multi-Tenant Code

Shared query engine

(Apex DB)

Unique Schema and data Unique

code and logic

Shared code

execution(Apex VM)

Page 15: 8.Apex Concepts v1.0

November 17, 2009

Agenda

When To Use Apex

Apex Framework

Traditional Code v/s Apex code

What is Apex

Apex

Do’s & Don’t’s in Apex

Governor Limits in Apex

Deploying Apex Scripts

Why Apex?

Invoking Apex & Tools for writing Apex

Page 16: 8.Apex Concepts v1.0

November 17, 2009

Tools for Writing Apex•Salesforce.com User Interface.

Page 17: 8.Apex Concepts v1.0

November 17, 2009

Invoking Apex ExecutionApex 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.

Page 18: 8.Apex Concepts v1.0

November 17, 2009

Agenda

When To Use Apex

Apex Framework

Traditional Code v/s Apex code

Invoking Apex & Tools for writing Apex

What is Apex

Do’s & Don’t’s in Apex

Governor Limits in Apex

Deploying Apex Scripts

Why Apex?

Apex

Page 19: 8.Apex Concepts v1.0

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 modifierOptional definition modifiers (virtual, abstract, etc.)The keyword classThe unique nameOptional extensions or implementations

private | public | global

[virtual | abstract | with sharing | without sharing | (none) ]

class ClassName [implements InterfaceNameList | (none) ] [extends

ClassOrInterfaceName | (none) ]

{

// Class Body

}

Page 20: 8.Apex Concepts v1.0

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

Page 21: 8.Apex Concepts v1.0

November 17, 2009

Data TypesAll 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).

Page 22: 8.Apex Concepts v1.0

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];

Page 23: 8.Apex Concepts v1.0

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.

Page 24: 8.Apex Concepts v1.0

November 17, 2009

Apex Script Example

Page 25: 8.Apex Concepts v1.0

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

Page 26: 8.Apex Concepts v1.0

November 17, 2009

Agenda

When To Use Apex

Apex Framework

Traditional Code v/s Apex code

Invoking Apex & Tools for writing Apex

Apex Script Example

What is Apex

Governor Limits in Apex

Deploying Apex Scripts

Why Apex?

Do’s and Don’t’s in Apex

Page 27: 8.Apex Concepts v1.0

November 17, 2009

Do’s & Don’t’s

Do’s•Handle dynamic bulk processing•Use Set,Map,List objects•Query database using Set/Map variables

Don’t’s•Hardcoding of Ids and values•Extensive use of traditional arrays•Query database inside loops

Page 28: 8.Apex Concepts v1.0

November 17, 2009

Do’s•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.

Don’t’s•Scattering of code in triggers resulting in difficulty to understand the business logic in the Org.

•Rely on existing database records in testmethods.

Page 29: 8.Apex Concepts v1.0

November 17, 2009

Agenda

When To Use Apex

Apex Framework

Traditional Code v/s Apex code

Invoking Apex & Tools for writing Apex

Apex Script Example

Do’s & Don’t’s in Apex

What is Apex

Deploying Apex Scripts

Why Apex?

Governor Limits in Apex

Page 30: 8.Apex Concepts v1.0

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.

Page 31: 8.Apex Concepts v1.0

November 17, 2009

Governor Limits

Page 32: 8.Apex Concepts v1.0

November 17, 2009

Agenda

When To Use Apex

Apex Framework

Traditional Code v/s Apex code

Invoking Apex & Tools for writing Apex

Apex Script Example

Do’s & Don’t’s in Apex

Governor Limits in Apex

What is Apex

Why Apex?

Deploying Apex Scripts

Page 33: 8.Apex Concepts v1.0

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 tool2. Using Eclipse IDE.

3. Change Set.

Page 34: 8.Apex Concepts v1.0

November 17, 2009

Thank You