Microsoft Business Central API (Beta) – Create a sales order and sales order line through PowerApps/Flow

At work I was experimenting with Microsoft PowerApps, Flow and the Business Central Beta API, trying to create a PowerApp that lets me create a sales order and the according sales order lines through Microsoft Flow. I ran into issues when I wanted to create a sales order line through Flow and got the following error message:

{
“status”: 400,
“message”: “ErrorCode: Application_DialogException You must specify a document id to get the lines.\r\nclientRequestId: bf31d2f6-3d8a-46d9-b0e6-0e8048352c7e”,
“source”: “api.businesscentral.dynamics.com”,
“errors”: []
}

So yeah, Microsoft Flow asks for a Document ID, but wait, there’s no field to specify the Document ID?

Luckily, Microsoft Flow supports HTTP requests which means that we can use the HTTP methods of the REST API’s to get our data. And that’s exactly what we are going to do. Follow through!

Side note: I assume you’re familiair with the basics of REST API’s, Microsoft Flow and PowerApps.

Prerequesites

The following is required to properly use the REST API of Microsoft Business Central:

  • Access to a Dynamics 365 Business Central environment.
  • Credentials necessary to connect and authenticate against the API. In this example I’m using basic authentication that works with a Web Service Access key. I’ll explain in this guide on how to get your key.
  • Your tenant ID. Your tenant ID can be found in the URL of Business Central, like shown below. You can also find your tenant ID in Microsoft Azure AD.

  • Access to Microsoft Flow.
  • Optional: a PowerApp that uses the Flow. Showing how to create a PowerApp is out of scope for this guide, but I’ll use my PowerApp to execute my Flow. There are plenty of guides on PowerApps on Google and YouTube :-).

Acquiring a Web Service Access key

You need a Web Service Access key to connect and authenticate against the Business Central API. In Business Central, do the following:

  1. Use the search function in BC. Located in the upper right corner or use Alt + Q. Search for Users and click on it.
  2. Locate your user name and click on it.
  3. On the user card page, your user name that will be used for authentication is on the General tab. Write down your user name.
  4. Generate a new Web Service Access Key using the AssistEdit button or just copy the currently shown web key. If this field is blank, generate a new key. Write down the key, we need this in Microsoft Flow.
  5. We’re done in Business Central for now. Next step is to configure Microsoft Flow.

Configuring Microsoft Flow

Allright, we have our Web Service Access Key. Great! Next step is to build a flow in Microsoft Flow that executes a POST request to the Business Central API so that the sales order lines will get created. I’ll show my complete Flow and guide you through step-by-step. You probably won’t need all the steps; suit the Flow to your needs.

  1. Step 1: make sure you have a connection with Microsoft Business Central in Microsoft Flow. I assume you are reading this guide because you can’t create sales order lines. Anyway, check your connections tab and make sure you have a connection with BC. Screenshot below is in Dutch, I’m lazy and Dutch ;-).
  2. Create a new Flow in Microsoft Flow. Easy peasy, not going to explain this.
  3. Open your newly created Flow.
  4. Time to explain the Flow I created before. Follow along and at the end you’ll have a Flow that creates sales order lines.

Creating the sales order lines Flow

The screenshot below shows the Flow that I used to create sales order lines. I’ll explain every step along the way; you don’t need all of them for it to work. The steps required are numbered from 1 to 4.

PowerApps:

  • This trigger step is required to run a Flow from a PowerApp. If you want to run your Flow through a PowerApp, use this step. If you don’t, you don’t need it.

Get My Profile (V2):

  • Gets your Office 365 profile information for use in Flow. Not required, but nice if you want to make your mails/messages more personal. I usually use this step in combination with the O365 Outlook connector.

Variable step:

  • This step is not mandatory, but I used a variable because I use this Flow in PowerApps. This step stores the item amount the user gave up in the PowerApp and saves it in the variable for later use. If you use this step, give it a logical name like any variable, set the Type to Integer and the Value to: Ask in PowerApps.

Create a empty Sales Order (MANDATORY):

Here come the mandatory steps. Step 1 is to create a empty sales order action. Click New Step in Flow -> Search for Business Central -> Actions -> Create Item.

Next up, select the Company that the step applies to and select the Table. For this example I’m using the Cronus demo company and the salesOrders table. Then, fill the following fields:

  • orderDate: use a function to automatically fill the date. Here’s the expression I used: formatDateTime(utcNow(),’yyyy-MM-dd’).
  • customerNumber: I selected customer 10000 for this example. You can also ask this in PowerApps if you want. For now, just use a dummy.

Get the items that we need to add to the sales order (MANDATORY):

We just created the step to create an empty sales order in Flow. Next step is to get the item info of the items that need to be added to the sales order. Perform the following steps:

  • Insert a new step in Flow -> Select Business Central connector -> Get item. Fill in the company name and the table name. Company name in this example is Cronus, table is items.
  • Next up we need the Row ID of the item that needs to be added to the sales order line. I use PowerApps to dynamically determine the Row ID of an item. You can also use the Table Viewer in Business Central to determine ID of the item you want to add to your sales order: https://businesscentral.dynamics.com/?table=27. Scroll to the right to see a list of ID’s that you can fill in.

Fill in the selected Row ID in Microsoft Flow.

Final step – use a POST call to create a sales order line (MANDATORY):

  • Add a new step in PowerApps -> Search for HTTP -> select the basic HTTP connector. Don’t choose the Swagger or Webhook connector.
  • Fill in the following information:
    • Method: POST.
    • URI: https://api.businesscentral.dynamics.com/v1.0/<yourTenantID>/api/beta/companies(<company ID>)/salesOrders(<Sales order number>)/salesOrderLines
    • Use the following JSON body:
      • {
        “itemId”: “<item id retrieved in previous step>“,
        “lineType”: “Item”,
        “quantity”: <fill in number or use variable in Flow>
        }
    • Click Show advanced options.
    • Verification Type: Basic.
    • Fill in the username and password you’ve retreived in the step “Acquiring a Web Acess Key”.

See the screenshot below of the configuration I used. I’ve hidden my Tenant ID.

Executing the Flow

We just created a Flow that creates an empty sales order, retrieves an item based on an item ID and creates sales order lines. Next step is to test the Flow and ensures it works.

  • While your Flow is in Edit mode, right click on Test in the upper right corner.
  • Select “I will execute the trigger activity”. You can also use the previous values if you like.
  • Save and test.

If executed succesfully, all steps will have a green check. I’ll show you the most important inputs below.

Input of the HTTP POST we executed

And the output in Business Central:

 

Leave a Comment

This site uses Akismet to reduce spam. Learn how your comment data is processed.