You are on page 1of 7

INTEGRATING WITH

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

Exercise 3-1: Invoking Third-Party SOAP Services from Apex


Scenario:
Universal Containers wants to test the platform’s SOAP-based callout capabilities. To do so,
they will add new behavior that occurs when a candidate’s address is saved. When the
address is saved, a trigger on the Candidate object will make a SOAP-based call to an
address validation Web service hosted by Heroku. The Web service will determine whether
the address is valid, and errors will be displayed on the Candidate page.
Goal:
Use a SOAP-based Web service to validate an address created in a Salesforce app.
Tasks:
1. Import the Address Validation WSDL.
2. Whitelist the Heroku endpoint using a Remote Site Setting.
3. Create the class that contains the @future method.
4. Create the trigger that invokes the @future method.
5. Test the callout.
Time:
15 minutes
Note: We will be using the WSDL from this site:
http://trg-mock-address-validator.herokuapp.com/soap/validateAddresses
The WSDL being used is:
http://trg-mock-address-
validator.herokuapp.com:80/soap/validateAddresses?wsdl
In order for it to be consumed by Force.com, we removed the external import from the
schema definition. Feel free to compare the WSDL at the site with the WSDL provided for
the exercise.

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

©Copyright 2016 salesforce.com, inc. All rights reserved. 1


EXERCISE GUIDE
Apex Callouts

F. Click the Generate Apex Code.


G. Review the ValidateAddressesSOAP class that was created in Apex.
i. In the Salesforce UI in the top right-hand corner, click your name and then click
Developer Console. In the Developer Console, type CTRL+O, click on Classes in
the Entity Type pane, and double-click ValidateAddressesSOAP to open up the
generated Apex class.
ii. Use a text editor such as Notepad++ to open the 10-2.AddressValidation.wsdl
file in the Solutions folder.
iii. Compare the WSDL file you just opened with the ValidateAddressesSOAP
class.
a. Verify that, for every complex type in the WSDL, there is a corresponding
inner class in the ValidateAddressesSOAP class.
b. Verify that the address for the ValidateAddressesService service
described in the WSDL matches the endpoint (endpoint_x) described in the
inner class ValidateAddressesSOAP.ValidateAddressesPort
c. Verify that the URL stored in the variable
ValidateAddressesSOAP.ValidateAddressesPort.endpoint_x points
to a live Web service by copying the address into a browser and reading the
resulting messages, one of which should list the service we plan to use:
ValidateAddressesService
2. Whitelist the Heroku endpoint using a Remote Site Setting.
A. Within your org, navigate to Setup | Security Controls | Remote Site Settings.
B. Click New Remote Site.
Remote Site Name: HerokuWebService
Remote Site URL: http://trg-mock-address-validator.herokuapp.com
C. Click Save.
3. Create the class that contains the @future method.
A. Switch to the Developer Console window. If it is not already open, in the Salesforce
UI in the top right hand corner, click your name and then click Developer Console.
In the Developer Console, click File | New | Apex Class.
New Class Name: ValidateAddresses
B. Copy the code from the file named 10-2.ValidateAddressesWithSOAP.txt in the
Exercises folder, and paste it into the Apex Class tab, overwriting any existing code.
C. Complete the two TODOs for Exercise 10-2 in the ValidateAddresses class.
D. Click Save.
E. Note the following in the existing code:
i. Line 2: The declaration of variable AsyncValidationFlag. The invoking trigger
looks at the value of this static Boolean to prevent the invoking trigger from

©Copyright 2016 salesforce.com, inc. All rights reserved. 2


EXERCISE GUIDE
Apex Callouts

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

©Copyright 2016 salesforce.com, inc. All rights reserved. 3


EXERCISE GUIDE
Apex Callouts

Exercise 3-2: Invoking Third-Party Services with HTTP Callouts


Scenario:
Universal Containers wants to test the platform’s REST-based callout capabilities. To do so,
they will use the code from their test of SOAP-based callouts. However, this time, when a
candidate’s address is saved, a trigger on the Candidate object will make a REST-based call
to an address validation Web service hosted by Heroku. The Web service will determine
whether the address is valid, and errors will be displayed on the Candidate page.
Goal:
Invoke a third-party service using an HTTP callout.
Tasks:
1. Create the ValidateAddressesREST class.
2. Whitelist the Heroku endpoint using a Named Credential.
3. Modify the class that contains the @future method.
4. Invoke the @future method.
5. Test the callout.
Time:
20 minutes

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

©Copyright 2016 salesforce.com, inc. All rights reserved. 4


EXERCISE GUIDE
Apex Callouts

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

©Copyright 2016 salesforce.com, inc. All rights reserved. 5

You might also like