Step 3: Create the API
Once the Entity is created, create an API to generate customer activities records and store them in the Activities Entity created in Step 1.
The API will receive the customer ID as a single value and a list of customer activities (based on the 1 to Many relation configured in Step 2). The items in the list will be defined by the attributes of the fields created in the Entity โUserName_Acc_Activitiesโ.
When all the items in the list are read, the API will create a new record for the Entity โUserName_Acc_Activitiesโ. The API will then update the activity information against the customer ID received.
To go to APIs, click โAPIโ under the โAPIs and integrationโ menu on the left side panel.
To create a new API, click the [+ API] button.
The New API page displays with the Details tab preselected. To define the API, add the following information:
Enter the API name as โUserName_Acc_AddActivitiesโ. The name indicates that a customer activity is added.
Select the API method as โPOSTโ. This defines that the API will be posting or sending data.
Optionally add a description for the API.
Select the Group "UserName".
Add constant text for API Paths as โaccโ and โaddactivitiesโ. Each of both parameters displays in a blue bubble.

The URL for the API is generated.
Enable โpublic documentationโ. This defines the API documentation to be accessed publicly.

To define the Request body and Response body details, go to the โInputs & outputsโ tab.

To define the Request body, go to the Inputs & outputs tab>Request body section. Click it to expand the already created Request Body. It is defined with the following information:
By default, the name for the request body is โdefaultRequestJsonBodyโ. For this exercise, label it as โActivityRequestJsonBodyโ.
Content type from the list of values is selected as โapplication/jsonโ. This defines the type of content in the request.
Character set is selected as โUniversal (UTF-8)โ.
Op.โonally enter a description for the โRequest Bodyโ. (This is used in the API documentation.)
To define the Json object, enter the following sample json object:
{ "customer_id" : "string: representing the customer GUID","activities" : [ { "Activity_Type" : "string: one of the following: Incoming Support call,Email,Product details request,Product Offer email,Outgoing call","Activity_Description" : "string: the description text of the activity","Activity_DateTime" : "string representing the date time in the form of YYYY-MM-DD hh:mm:ss","Activity_Priority" : 0,"Activity_AssignedTo": "string representing the person this activity is assigned to and if missing sets it to default","Activity_CreatedBy" : "string representing the person created this activity and if missing sets it to default" } ] }This json object indicates the type of request that will be received including the Customer ID and the list of activities for the customer. The key โactivitiesโ has the value enclosed in โ[ ]โ. This means that this key will have a list of values instead of just one value.
To define the Response body, go to the Inputs & outputs tab>Response body section and click it to expand it. A Response body is already created and defined with the following information:
By default, the name for the response body is โdefaultResponseJsonBodyโ. For this exercise, keep the response body name as is.
The Content type from the list of values is selected as โapplication/jsonโ. This defines the type of content in the response.
The Response Code is selected as โ200 OKโ.
Character set is selected as โUniversal (UTF-8)โ.
Optionally, add a description for the โResponse bodyโ. (This is used in the API documentation)
To define a sample Json for the response, type the following json object:
{ "status": "success or failed","errorcode": "0","message": "The response of creating the entity record.","details": [] }This object indicates that: - The status is โsuccessโ or โfailedโ. - The error code is zero (0). - The message displayed is โThe response of creating the entity recordโ. - The details will be a list of values as identified by the two square brackets โ[]โ.
To define the Target Entity for this API, go to the โTarget Entitiesโ tab. Target entities are the entities that the Actions in this API can process data into i.e. perform add, update or delete upon. To create a new target entity, click [+ Target entity].
To define the Target Entity for storing the created record:
Select Target Entity as โUserName_Acc_customersโ.
Keep โSelect a default Validation pipelineโ as is. If a Validation pipeline needs to be applied to the data before storing in an entity, the relevant Validation pipeline can be selected in this field. In this exercise, no Validation pipeline is applied.
โShow in API gatewayโ toggle button is enabled by default. This must be enabled so that the API is visible to the API gateway through which it can process the information into the entity.

To see the default variables created for an API, go to the โVariablesโ tab. The following two (2) Read Only variables are predefined:
Variable Name: โRequestโ, Data Type: โWebRequestโ. This variable contains the Web Request received.
Variable Name: โResponseโ, Data Type: โWebResponseโ. This variable contains the Web Response sent.
To create Actions for this API, go to the โCodeโ tab. By default, an โAction boxโ is already created with the description of the โAction boxโ as โOnExitโ.
Double click the description and label the โAction boxโ as โadd customer activitiesโ.
Expand the โAction boxโ. In the โCodeโ tab, an Action called โAPI Respondโ is already added. Notice the variable as โdefaultResponseJsonBodyโ. Remove the existing action โAPI Respondโ. (It will be added later after all the actions are defined as no action is executed after โAPI Respondโ Action.)
To define the variables, go to the โVariablesโ tab of the โAction boxโ. To create a new variable, click the [+ Variable] button and create 3 new variables.
Define the variables as follows:
Variable Name: โcustomeractivityโ, Data Type: โUserName_Acc_Activitiesโ. This variable will contain customer activity details for the customer activities entity.
Variable Name: โcustomerโ, Data Type: โUserName_Acc_customersโ. This variable will contain customer information from the customer account entity.
Variable Name: โresultofupdateโ, Data Type: โJXPResponseโ. This variable contains the response to be shown to the user i.e. success or failure of creation of record.
To define the actions for the API, click on the โCodeโ tab. Add Action by clicking the (+) sign in the โAction boxโ.
To receive the details (from the json object) for the customer field โCustomer_IDโ in the Entity โUserName_Acc_customersโ (via โRequest Bodyโ), define the following Action to first convert it to GUID data type from string and update the variable โcustomerโ as follows:
Select Action as โUpdate Variableโ.
Select the Target variable as โcustomer.Customer_IDโ.
Select Value as Variables>Request>ActivityRequestJsonBody>customer_id>toGUID(). It displays as โRequest.ActivityRequestJsonBody.customer_id.toGUID()โ.

Add four (4) more โActionsโ by clicking the (+) button.
To traverse a list of items received in the Activities list in the Request, update each item to the corresponding field in the record for the Activities entity and update the information for the customer via the โCustomer_Activitiesโ relation, define the second Action as follows:
Select Action as โTraverse Listโ. A list of the activity records will be traversed.
Select โTraverse the Listโ as Variables>Request>ActivityRequestJsonBody>Activities. It is displayed as โRequest.ActivityRequestJsonBody.Activitiesโ.
The โitemโ is the element in the list to be traversed. It is automatically populated.

To store the values in the โActionsโ, in the โVariablesโ tab of the โTraverse Listโ Action:
Variable Name: โitemโ, Data Type: โJsonโ. This variable is automatically created on selecting โTraverse Listโ Action.
Add a variable with variable Name: โactivityโ, Data Type: โUserName_Acc_Activitiesโ. This variable will be updated with the value from the item traversed.
To define โActionsโ in the โTraverse Listโ, go to the "Code" tab and add two (2) Actions by clicking the (+) button.
To take the value of each item received in the Json request (โitemโ) and update it to a corresponding field for activity record (for โUserName_Acc_Activities entityโ), define the first Action in the Traverse List as follows:
Select Action as โUpdate Entity fields from jsonโ.
Select the Target variable as โactivityโ.
Select Source variable as โitemโ (Variables>item).
The second Action in the Traverse List updates the value of the activity variable and updates the variable customer information through the 1 to many relation.
Select Action as โAdd item to Listโ.
Select the Target variable as customer>Customer_Activities. It is displayed as โcustomer.Customer_Activitiesโ.
Select Source variable as Variables>activity. It is displayed as โactivityโ.

To add the detail as a record to the target entity, define the third Action after the โTraverse Listโ in the โAction boxโ to update the record in the target entity:
Select Action as โUpdate Entity record on primary keyโ. This Action updates the record of the Target Entity or related entities based on the primary key.
Select โresultofupdateโ. (This variable โresultofupdateโ displays success or error messages.)
Select Source as โcustomerโ.

To convert the โresultofupdateโ variable to json and store it in the response body, define the fourth Action in the โAction boxโ as follows:
Select Action as โUpdate Variableโ.
Select the Target variable as Response>defaultResponseJsonBody. It displays as โResponse.defaultResponseJsonBodyโ.
Select Value as Variables>resultofupdate>toJson(). It displays as โresultofupdate.toJson()โ.

The fifth and last Action in the โAction boxโ sends the response to the user.
Select Action as โAPI Respondโ.
Select Response Body as โdefaultResponseJsonBodyโ.

After all the Actions are defined, the โCodeโ tab should look as per the image below.

To save the API, click the [Save] button.

To publish the API, click the [Publish] button.

To define the API gateway, go to the Details tab and add the API gateway for this API as โUserName_Acc_AddActivitiesโ.

To save the API, click the [Save] button.
In the โDetailsโ tab and on the right-hand side, ensure the API is enabled.

Last updated
