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:

    1. Enter the API name as โ€œUserName_Acc_AddActivitiesโ€. The name indicates that a customer activity is added.

    2. Select the API method as โ€œPOSTโ€. This defines that the API will be posting or sending data.

    3. 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:

    1. By default, the name for the request body is โ€œdefaultRequestJsonBodyโ€. For this exercise, label it as โ€œActivityRequestJsonBodyโ€.

    2. Content type from the list of values is selected as โ€œapplication/jsonโ€. This defines the type of content in the request.

    3. Character set is selected as โ€œUniversal (UTF-8)โ€.

    4. Op.โ€onally enter a description for the โ€œRequest Bodyโ€. (This is used in the API documentation.)

    5. 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:

    1. By default, the name for the response body is โ€œdefaultResponseJsonBodyโ€. For this exercise, keep the response body name as is.

    2. The Content type from the list of values is selected as โ€œapplication/jsonโ€. This defines the type of content in the response.

    3. The Response Code is selected as โ€œ200 OKโ€.

    4. Character set is selected as โ€œUniversal (UTF-8)โ€.

    5. Optionally, add a description for the โ€œResponse bodyโ€. (This is used in the API documentation)

    6. 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:

    1. Select Target Entity as โ€œUserName_Acc_customersโ€.

    2. 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.

    3. โ€œ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:

    1. Variable Name: โ€œRequestโ€, Data Type: โ€œWebRequestโ€. This variable contains the Web Request received.

    2. 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:

    1. Variable Name: โ€œcustomeractivityโ€, Data Type: โ€œUserName_Acc_Activitiesโ€. This variable will contain customer activity details for the customer activities entity.

    2. Variable Name: โ€œcustomerโ€, Data Type: โ€œUserName_Acc_customersโ€. This variable will contain customer information from the customer account entity.

    3. 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:

    1. Select Action as โ€œUpdate Variableโ€.

    2. Select the Target variable as โ€œcustomer.Customer_IDโ€.

    3. 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:

    1. Select Action as โ€œTraverse Listโ€. A list of the activity records will be traversed.

    2. Select โ€œTraverse the Listโ€ as Variables>Request>ActivityRequestJsonBody>Activities. It is displayed as โ€œRequest.ActivityRequestJsonBody.Activitiesโ€.

    3. 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:

    1. Variable Name: โ€œitemโ€, Data Type: โ€œJsonโ€. This variable is automatically created on selecting โ€œTraverse Listโ€ Action.

    2. 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:

    1. Select Action as โ€œUpdate Entity fields from jsonโ€.

    2. Select the Target variable as โ€œactivityโ€.

    3. 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.

    1. Select Action as โ€œAdd item to Listโ€.

    2. Select the Target variable as customer>Customer_Activities. It is displayed as โ€œcustomer.Customer_Activitiesโ€.

    3. 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:

    1. 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.

    2. Select โ€œresultofupdateโ€. (This variable โ€œresultofupdateโ€ displays success or error messages.)

    3. 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:

    1. Select Action as โ€œUpdate Variableโ€.

    2. Select the Target variable as Response>defaultResponseJsonBody. It displays as โ€œResponse.defaultResponseJsonBodyโ€.

    3. 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.

    1. Select Action as โ€œAPI Respondโ€.

    2. 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