Show Menu
Cheatography

Apex for interview and

This is a draft cheat sheet. It is a work in progress and is not finished yet.

Apex Applic­ation

 
Create Web Service by integr­ating other systems
Create email services for an email blast or Email Setup
Perform Complex Validation over multiple objects at the same time and also custom validation implem­ent­ation.
Create complex business processes that are not supported by existing workflow functi­onality or Flows
Create custom transa­ctional logic
Perform some Logic when a record is modified.

Working Structure of Apex
Flow of Action
Developer Action
End User Action

Unders­tanding the Apex Syntax

 
Variable Decalation
SOQL Query
Loop Statement
Flow Control Statement
DML Statement

Apex

What is Apex
Apex is a strongly typed, object­-or­iented Progra­mming language that allows developers to execute the flow and transa­ction control statements on the force.com platform server in conjun­ction with calls to the Froce.com API.

Features of Apex as a language
Integrated
Java-like syntax and easy to use
Strongly Integrated with Data
Strongly Typed
Multit­enant Environent
Upgrades Automa­tically
 

More String Examples

 
Pliblic Boolean equals­Ign­ore­Cas­e(S­tring string­toc­ompate)
String MyString1 = 'MySTR­ING';
String MyString2 = 'MyStr­ing';
Boolean result = myStri­ng2.eq­ual­sIg­ono­reC­ase­(my­Str­ing1);
Sytem.d­ebug(' your message :' +result);

String Examples

 
public Boolean equals­Ign­ore­Cas­e(S­tring string­toc­ompare)

String myString1 = 'MySTR­ING';
String myString2 = 'MyStr­ing';
Boolean result = myStri­ng2.eq­ual­sIg­nor­eCa­se(­myS­tri­ng1);
System.de­bug­('Value of equal will be true as they are same and result is : ' +result);

Apex - String

 
String in Apex: is any set of character with no character limit.
String compan­yName = 'ABC';

String Methods
Syntax
public Boolean contai­ns(­String substring)

Example
String myProd­uct­Name1 = 'HCL' ;
String myProd­uct­Name2 = 'NAHCL' ;
Boolean result = myProd­uct­Nam­e2.c­on­tai­ns(­myP­rod­uct­Name1);
System.de­bug­('O/P will be true as it contains the string and Output is :' +result) ;

Apex - Variables

 
Declaring Variables
String StrName = 'My String';
Integer myInterger = 1;
Boolean mtBoolean = true;

Apex Variables are case-I­nse­nsi­tive( you cannot declare a variable two times)
Integer m = 100;
For (Integer I = 0; I < 0; I++){
integer m = 1;
Scope of Variables
List<s­tri­ng> Products = new List<s­tri­ngs­>();
Produc­ts.a­dd­(HCL);
}

Primitive Data Types

 
Interger
Interger X = 1000;
System.de­bug(' value of x Variables: ' +x);

Boolean
Boolean x;
x = true;
System.de­bug(' ' +x);

Date
Date x = date,t­oday();

Long/S­tring
Long x = 231321­265­465­444­5643;

Object
Account x = new Account ( Name ='Test AC');

Unders­tanding the Data Types

 
Apex Supports the Followings data types
Primit­ive­(In­teger, Double, Long, Date, Datetime, String, ID, or Boolean)
Collec­tio­ns(­Lists, Sets and Maps)
sObject
Enums
Classes, Objects and Interfaces

Apex - Enviroment

 
Apex Code Develo­pment Tools
Force.com Developer Console
Froce.com IDE
Code Editor in the Salesforce use Interface
 

Apex - Arrays

 
Syntax
<St­rin­g> [ ] arrayO­fPr­oducts = new List<S­tri­ng>();
Ex:
String [ ] arrayo­fpr­oduts = new List <St­rin­g>();
//Adding elements in arrays
arrayO­fPr­odu­cts.ad­d('­HCL');
arrray­OfP­rod­uct­s.a­dd(­'H2­SO4');

For (Integer i=0; I<a­rra­yOf­Pro­duc­ts.s­ize( ); i++){
System.de­bug­('Value In Array :' +array­OfP­rod­ucts[ I ]);}

Accessing array element by using index

Apex - Constants

 
Constants are variables which donot change their values
Ex:

public class Custom­erO­per­ati­onC­lass{
Static final Double regula­rCu­tom­erD­iscount = 0.1;
Static Double finalPrice = 0;
public static Double provid­eDi­scount (Integer price){
// calculate the Discount
finalPrice = price - price * rebula­rCu­sto­mer­Dis­count;
return finalP­rice;
}
}
OUTPUT
Double finalPrice = Custom­erO­per­ati­onC­las­s.p­rov­ide­Dis­cou­nt(­100);
System.de­bug­('F­ina­lPrice :' +final­Price);

Apex - Decision Making

 
Decisi­on-­making structures require that the programmer specify one or more conditions to be evaluated.

Apex - Loops

 
Loop Type & Descri­ption
For loop : This loop performs a set of statements for each item In a set of records.
SOQL for Loop: Execute a sequence of statements directly over the returned set of SOQL query.
Java-Like for Loop : Execute a sequence of statements in tradit­ional Java-like syntax

Conti...

 
While loop: Repeats a statement or group of statements while a given condition is true. it tests the condition before executing the loop body.
do...while loop
Like a while statement, except that it tests the condition at the end of the loop body.
 

Apex - Collec­tions

 
Lists
Ex:
List<S­tri­ng> ListOf­Cities = new List<S­tri­ng>();
System.de­bug­('Value of ListOf­Cities :' +ListO­fCi­ties);

List <ac­cou­nt> Accoun­tTo­Delete = new List<a­cco­unt> ();
System.de­bug­('Value Of ListOf­Cities' +Accou­ntT­oDe­lete);

There is a limitation on heap size.

Methods For Lists:
size()
add()
get()
clear()
set()

Sets and Method for Sets

 
Set<st­rin­g> ProductSet = new Set<st­rin­g>{­'Ph­enol', 'Benzene', 'H2SO4'};
Prodcc­tSe­t.a­dd(­'HCL');
System.de­bug­('Set with New Value :' +Produ­ctSet);

Produc­tSe­t.r­emo­ve(­'HCL');
Produc­tSe­t.c­ont­ain­s('­HCL');

Maps

 
Map<st­ring, string> Produc­tCo­deT­oPr­odu­ctName = New Map<st­ring, string>
{'1000' =>'­HCL', '1001' => 'H2SO4'};