Вы находитесь на странице: 1из 19

Introduction to Apex

for Non-Developers
Exercise Guide

Copyright 2014 salesforce.com, inc. All rights reserved.


Introduction to Apex for Non-Developers Exercise Guide Page 1 of 18

Prerequisites
Goal:
Log in to your Salesforce environment and download the required HOT session files.
Tasks:
1. Log into your unique Salesforce environment.
2. Download and unzip the required files.
Time:
5 minutes

Instructions:
1. Log into your unique Salesforce environment.
A. In the Web browser address bar, enter https://login.salesforce.com
B. Enter the username and password provided by the instructor and click Login.
2. Download and unzip the required files.
A. Click the Documents tab.
B. In the Document Folders section, select My Personal Documents from the picklist and
then click Go.
C. Click View to download the DF12 Introduction to Apex for Non-Developers Lab
Files zip file.
D. Save the file to the local C: drive.
E. Use Windows Explorer to extract the files to the local C: drive.

Copyright 2014 salesforce.com, inc. All rights reserved.


Introduction to Apex for Non-Developers Exercise Guide Page 2 of 18

Exercise 1: Create a New Force.com IDE Project


Goal:
Create a Force.com project using the Force.com IDE.
Tasks:
1. Launch Eclipse.
2. Enter the information for a new Force.com Project.
3. Download the metadata components of the org into the new project.
Time:
10 minutes

Instructions:
1. Launch Eclipse.
A. Double-click the Eclipse shortcut icon on the Desktop.
B. On the Eclipse menu bar, select Window | Open Perspective | Other | Force.com and then
click OK.
C. Navigate to File | New | Force.com Project.

2.

Enter the information for a new Force.com Project.


A.
B.
C.
D.
E.
F.

Project Name: (Enter your username.)


Username: (Enter your username.)
Password: (Enter your password.)
Security Token: (Leave blank; IP Ranges are open.)
Environment: Production/Development Edition
Click Next.

Copyright 2014 salesforce.com, inc. All rights reserved.


Introduction to Apex for Non-Developers Exercise Guide Page 3 of 18

3. Download the metadata components of the org into the new project.
A. Accept the default settings to download Apex and Visualforce metadata components.
B. Click Finish.

Copyright 2014 salesforce.com, inc. All rights reserved.


Introduction to Apex for Non-Developers Exercise Guide Page 4 of 18

Exercise 2: Execute Anonymous Blocks


Goal:
Gain an understanding of functionality through Force.com IDE anonymous blocks.
Tasks:
1. Navigate to the Execute Anonymous window in the Force.com IDE.
2. Try out your own Execute Anonymous code.
Time:
5 minutes

Instructions:
1. Navigate to the Execute Anonymous window in the Force.com IDE.
A. Open the Force.com IDE.
B. At the bottom of Eclipse there will be a tab labeled Execute Anonymous. Double-click it to
expand or contract it
C. Copy and paste the contents of the 2-1. Execute Anonymous.txt from the Exercises
folder into the Execute Anonymous window.
string tempvar = 'Enter_your_name_here';
System.debug('Hello World!');
System.debug('My name is ' + tempvar);
D. Replace Enter_your_name_here with your name.
E. Click Execute Anonymous to run the code in the Source to Execute window.

Copyright 2014 salesforce.com, inc. All rights reserved.


Introduction to Apex for Non-Developers Exercise Guide Page 5 of 18

Exercise 3: Create a BankAcct and CreateContactFromCan Class


Goal:
Understand how to create a class with attributes and methods.
Tasks:
1. Create a class named BankAcct, and give it a balance and acctName attribute as well as a
makeDeposit method to set the balance attribute value.
2. Create a class named CreateContactFromCan with a method named createContact.
Time:
10 minutes

Instructions:
1. Create a class named BankAcct, and give it a balance and acctName attribute as well as a
makeDeposit method to set the balance attribute value.
A. In the Force.com IDE, in the Package Explorer window on the left-hand side, right-click your
project folder and select New | Apex Class. (Note: You may need to double-click the
Execute Anonymous tab to restore it to its smaller size.)
i. Name: BankAcct
ii. Click Finish.
B. Copy the code from the 3-1. Create a BankAcct Class.txt file in the Exercises
folder.
C. Paste it into Eclipse (replacing any default text).
D. Complete the TODO sections as required.
//TODO: Declare a private integer attribute called balance
initialize to zero
private integer balance=0;
//TODO: Declare a public string attribute called acctName
public string acctName;
//TODO: Declare a method named makeDeposit that does not return
anything and one input parameter of type integer called deposit
public void makeDeposit (integer deposit){
//TODO: Add the deposit input parameter to the balance attribute
balance = balance + deposit;
}
E. Save the class by clicking the floppy disk icon in the upper-left of the window.
F. Check the Problems tab in Eclipse to see if you have any errors in your class that prevented
the class from saving to the server.
2. Create a class named CreateContactFromCan with a method named createContact.
Copyright 2014 salesforce.com, inc. All rights reserved.
Introduction to Apex for Non-Developers Exercise Guide Page 6 of 18

A. In the Force.com IDE, in the Package Explorer window on the left-hand side, right-click your
project folder and select New | Apex Class. (Note: You may need to double-click the
Execute Anonymous tab to restore it to its smaller size.)
i. Name: CreateContactFromCan
ii. Click Finish.
B. Copy the code from the 3-2. Create a CreateContactFromCan.txt file in the
Exercises folder.
C. Paste it into Eclipse (replacing any default text).
D. Complete the TODO sections as required.
//TODO: Declare a method that does not return anything called
createContact
public void createContact (){
}
E. Save the class by clicking the floppy disk icon in the upper-left of the window.
F. Check the Problems tab in Eclipse to see if you have any errors in your class that prevented
the class from saving to the server.

Copyright 2014 salesforce.com, inc. All rights reserved.


Introduction to Apex for Non-Developers Exercise Guide Page 7 of 18

Exercise 4: Instantiate an Object and Invoke a Method from Execute Anonymous


Goal:
Understand how to instantiate an object from a class, assign values to the attributes of the object,
and invoke a method.
Task:
Instantiate an object called chkAcct from the BankAcct class created in the previous exercise, set the
attributes of the chkAcct object, and invoke the makeDeposit method.
Time:
10 minutes

Instructions:
1. Instantiate an object called chkAcct from the BankAcct class created in the previous exercise, set
the attributes of the chkAcct object, and invoke the makeDeposit method.
A. Click the Execute Anonymous tab.
B. Copy the code from the 4-1. Instantiate Object_Execute Anonymous.txt file in
the Exercises folder.
C. Paste it into the Source to Execute window (replacing any default text).
D. Complete the TODO sections as required.
//TODO: Instantiate an object from the BankAcct class called chkAcct
BankAcct chkAcct = new BankAcct();
//TODO: Set the attribute accttype of the object chkAcct to Checking
chkAcct.accttype = 'Checking';
//TODO: Set the attribute acctName of the object chkAcct to
D.Castillo-Chk
chkAcct.acctName = 'D.Castillo-Chk';
//TODO: Invoke the makeDeposit method for $150
chkAcct.makeDeposit(150);
E. Click Execute Anonymous.
F. Review the output in the Results window.

Copyright 2014 salesforce.com, inc. All rights reserved.


Introduction to Apex for Non-Developers Exercise Guide Page 8 of 18

Exercise 5: Create a List Data Structure


Goal:
Gain an understanding of the List data structure.
Tasks:
1. Instantiate a list called bankAccts and add the two objects to the list.
2. Modify the class named CreateContactFromCan created in Exercise 3 by adding the
instantiation of a Contact list object the createContact method.
Time:
10 minutes

Instructions:
1. Instantiate a list called bankAccts and add the two objects to the list.
A. Click the Execute Anonymous tab.
B. Copy the code from the 5-1. Create a List_Execute Anonymous.txt file in the
Exercises folder.
C. Paste it into the Source to Execute window (replacing any default text).
D. Complete the TODO sections as required.
//TODO: Instantiate a BankAcct List object from the List class
called bankAccts
List <BankAcct> bankAccts = new List<BankAcct>();
//TODO: Add the chkAcct object to the bankAccts list
bankAccts.add(chkAcct);
//TODO: Add the savAcct object to the bankAccts list
bankAccts.add(savAcct);
E. Click Execute Anonymous.
F. Review the output in the Results window.
2. Modify the class named CreateContactFromCan created in Exercise 3 by adding the
instantiation of a Contact list object the createContact method.
A. In the Force.com IDE, in the Package Explorer window on the left-hand side, expand your
project folder, expand the src folder, expand your classes folder, and open your
CreateContactFromCan class (if not already open) by double-clicking on the
CreateContactFromCan.cls file.
B. Copy the code from the 5-2. Create a List in CreateContactFromCan
Class.txt file in the Exercises folder.
C. Paste it into Eclipse (replacing any previous text).
D. Complete the TODO sections as required.

Copyright 2014 salesforce.com, inc. All rights reserved.


Introduction to Apex for Non-Developers Exercise Guide Page 9 of 18

//TODO: Instantiate a Contact list object from the List class


List<Contact> conList = new List<Contact>();
E. Save the class by clicking the floppy disk icon in the upper-left of the window.
F. Check the Problems tab in Eclipse to see if you have any errors in your class that prevented
the class from saving to the server.
G. Notice how the createContact method has been modified to take in an input parameter called
candsFromTrigger that is a Candidate list object.

Copyright 2014 salesforce.com, inc. All rights reserved.


Introduction to Apex for Non-Developers Exercise Guide Page 10 of 18

Exercise 6: Create a List FOR Loop Construct


Goal:
Gain an understanding of how to create a List FOR loop
Tasks:
1. Add a List FOR loop to iterate through the bankAccts list created in the previous exercise.
2. Add a List FOR loop to iterate over a Candidate list in the createContact method.
Time:
10 minutes

Instructions:
1. Add a List FOR loop to iterate through the bankAccts list created in previous exercise.
A. Click the Execute Anonymous tab.
B. Copy the code from the 6-1. Create a List Loop_Execute Anonymous.txt file in
the Exercises folder.
C. Paste it into the Source to Execute window (replacing any existing text).
D. Complete the TODO sections as required.
//TODO: Declare a For List loop to loop through the list bankAccts
with an iterationvariable called tempacct
For (BankAcct tempacct:bankAccts)
E. Click Execute Anonymous.
F. Review the output in the Results window.
2. Add a List FOR loop to iterate over a Candidate list in the createContact method.
A. In the Force.com IDE, in the Package Explorer window on the left-hand side, expand your
project folder, expand your classes folder, and open your CreateContactFromCan class
(if not already opened) by double-clicking on the CreateContactFromCan.cls file.
B. Copy the code from the 6-2. Create a List For Loop in CreateContactFromCan
Class.txt file in the Exercises folder.
C. Paste it into Eclipse (replacing any previous text).
D. Complete the TODO sections as required.
//TODO: Declare a For list loop to loop through the input parameter
list candsFromTrigger with an iterationvariable called
currentCandidate
for(Candidate__c currentCandidate:candsFromTrigger)
E. Save the class by clicking the floppy disk icon in the upper-left of the window.

Copyright 2014 salesforce.com, inc. All rights reserved.


Introduction to Apex for Non-Developers Exercise Guide Page 11 of 18

F. Check the Problems tab in Eclipse to see if you have any errors in your class that prevented
the class from saving to the server.

Copyright 2014 salesforce.com, inc. All rights reserved.


Introduction to Apex for Non-Developers Exercise Guide Page 12 of 18

Exercise 7: Reference sObjects in Apex and Use Data Manipulation Language (DML) to
Persist sObjects to the Database
Goal:
Gain an understanding of how to reference sObjects in Apex and use DML.
Tasks:
1. Create a Recruiting Account sObject in memory and persist it to the database.
2. Create a Contact sObject, populate the attributes, add the sObject to the conList, and persist the
list to the database within the createContact method.
Time:
10 minutes

Instructions:
1. Create a Recruiting Account sObject in memory and persist it to the database.
A. Click the Execute Anonymous tab.
B. Copy the code from the 7-1. Insert record into DB_Execute Anonymous.txt file
in the Exercises folder.
C. Paste it into the Source to Execute window (replacing any existing text).
D. Complete the TODO sections as required.
//TODO: Instantiate an object called acct from the sObject Account
class
Account acct = new Account();
//TODO: Set the attribute name of the acct object to Recruiting
acct.name='Recruiting';
//TODO: Persist the acct object to the DB using DML
Database.insert(acct);
E. Click Execute Anonymous.
F. Review the output in the Results window.
2. Create a Contact sObject, populate the attributes, add the sObject to the conList, and persist the
list to the database within the createContact method.
A. In the Force.com IDE, in the Package Explorer window on the left-hand side, expand your
project folder, expand your classes folder, and open your CreateContactFromCan class
(if not already opened) by double-clicking on the CreateContactFromCan.cls file.
B. Copy the code from the 7-2. Create Contact object and DML in
CreateContactFromCan class.txt file in the Exercises folder.
C. Paste it into Eclipse (replacing any previous text).
D. Complete the TODO sections as required.

Copyright 2014 salesforce.com, inc. All rights reserved.


Introduction to Apex for Non-Developers Exercise Guide Page 13 of 18

//TODO: Instantiate an object called con from the sObject class


contact
contact con = new Contact();
//TODO: Set the attribute Firstname of the con object to the values
of the First_Name__c attribute of the currentCandidate object
con.FirstName = currentCandidate.First_Name__c;
//TODO: Set the attribute Lastname of the con object to the values
of the Last_Name__c attribute of the currentCandidate object
con.LastName = currentCandidate.Last_Name__c;
//TODO: Set the attribute Email of the con object to the values of
the Email__c attribute of the currentCandidate object
con.Email = currentCandidate.Email__c;
//TODO: Add the con object to the conList
conList.add(con);
//TODO: Persist the conList to the database
Database.insert(conList);
E. Save the class by clicking the floppy disk icon in the upper-left of the window.
F. Check the Problems tab in Eclipse to see if you have any errors in your class that prevented
the class from saving to the server.

Copyright 2014 salesforce.com, inc. All rights reserved.


Introduction to Apex for Non-Developers Exercise Guide Page 14 of 18

Exercise 8: Create SOQL to Return Data to Apex


Goal:
Create a SOQL query using the schema explorer. Use the SOQL query created in the Schema
Explorer in Apex code.
Tasks:
1. Create a SOQL query in the Schema Explorer to retrieve the Recruiting Account record created
in the previous exercise.
2. Add the SOQL query to the createContact method to associate the newly created Contact to the
Recruiting Account.
Time:
10 minutes

Instructions:
1. Create a SOQL query in the Schema Explorer to retrieve the Recruiting Account record created
in the previous exercise.
A. In the Force.com IDE, in the Package Explorer window on the left-hand side, expand your
project folder, double click on the salesforce.schema
B. In the schema window on right-hand side, expand Account, then expand Fields.
C. Select the ID and Name fields.
D. Notice the SOQL statement created in the Query Results window. Click the Run Me button.
E. Notice the Recruiting Account in the results window.
F. Deselect the Name field.
G. Place cursor in the Query Results and add where a.Name = 'Recruiting'
H. Click the Run Me button.
2. Add the SOQL query to the createContact method to associate the newly created Contact to the
Recruiting Account.
A. In the Force.com IDE, in the Package Explorer window on the left-hand side, expand your
project folder, expand your classes folder, and open your CreateContactFromCan class
(if not already opened) by double-clicking on the CreateContactFromCan.cls file.
B. Copy the code from the 8-1. Create SOQL for CreateContactfrom Can
Class.txt file in the Exercises folder.
C. Paste it into Eclipse (replacing any previous text).
D. Complete the TODO sections as required.
//TODO: Select the Recruiting record from the database and assign to
an object called candAcct from the sObject Account class
Account candAcct = [SELECT a.Id
FROM Account a
WHERE a.Name = 'Recruiting'];

Copyright 2014 salesforce.com, inc. All rights reserved.


Introduction to Apex for Non-Developers Exercise Guide Page 15 of 18

//TODO: Set the attribute AccountID of the con object to the value
of the Id attribute of the candAcct object
con.AccountId = candAcct.Id;
E. Save the class by clicking the floppy disk icon in the upper-left of the window.
F. Check the Problems tab in Eclipse to see if you have any errors in your class that prevented
the class from saving to the server.

Copyright 2014 salesforce.com, inc. All rights reserved.


Introduction to Apex for Non-Developers Exercise Guide Page 16 of 18

Exercise 9: Create a Trigger


Goal:
Create and develop a trigger that invokes a method from a class.
Tasks:
1. Create the CreateContact trigger to invoke the createContact method.
2. Create a new candidate to test the trigger.
Time:
10 minutes

Instructions:
1. Create the CreateContact trigger to invoke the createContact method.
A. Right-click the project folder and select New | Apex Trigger.
B. Enter the following information:
i.
ii.
iii.
iv.

Name: CreateContact
Trigger Object: Candidate__c
Trigger Operations: after insert
Click Finish.

C. Copy the code from the 9-1. Create trigger on Candidate.txt file in the
Exercises folder.
D. Paste it into the CreateContact editor window (replacing any default text).
E. Complete the TODO sections as required.
//TODO: Instantiate an object called cc from the class
CreateContactFromCan
CreateContactFromCan cc = new CreateContactFromCan();
//TODO: Invoke the method createContact and send a List of
Candidates as an input parameter
cc.createContact(Trigger.new);
F. Save the trigger.
2. Create a new candidate to test the trigger.
A. In Salesforce, click the Candidates tab.
B. Click New.
C. Fill out the following fields:
i.
ii.
iii.
iv.

First Name: Nina


Last Name: Simone
Gender: Female
Street Address 1: 1234 F St
Copyright 2014 salesforce.com, inc. All rights reserved.
Introduction to Apex for Non-Developers Exercise Guide Page 17 of 18

v. City: Washington
vi. State: DC
vii. Zip: 20008
viii. Country: USA
D.
A.
B.
C.

Click Save.
Click the Contacts tab.
From the View picklist, select All User and click Go!
Verify that a contact was created.

Copyright 2014 salesforce.com, inc. All rights reserved.


Introduction to Apex for Non-Developers Exercise Guide Page 18 of 18