Anda di halaman 1dari 18

Getting Started with SalesForce CRM

Apex Code fundamentals Guide Part I

Description:
BISP is committed to provide BEST learning material to the beginners and advance learners. In the same series, we have prepared a complete end-to end Hands-on Beginners G ide !or Sales"orce. The doc ment !oc ses on writing #pe$ code. %oin o r pro!essional training program and learn !rom e$perts.

History:
Version Description Change 0.1 Initial Draft 0.1 &e'ie()1 Author Chandra Prakash har!a A!it har!a Publish Date "th #uly $01% "th #uly $01%

www.bispsolutions.com

www.bisptrainigs.com

www.hyperionguru.com

Page 1

Contents
Apex classes in SalesForce..............................................................................................................3 How to Add an Apex Class:............................................................................................................ 3 Apex Triggers in SalesForce..............................................................................................................5 Triggers:..................................................................................................................................... 5 How to write Trigger in SalesForce.com :...................................................................................6 How Do Vis alForce !ages Compare to S"Controls#........................................................................$ Creating a Vis alForce Component...................................................................................................$ %&at are C stom Components :.................................................................................................$ 'sing C stom Components in a Vis alForce !age:...................................................................$ How to De(ining C stom Components :....................................................................................1) %&at is Vis alForce..................................................................................................................... 1) %&* se Vis alForce :.............................................................................................................. 11 How To Create Vis alForce !age ...........................................................................................11 ................................................................................................................................................ 13 'sing t&e Vis alForce Component +i,rar* .....................................................................................13 -.erriding an /xisting !age wit& a Vis alForce !age.....................................................................10 1edirecting to a Standard -,2ect +ist !age.....................................................................................16 'sing 3np t Components in a !age.................................................................................................14

www.bispsolutions.com

www.bisptrainigs.com

www.hyperionguru.com

Page 5

Apex classes in SalesForce


3n SalesForce Force.com Apex Code is an o,2ect oriented programming lang age t&at allows de.elopers to de.elop on"demand , siness applications on t&e Force.com plat(orm. Define a class, specify the following : 1. Access modi(iers : 6o can se access modi(iers p ,lic or glo,al in t&e declaration o( a top"le.el class. Do not se an access modi(ier in t&e declaration o( an inner class. 5. -ptional de(inition modi(iers 7s c& as .irt al8 a,stract8 and so on9 3. 1e: ired : T&e ;e*word class (ollowed ,* t&e name o( t&e class 0. -ptional extensions: Following s*ntax (or de(ining classes: pri.ate < p ,lic < glo,al =.irt al < a,stract < wit& s&aring < wit&o t s&aring < 7none9> class Class?ame =implements 3nter(ace?ame+ist < 7none9> =extends Class?ame < 7none9> @ AABod* o( t&e class C

ow to Add an Apex Class:


Setup D De!eloper D Apex classes8 t&ere is man* option. *o can see ,elow. De!eloper Console : 3n Sales(orce.com de.eloper Console is an integrated de.elopment en.ironment wit& a collection o( tools *o can se to create8 de, g8 and test applications in *o r SalesForce organiEation. "ew : Clic; on ?ew , tton open new page on t&is page *o can write Apex code &ere. Generate From #SD$ : To access t&e Force.com %e, ser.ice8 *o need a %e, Ser.ice Description +ang age 7%SD+9 (ile. T&e %SD+ (ile de(ines t&e %e, ser.ice t&at is a.aila,le to *o . 6o r de.elopment plat(orm ses t&is %SD+ to generate an A!3 to access t&e Force.com %e, ser.ice it de(ines. %un All &ests : Clic; on 1 n All tests , tton (or r n all (iles. Schedule Apex : *o can sc&ed le Apex code ,* sing Schedule Apex , tton.

www.bispsolutions.com

www.bisptrainigs.com

www.hyperionguru.com

Page 3

For create new Apex code clic; on ?ew , tton . Clic; on "ew , tton8 A(ter t&at open new we, page on t&is page Apex Code editor is a.aila,le. *o can see ,elow. %rite &ere Apex Class a(ter t&en clic; on Sa!e , tton.

www.bispsolutions.com

www.bisptrainigs.com

www.hyperionguru.com

Page 0

Schedule Apex : Sc&ed le an Apex class t&at implements t&e FSc&ed la,leF inter(ace to ,e
a tomaticall* exec ted on a wee;l* or mont&l* inter.al. Clic; on Schedule Apex , tton.

Apex &riggers in SalesForce


&riggers:
Apex can ,e in.o;ed t&ro g& t&e se o( triggers. A trigger is exec tes ,e(ore or a(ter t&e (ollowing t*pes o( operations: insert pdate delete psert merge ndelete &riggers can 'e di!ided into two types : Be(ore triggers can ,e sed to pdate or .alidate record .al es ,e(ore t&e* are sa.ed to t&e data,ase. Triggers can ,e sed to access (ield .al es t&at are set ,* t&e data,ase8 For /xample recordFs 3d or last'pdated (ield8 and to a((ect c&anges in ot&er records8 s c& as logging into an a dit ta,le or (iring as*nc&rono s e.ents wit& a : e e.

www.bispsolutions.com

www.bisptrainigs.com

www.hyperionguru.com

Page 5

&rigger Context (aria'les : (aria'le is3nsert is'pdate is/xec ting isDelete isBe(ore isA(ter is'ndelete )sage 1et rns tr e i( t&is trigger was (ired d e to an insert operation8 (rom t&e SalesForces ser inter(ace8 Apex8 or t&e A!3. 1et rns tr e i( t&is trigger was (ired d e to an pdate operation8 (rom t&e SalesForce ser inter(ace8 Apex8 or t&e A!3. 1et rns tr e i( t&e c rrent context (or t&e Apex code is a trigger8 not a Vis alForce page8 a %e, ser.ice8 or an exec teanon*mo s79 A!3 call. 1et rns tr e i( t&is trigger was (ired d e to an delete operation8 (rom t&e SalesForce ser inter(ace8 Apex8 or t&e A!3. 1et rns tr e i( t&is trigger was (ired ,e(ore an* record was sa.ed. 1et rns tr e i( t&is trigger was (ired a(ter all records were sa.ed. 1et rns tr e i( t&is trigger was (ired a(ter a record is reco.ered (rom t&e 1ec*cle Bin 7t&at is8 a(ter an ndelete operation (rom t&e SalesForce ser inter(ace8 Apex8 or t&e A!3.9 1et rns a list o( t&e new .ersions o( t&e s-,2ect records. A map o( 3Ds to t&e new .ersions o( t&e s-,2ect records. 1et rns a list o( t&e old .ersions o( t&e s-,2ect records. A map o( 3Ds to t&e old .ersions o( t&e s-,2ect records. T&e total n m,er o( records in a trigger in.ocation8 ,ot& old and new.

new newGap old oldGap siEe

ow to write &rigger in SalesForce*com :


Setup D De!elop D Apex &riggers8 Clic; on De!eloper Console , tton. open new we, page. 6o can see ,elow. For create Apex Trigger clic; on File D "ew D Apex &rigger.

A(ter t&at open pop p window in t&is window8 %rite Trigger name and select -,2ect class 7w&ic& *o need to ,ind wit& trigger9 t&en clic; on s ,mit , tton. T&en *o can see new page (or write new trigger.

www.bispsolutions.com

www.bisptrainigs.com

www.hyperionguru.com

Page 6

A(ter %rite trigger 8 go to File men and clic; on Sa!e see ,elow . also *o se C&%$ + S ,* sing ;e*,oard. "ote : For nderstanding Apex Classes or Apex Trigger *o can see &ere. /xample 8 see ,elow .

,xample : %rite apex code (or a tomaticall* increasing 5H on t&e s ,mit (ees (or e.er* st dent. Step - : Create an* c stom o,2ect ta,. For /x : College *o can see ,elow.

www.bispsolutions.com

www.bisptrainigs.com

www.hyperionguru.com

Page 4

Step . : A(ter t&en go to adding Apex code8 Setup D De!eloper D Apex classes, clic; on "ew , tton. t&en write &ere Apex code.

Step / : A(ter write Apex classes 8 %rite Apex Trigger Clic; on Apex &rigger (or write Apex Trigger.

Step 0 : A(ter t&at Sa.e (ees8 t&en see (ees increase 5H .

www.bispsolutions.com

www.bisptrainigs.com

www.hyperionguru.com

Page I

ow Do (isualForce Pages Compare to S1Controls2


S"Controls8 is older Force.com tec&nolog* t&at &as now ,een s perseded ,* Vis alForce8 still ( nction on Force.com. 6o can contin e to se existing S"Controls8 , t restrictions are ,eing placed on creating new ones.

Creating a (isualForce Component


Sales(orce.com pro.ides a li,rar* o( standard8 pre", ilt components8 s c& as Japex:page renderAsKLpd(LD8 Japex:ta,!anel D and Japex:dataTa,leD8 t&at can ,e sed to de.elop Vis alForce pages. 3n addition8 *o can , ild *o r own c stom components to a gment t&is li,rar*.

#hat are Custom Components :


3n c stom components *o can re se t&at met&od se.eral times in a program8 *o can encaps late a common design pattern in a c stom component and t&en re se t&at component se.eral times in one or more Vis alForce pages. ,xample8 3( *o want to create a /mplo*ee list sing Vis alForce pages. /ac& /mplo*ee record in t&e list &as its own ,order. 1at&er t&an repeating t&e Vis alForce mar; p re: ired (or displa*ing e.er* /mplo*ee record in t&e list. -nce de(ined8 standard components s c& as Japex:related+istD or Japex:dataTa,leD .

)sing Custom Components in a (isualForce Page:


Create a new c stom component named recordDispla*. Japex:page D Japex:pageBloc; titleKL/mplo*ee +istL D J,D %elcome :JA,D @MN'ser.First?ameC Japex:detail AD JAapex:pageBloc;D www.bispsolutions.com www.bisptrainigs.com www.hyperionguru.com Page $

JAapex:pageD

ow to Defining Custom Components :


Setup D De!elop D Components8 Clic; on "ew , tton.

Step - : /nter +a,el ?ame t&en write Vis alForce Component a(ter t&en clic; on Sa!e , tton.

Step . : A(ter t&at create new Vis alForce page and write &ere some cod *o can see ,elow.

#hat is (isualForce
Vis alForce is (ramewor; t&at allows de.elopers to , ild re(ined8 c stom ser inter(aces t&at can ,e &osted nati.el* on t&e Force.com plat(orm. T&e Vis alForce (ramewor; incl des a tag",ased mar; p lang age8 similar to HTG+. 3n Vis alForce page *o can se &tml code8 Oa.aScript8 CSS8 A2ax8 Fles&. www.bispsolutions.com www.bisptrainigs.com www.hyperionguru.com Page 1)

De.elopers can se Vis alForce to create a Vis alForce page de(inition. A page de(inition consists o( two primar* elements: " Vis alForce mar; p. " A Vis alForce controller. (isualForce 3ar4up :1 Vis alForce mar; p consists o( Vis alForce tags8 HTG+8 Oa.aScript8 or an* ot&er %e,"ena,led code em,edded wit&in a single <apex: page> tag. T&e mar; p de(ines t&e ser inter(ace components t&at s&o ld ,e incl ded on t&e page8 and t&e wa* t&e* s&o ld appear. (isualForce Control :1 A Vis alForce controller is a set o( instr ctions t&at speci(* w&at &appens w&en a ser interacts wit& t&e components speci(ied in associated Vis alForce mar; p8 s c& as w&en a ser clic;s a , tton or lin;.

#hy use (isualForce :


" To create c stom inter(aces. " To , ild wiEards. " To de(ine c stom na.igation patterns.

ow &o Create (isualForce Page


www.bispsolutions.com www.bisptrainigs.com www.hyperionguru.com Page 11

Clic; on Setup D de!elop D Pages T&en Clic; on ?ew , tton.

Step - :

A(ter Sa.ing t&is page8 clic; on t&is icon to .iew Home page.

3( *o are getting error (or .iew page8 sol.e t&is pro,lem ,* (ollowing some steps.Pi.en ,elow. Clic; on Setup D 3anage )sers D )sers t&en clic; on /dit lin; , tton (or edit ser settings.

www.bispsolutions.com

www.bisptrainigs.com

www.hyperionguru.com

Page 15

T&en *o can (ind De.elopment Gode and c&ec;ed c&ec; ,ox t&en clic; on sa.e , tton. A(ter t&en *o can clic; on .iew icon t&at time8 t&ere is Vis alForce to as; *o to create new page ,eca se t&is page is open in de.elopment mode.

Second type of create (isualForce page : go to we, ,rowser t*pe '1 +8 *o can see ,elow. /x : Http:AA SalesForce lin; a(ter loginAapexA!age ?ame

)sing the (isualForce Component $i'rary


Vis alForce Japex:pageDJAapex:pageD tag is se mandator*8 tag m st ,e placed at start and end o( all Vis alForce mar; p. and also Vis alForce s pports all &tml tag. For example : Jimg srcKL LD8 Jta,leDJtrDJtdDJAtdDJAta,leD8 Jmar: eeDJAmar: eeD etc. ow to use (isualForce Component $i'rary : O st go to create new page. For example 3 will create /xample5 . 6o can see ,elow.

www.bispsolutions.com

www.bisptrainigs.com

www.hyperionguru.com

Page 13

%&en *o clic; on Component 1e(erence lin; *o can see new we, page. -n t&is we, page all Component wit& s*ntax are a.aila,le8 w&ic& Component *o want to add O st write in code window. see ,elow. And also see &ow to se Iframe in (isualForce page .

5!erriding an ,xisting Page with a (isualForce Page


3( *o need to c&ange t&e (ormat o( an existing page8 s c& as t&e standard acco nt detail page. All t&e in(ormation displa*s on a single page. 3( t&ereFs a lot o( in(ormation8 *o mig&t end p doing a lot o( scrolling. 'sing Vis alForce in a page *o can ma;e eac& section7Ta,9 (or an acco nt displa* in a ta,. 1. Firstl*8 Create a new Vis alForce page sing t&e : ic; (ix. AapexA!age?ame For /xample %e create t&is page &ttps:AAc.ap1..is al.(orce.comAapexA-.errding www.bispsolutions.com www.bisptrainigs.com www.hyperionguru.com Page 10

5. Create -.erriding page. 3. Delete /xisting code. and paste it ,elow code. 0. See ,elow code.

www.bispsolutions.com

www.bisptrainigs.com

www.hyperionguru.com

Page 15

%edirecting to a Standard 5'6ect $ist Page


3n Sales(orce.com8 , ttons or lin;s t&at na.igate a ser to a standard ta,8 *o can redirect t&e content to present a list o( standard o,2ects. Create a Vis alForce page wit& t&e (ollowing mar; p : Following code ses command , tton8 see ,elow.

www.bispsolutions.com

www.bisptrainigs.com

www.hyperionguru.com

Page 16

A(ter Clic; on t&is , tton *o can see page redirect Acco nt page. *o can see ,elow.

)sing Input Components in a Page


3n Vis alForce *o can se t&e Japex:(ormD tag wit& one or more inp t components and a Japex:command+in;D or Japex:commandB ttonD tag to s ,mit t&e (orm. &he input component tag : Japex:inp tC&ec;,oxD Japex:inp tFieldD Japex:inp tSecretD Japex:inp tTextD Japex:inp tTextareaD Japex:o tp tFieldD Japex:o tp tTextD Japex:selectC&ec;,oxesD Japex:select+istD Japex:select1adioD

www.bispsolutions.com

www.bisptrainigs.com

www.hyperionguru.com

Page 14

www.bispsolutions.com

www.bisptrainigs.com

www.hyperionguru.com

Page 1I

Anda mungkin juga menyukai