Professional Documents
Culture Documents
APEX
(APEX CALLOUTS)
Exercise Guide
EXERCISE GUIDE
Apex Callouts
Table of Contents
Exercise 3-1: Invoking Third-Party SOAP Services from Apex .................................................. 1
Exercise 3-2: Invoking Third-Party Services with HTTP Callouts............................................... 4
.
EXERCISE GUIDE
Apex Callouts
Instructions:
1. Import the Address Validation WSDL.
A. In the UI, navigate to Setup | Develop | Apex Classes.
B. Click Generate from WSDL.
C. Click Choose File.
D. Navigate to the Exercises/force.com folder and select the
10-2.AddressValidation.wsdl file.
E. Click Parse WSDL.
Apex class name: ValidateAddressesSOAP
going into an infinite loop. Notice where the flag gets set to true on line 44.
ii. Line 4: The method is marked as @future(callout=true)
iii. Line 9: Inside the validateAddressSOAP method, we declare the
listAddress variable. It is a list of elements of type
ValidateAddressesSOAP.addressSOAP. This type was defined by importing
the WSDL. addressSOAP is an inner class of ValidateAddressesSOAP
iv. Line 10: This for loop iterates over mapCands. This loop will build a list of
Candidate addresses in a form that the Heroku Web service can parse.
v. Line 35: This for loop iterates over the variable listResult. This loop will use
the result from calling the Heroku Web service to set the values on the
Candidate record.
4. Create the trigger that invokes the @future method.
A. In the Developer Console, click File | New | Apex Trigger.
i. Enter the following information:
Name: CandidateAddressValidation
sObject: Candidate__c
ii. Click Submit.
iii. Copy the code from the file named 10-2.CandidateAddressValidation.txt in the
Exercises folder, and paste it into the Apex Trigger tab, overwriting any existing
code.
iv. Complete the TODO for Exercise 10-2 at the bottom of the trigger.
B. Click Save.
5. Test the callout.
A. Test an invalid address: In the UI, open an existing candidate, and change the
address to your home address. You need to refresh your screen (F5) to see the
resulting validation error. You should receive a message that your address is invalid.
B. Test a valid address: Modify the candidate record using the only address that is
“valid” according to this simple Web service that is helping Universal Containers test
the platform’s capabilities to make callouts:
Street Address 1: One Market
Street Address 2: Suite 300
City: San Francisco
State/Province: CA
Zip/Postal Code: 94105
Country: USA
Instructions:
1. Create the ValidateAddressesREST class.
Note: In the previous lab, when you tested the SOAP-based Web service, you generated
a class from a supplied WSDL that contained the mapping of the contract to Apex
constructs. When you work with REST-based Web services, you need to manually create
the Apex constructs that map to the service. In this exercise, you will create a class
called ValidateAddressesREST that is the REST-based equivalent of the class
generated from the WSDL in the previous exercise.
A. Within your organization, navigate to Setup | Develop | Apex Classes.
B. Click New.
C. Copy the code from the file named 10-3.ValidateAddressesREST.txt in the Exercises
folder and paste it into the Apex tab, overwriting any existing code.
D. Complete the sections of the ValidateAddressesREST class marked with TODO.
E. Save the class.
2. Whitelist the Heroku endpoint using a Named Credential.
A. Within your org, navigate to Setup | Security Controls | Named Credentials.
B. Click New Named Credential.
Label: HerokuWebService
Name: HerokuWebService
URL: http://trg-mock-address-validator.herokuapp.com
C. Click Save.
3. Modify the class that contains the @future method.
A. Within your org, navigate to Setup | Develop | Apex Classes.
B. Click Edit next to the ValidateAddresses class.
C. Copy the code from the file named 10-3. ValidateAddressesWithREST.txt in the
Exercises folder, and paste it into the Apex Class tab, overwriting any existing code.
D. Complete the three TODOs for Exercise 10-3 in the ValidateAddresses class.
E. Click Save.
4. Invoke the @future method.
A. Within your org, navigate to Setup | Develop | Apex Triggers.
B. Click Edit next to the CandidateAddressValidation trigger.
i. Complete the TODOs for Exercise 10-3 at the bottom of the trigger.
C. Click Save.
5. Test the callout.
A. Test an invalid address: In the UI, open an existing candidate, and change the
address to your home address. You need to refresh your screen (F5) to see the
resulting validation error. You should receive a message that your address is invalid.
B. Test a valid address: Modify the candidate record using the only address that is
“valid” according to this simple Web service that is helping Universal Containers test
the platform’s capabilities to make callouts:
Street Address 1: One Market
Street Address 2: Suite 300
City: San Francisco
State/Province: CA
Zip/Postal Code: 94105
Country: USA