for Non-Developers
Exercise Guide
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.
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.
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.
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.
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.
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.
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.
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.
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.
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.
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'];
//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.
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.
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.