Creating flows for an API (Part 2)
In the preceding tutorial about creating flows for an API, you created a REST API to create a customer contact in Salesforce. This tutorial describes how to create an API that retrieves and updates contact details in Salesforce.
Before you start
- Complete the preceding tutorial at Creating flows for an API.
Create the flow for the API
- In the App Connect Dashboard, click the Customer API tile.
- Click the Operations tab. You can't edit the API because it is still running. To make changes, stop the API.
- To stop the API, in the navigation banner, click the options menu (⋮) and then click Stop API.
- Click
- Click Note: In this tutorial, you retrieve a customer by using an ID, and update a customer by using a filter that is based on the properties that you added for the model. In this case, the available filters are first name, last name, and email.
- For the Retrieve Customer by ID operation, click Implement flow.
- Notice the Request URL example. When you call this API, you must append the customer ID to the
end of the URL to retrieve the customer details.
Next, define which customer details you want to receive in the response.
- Click Add node to add a node to the flow.
- Select . (If App Connect isn't already connected to Salesforce, specify the name and password of your Salesforce account.)
- You want to retrieve contacts whose contact ID is identical to the customer ID that the user of your API enters. Therefore, add a condition and update the Where fields as follows.
- Leave the other fields with their default values.
- Click Response to define the status code and which fields to return in the response body.
- Leave the status code as the default and map all the fields to the Salesforce Retrieve contacts
fields.
Ignore the warning messages. They don't affect the response because only a single contact (that corresponds to the unique ID that is specified) is returned.
Note: When you're creating an object, typically the response message includes only the ID from the target application. If you're retrieving an object, the response message shows all the fields that you requested from the target application.In the Response header section, you can choose your own response code mapping. The following response codes are returned for the different operations:- Create operations return a response code of 201 (record created).
- Retrieve operations return a response code of 200 (record retrieved).
- Replace or create operations return a response code of 200 (record replaced) or 201 (record created).
- Click Done
- Notice the Request URL example. When you call this API, you must append the customer ID to the
end of the URL to retrieve the customer details.
- For the Replace or create Customer with filter operation, click
FirstName
andLastName
to deselect these properties as filters. Then, click Implement flow.- Notice the request URL example and request body example.
- Click (+).
- Select .
- You want to uniquely identify the contact to be updated. Therefore, update the
Where fields as follows. Select Email in the first
field. In the last field, open the list of available inputs and map to the
Email
value under Request body parameters.
- Configure these Salesforce fields by mapping their values to the corresponding values from the
request body parameters.
- Map Last name to LastName
- Map First name to FirstName
- For Email, the value in the
where
condition is used. Therefore, you don't need to specify a mapping manually.
- Click Response.
- Leave the status code as
200
. - Configure the response body fields by mapping their values to the values from the Salesforce Update or create contact fields as follows.
- Click Done and then start the API.
Test your flow
You can test the flow by retrieving a customer and then updating the first name of the same customer. You can test the flow by using the API portal in App Connect. If you're familiar with using other tools such as cURL to invoke an API, you can also test the flow for your API by using those tools.
Testing in the API portal
- On the Manage tab, go to the Sharing Outside of Cloud Foundry
organization section.
The API key and associated API documentation link for the API Portal, which you generated to test the Create Customer operation in the Creating flows for an API (Part 1) tutorial, is displayed.
- Copy the API Documentation Link link and paste into your web browser to open the API in an API portal. Notice that the Customer.create operation that you created in the earlier tutorial is shown under Operations, together with the two new operations that you created in this tutorial.
- Click the Customer.create operation.
Then, invoke the Customer.create operation again, as described in the Finally, test the flow for your API section of the part 1 tutorial. Next, copy the
CustomerID
value that Salesforce returned and paste it into a text editor. - To invoke the operation to retrieve a customer by ID, click Customer.findById, then click Try it.
- In the Security / Client ID field, enter or paste the API key that you created in App Connect.
- In the ID field, paste the
CustomerID
value that you copied earlier. - Click Send.
- Scroll down to the Response section, where you can see the success code
200 OK
, and the response body that contains the contact details that were retrieved from Salesforce; for example:Response Code: 200 OK Headers: ... { "CustomerID": "0034I00001pvaenQAA", "Email": "test1.apiname@email.com", "FirstName": "Test1", "LastName": "APIname" }
- Copy the contact details in the response body and paste it into a text editor; for example:
{ "CustomerID": "0034I00001pvaenQAA", "Email": "test1.apiname@email.com", "FirstName": "Test1", "LastName": "APIname" }
- To invoke the operation to update the details of an existing a Customer by using a filter, click Customer.upsertWithWhere, then click Try it.
- In the data field, paste the contact details that you copied earlier, and then delete the
CustomerID
line. - Copy the
Email
value (for example,test1.apiname@email.com
) in the data field and then paste it into the Email field. (This value is used to filter for a Salesforce contact with this identical email address.) - In the data field, change the value of the
FirstName
property to a different name (for example,Test2
). You want to update the first name of the Salesforce contact whose email you specified in the previous step. - Click Call operation.
- In the Response section, you can see the success code
200 OK
, and the response body that contains theCustomerID
value. You can also see the updatedFirstName
value and unchangedEmail
andLastName
values; for example:Response Code: 200 OK Headers: ... { "CustomerID": "0034I00001pvaenQAA", "Email": "test1.apiname@email.com", "FirstName": "Test2", "LastName": "APIname" }
- You updated your API with two more operations. Close the API portal tab to return to the API flow. Then, click Dashboard to return to the App Connect Dashboard.
- Go to your Salesforce instance, and verify that the contact's first name changed.
- As in the previous tutorial, you can use Postman, or you can use your preferred utility, such as the Linux® command line and cURL, or the HttpRequester add-on for Firefox.To retrieve a customer, complete the following steps.
- Click the Manage tab and make note of the URL, username, and password. You can use these credentials to invoke the API operations.
- In the Postman utility, open a new tab and select the GET function. (The following steps use Postman version 5.3.1. Some steps might differ for different versions.)
- In the Enter request URL field, paste the API base URL that you copied
earlier and append the model name and customer ID to the end. For example,
/Customer/0030Y00000X7g6uQAB
.Note: To get a customer ID, open Salesforce and click a contact record. The customer ID is displayed in the Salesforce URL when you're in the contact record. For instance, it's the string0030Y00000X7g6uQAB
from the URLhttps://eu11.lightning.force.com/one/one.app#/sObject/0030Y00000X7g6uQAB/view?a:t=1507115352306
- In the Authorization tab, change the Type to Basic Auth.
- Add the username and password that you copied earlier to their respective fields.
- Click Update Request.
- Go to the Headers tab and select the following key and value pairs.
- Key
Content-Type
and valueapplication/json
- Key
- Click Send.
- The customer details are displayed. Notice that the response contains all the fields that you specified when you created the flow.
- Copy the response for testing the replace or create operation.