In this Tech Tuesday, we’ll look at what it takes to create a Microsoft Dynamics 365 Business Central Sales Invoice with multiple line types using the OData v4 page endpoints.
Creating a Business Central Sales Invoice
First, if we don’t have it, we need to publish the OData page required for the specific integration we’re looking for. If that page number is not known, the easiest way to find out is to use Business Central “Help” to tell us that.
With the Sales Invoice page open in Business Central, just press Ctrl+Alt+F1.

On the side panel, view “Page Inspection“. It shows that this is the Sales Invoice Page number 43.
Going to the Web Services page in Business Central, we can see that Page 43 was already published with the name “SalesInvoices“. If it wasn’t, we can add it here using the New button.

Once published, press the Refresh Metadata button in the SmartConnect Business Central Connector to make sure that SmartConnect refreshes its cache to include this new web service as shown below.

The actual integration can be as simple or as complex as you need it. Since the integration page we’re using (43) is the actual Business Central page that you enter by hand, we should be able to add any information on that page that we need.

In this case, we’re using a simple file with a Document Number, optional Line Number, the Item/account number, and a few other fields.
When creating the new map, we select this data source.

The “Data Source Keys” tell SmartConnect how the data for a map should be grouped into documents.
From the data preview, it would appear that we can use “DocNo” as our Data Source Key. This will give us two invoices since there are two different values.
For the map Destination, we choose Dynamics 365 Business Central OData and then select the SalesInvoices service we published earlier.

By itself, that is ONLY the Sales Invoice Header – no lines. However, if we mark “Include linked entities“, SmartConnect will add this for us as part of that service instead of having to add the SalesLines Service (page 47).
Looking at the mapping for the SalesInvoice (header) node, we’ll map the bare minimum.

We have the choice to map the “No” field (Document Number), which we did for this example. If left empty, then Business Central would default to the “next” number in the Series that Sales Invoices are using, if set up for it.
SmartConnect also makes us group the data, but we did that anyway because we only need one “header” record, and we have multiple line items for each document.
We’ll also take the opportunity to mark the “update existing” checkbox so that we can update an existing document if it already exists.
The “Group On” field serves two functions:
1.) To physically group the data as we need here when the “update existing” checkbox is marked.
2.) To allow SmartConnect to look up records in Business Central to see whether they exist. This is necessary because the Business Central API has slightly different calls for updating an existing record versus inserting a new record.
Saving the header, we open the SalesInvoiceSalesLines node next.

Once again, SmartConnect makes us group our data, which we’d need anyway if we mark the “Update Existing” checkbox.
Because we don’t want to actually “group” the lines like we did with the header, we’ll group on fields that make a unique record. In this case, the DocNo & the LineNo fields are unique per document. The Account_Item field contains the Item Number or Account Number, and the Type field tells Business Central what “type” of data has been added, either Item or G/L Account in our case.
Save the map and run it twice: once to create the invoice, and again to verify it updates that same Sales Invoice and does not fail with duplicate errors or create extra lines.

Reviewing one of the documents in Business Central, we can see it came in correctly.

As noted earlier, Business Central can also default the Document Number if we leave it blank instead of mapping to it as the above example does.
If we duplicate the map and review the new one, the mapping for the SalesInvoice & SalesInvoiceSalesLines is very similar. However, No on the header and Document_Number on the line are left unmapped. In the SalesInvoices node, the External Document Number is mapped to DocNo instead, and the grouping was changed to DocNo & CustomerID.

Run the duplicated map twice as well. When reviewing in Business Central, we can see the four imported invoices.

The top two invoices were created by the first map using the provided DocNo data.
For the other two, created by the second map, we let Business Central default the Document Number and we can see that our DocNo field populated the External Document No. field as we mapped it.
Below is a zip file containing the exported maps:
1.) The one noted above, where we provided the Document Number.
2.) Essentially the same map, but the Document Number is left empty and is defaulted by Business Central.