Technology Blogs by SAP
Learn how to extend and personalize SAP applications. Follow the SAP technology blog for insights into SAP BTP, ABAP, SAP Analytics Cloud, SAP HANA, and more.
cancel
Showing results for 
Search instead for 
Did you mean: 
dipper
Explorer
In the previous blog post, you have learnt how to make the bot automatically extract entities from user input. In this part, we will focus on the “Build” tab and learn how to build the handling logic of our bot based on the extracted entities.

Step 1: Add a “request-sales-order-creation” Skill


A skill is a handling logic for the bot. If you switch to the tab “Build”, you can see there are already two skills:

- fallback: If a user input doesn’t trigger any skill, the fallback skill will be triggered.

- disambiguation: If a user input triggers more than two skills, the disambiguation skill will be triggered.


(Some parts of the screenshot are blurred for protecting user information)

Now we need to add a skill to catch the user intent of creating sales order.

1. Switch to the “Build” tab, and click the “Add skill” button


2. Name it as “request-sales-order-creation”, choose “Business” as “Type”, and click “Add”.

3. Open the “request-sales-order-creation” skill, and switch to the “Triggers” tab. Then set the trigger to “If @create-sales-orders is-present”.


By doing this, when user enters something that matches the “want-to-create-sales-orders” intent, this skill will be triggered.

4. Switch to the “Requirements” tab. You can see Primary requirements and Secondary requirements. The difference between primary requirements and secondary requirements is the former one must be gathered before executing desired actions.

In our case, we need the sold-to party and the other fields to create a sales order, so we’re going to add them to primary requirements.


In the screenshot, the "#soldto-party" on the left side is the entity extracted by the bot, and the "SoldToParty" on the right side is a name that will be used to store the entity values in the bot’s memory.

Okay, now let’s test the bot to see whether the skill works as we expect.

Step 2: Test the Bot (Optional)


1. Open the Chat Preview side menu, and input “I want to create sales order for customer 10100001, 5 PC of product TG11, sales area 1010/10/00”.

2. To check whether the skill has been triggered, click the small yellow information button to see the JSON View of the bot. In the JSON View, if you scroll down to find the “memory” field, you can see a SoldToParty value in the memory block. That means the bot successfully detected the sold-to party and stored it into the memory.


3. Click the “Open debug panel” button to see the detailed handling process of our input:


So, we can know that first the skill "request-sales-order-creation" was triggered, then the bot stored the SoldToParty into the memory, and in the end the actions of the skill request-sales-order-creation were executing.

Now that the bot kill can be triggered, let’s add more fields.

Step 3: Add More Primary Requirements


Beside the sold-to party, we need more fields to create a sales order. For example, the product and requested quantity with the unit of measure.


For “#product”, ""#product-quantity", and "#product-uom" entities, we let the bot directly store the extracted values to the bot’s memory.

But for the entity "#sales area", we need to do some post-processing. Because a sales area is a combination of sales organization, distribution channel and division, and its value will be like “1010/10/00”. So, we want to store 1010 to sales organization, 10 to distribution channel, and 00 to division separately.

To reach our goal, we need to customize the storage process based on extracted entities. Let’s click the “+ New Replies” button of “If #SalesArea is complete”:


In the dialog, choose Update Conversation -> EDIT MEMORY.


Then set memory fields as:


















SalesOrganization Use scripting to build my JSON "{{itemAt (split memory.SalesArea.raw '/') 0}}"
DistributionChannel Use scripting to build my JSON "{{itemAt (split memory.SalesArea.raw '/') 1}}"
OrganizationDivision Use scripting to build my JSON "{{itemAt (split memory.SalesArea.raw '/') 2}}"





Please note that you need to switch on the “Use scripting to build my JSON” option. And in scripting we use two syntax called itemAt and split. If you want to check more details about the scripting syntax, you can check with link Scripting Syntax - SAP Help Portal | SAP Help Portal.

Basically, we want to split string “1010/10/00” with “/”, and assign the first element to Sales Organization, the second to Distribution Channel and the third to Division.

Step 4: Test the Bot (Optional)


Now let’s input “I want to create sales order for customer 10100001, 5 PC of product TG11, sales area 1010/10/00” again in the Chat Preview side menu.

If everything works as expected, the memory has been set correctly.



Step 5: Prepare the Payload for API


Okay, with previous steps, our bot has the necessary values stored in the memory. Next, we will prepare the payload for API. I want to check sales orders in my SAP S/4HANA Cloud system, so I choose the Sales Order (A2X) API for SAP S/4HANA Cloud.

After checking the API document in the API Business Hub, we will see that for creating a sales order, we need to send a POST request to /A_SalesOrder with a JSON payload like:
{
"SalesOrderType": "",
"SalesOrganization": "",
"DistributionChannel": "",
"OrganizationDivision": "",
"SoldToParty": "",
"to_Item": [
{
"Material": "",
"RequestedQuantity": "",
"RequestedQuantityUnit": ""
}
]
}

Now let us build the payload.

1. Switch to the “Actions” tab of skill “request-sales-order-creation”, and click the “+ New Action Group” button:


2. Click Update Conversation -> EDIT MEMORY, let’s set the key ApiPayload to be:
{
"SalesOrderType": "OR",
"SalesOrganization": "{{memory.SalesOrganization}}",
"DistributionChannel": "{{memory.DistributionChannel}}",
"OrganizationDivision": "{{memory.OrganizationDivision}}",
"SoldToParty": "{{memory.SoldToParty.value}}",
"to_Item": [
{
"Material": "{{uppercase memory.Product.value}}",
"RequestedQuantity": "{{memory.ProductQuantity.value}}",
"RequestedQuantityUnit": "{{uppercase memory.ProductUoM.value}}"
}
]
}


3. Click Choose Message Type -> Text to add a bot reply. We’ll let the bot reply with the payload so that we could easily check the payload value.


The bot reply supports scripting syntax, so we can simply use {{memory.ApiPayload}} to refer to the payload in the bot’s memory.

4. Use the test sentence “I want to create sales order for customer 10100001, 5 PC of product TG11, sales area 1010/10/00” again in the Chat Preview side menu, and check the reply:


Cool, now we have the payload for API.

Step 6: Set up the Communication Management in SAP S/4HANA Cloud


According to the API overview, the API that I’m going to use belongs to communication scenario SAP_COM_0109 (Sales Order Integration). So, before sending the payload to the API, I need to set up the communication management in my S/4HANA Cloud system.

For setup instructions, see blog Setting up Communication Management in SAP S/4HANA Cloud. Basically, the setup is about creating the communication user, communication system and communication arrangement. Once you finish the setup, then do the following:

1. Note down the communication user and its password:


2. Note down the system-specific service URL for Sales Order (A2X):


3. (Optional) Check the API with Postman to see whether it is correctly configured:



Step 7: Configure the System Alias


Now we have the communication user, its password and service URL for chatbot usage. Let’s maintain these values into chatbot’s system alias.

1. Click the “Settings” button, and enable the system alias.



2. Add a system alias named “S/4HANA Cloud Sales Order Service”.

3. Switch to Environments settings. Fill the URL with the system-specific service URL, and Authentication with the communication user and its password.



Step 8: Let the Bot Call API


1. Open the “Build” tab of the chatbot, and create a new skill named “call-api-to-create-sales-order”

2. Open the “Actions” tab of this new created skill, and add a “COMSUME API SERVICE” action. You can check more details about how to connect external service in here.


3. First we will let the bot fetch the x-csrf-token from API.



In the screenshot, you can see that I can directly choose the system alias “S/4HANA Cloud Sales Order Service”. The system alias is a convenient feature to help us to configure different environment for a chat bot.

Also, the namespace for response is “metadata” and includes the headers.

4. Add an “EDIT MEMORY” action, and set memory fields:

 













ApiCookie Use scripting to build my JSON "{{api_service_response.metadata.headers.set-cookie.1}}"
ApiXCsrfToken Use scripting to build my JSON "{{api_service_response.metadata.headers.x-csrf-token}}"

 

Please be aware: The API cookie is not always the second element of set-cookie response header. You need to try and confirm its position in the Postman.

5. Add another “COMSUME API SERVICE” action:




This action is used to create a sales order with the payload. I set the namespace for this response to “creation-result”. I’ll use this namespace to check whether the sales order is successfully created.

Step 9: Let the Bot Tell the Creation Result


Now we want the bot to tell us the creation result after calling the API, so I’m going to add two new action groups to let the bot reply with messages that vary with the creation result.

1. Click the “+ New Action Group” to add an action group for handling the creation success case.


2. Set the condition of this action group to “_api_service_response.creation-result.body.d is-present”, and the text message to “Your sales order {{api_service_response.creation-result.body.d.SalesOrder}} has been created.”:


3. Add another Action Group for handling the failed creation case.

4. Set the condition of this action group to “_api_service_response.creation-result.body.error is-present”, and the text message to “Error happens when creating sales order: {{api_service_response.creation-result.body.error.message.value}}”:



Step 10: Connect Two Skills


1. Open the skill “request-sales-order-creation”.

2. Switch to the “Actions” tab of this skill.

3. Add a “GO TO” action, and make it redirect to the skill “call-api-to-create-sales-order”.




Step 11: Test the Bot


Great. Now everything is ready. Let’s test the bot.

1. Open the Chat Preview side menu

2. Input “I want to create sales order for customer 10100001, 5 PC of product TG11, sales area 1010/10/00” and press send.

3. If everything goes fine, the bot replies with message “Your sales order 3768 has been created.”


4. Okay now let us check this sales order 3768 in SAP S/4HANA Cloud system.


Superb! The sales order has been created with the expected data!

Okay that’s all for the part 2 of our blog series. In this blog post, we learned how to build business logic with extracted entities and how to make the bot call API. Hope you enjoy it.

In the next blog post, I’ll focus on the “Connect” tab and show you how to integrate this bot to a html web page. If you have any questions or ideas, feel free to comment under this blog post. Please also feel free to follow this blog post or my profile to receive new article notifications. See you soon 🙂

Series Navigation


Part 1: Train

Part 2: Build (Current article)

Part 3: Connect to Webpage

Related Information


SAP Conversational AI

SAP Conversational AI - Scripting Syntax

SAP Conversational AI - Connect to External Service

SAP API Business Hub - Sales Order (A2X)

Setting up Communication Management in SAP S/4HANA Cloud | SAP Blogs

 
3 Comments