In a previous post, I explored the AP Automation framework used in Dynamics 365 for Finance and Operations (D365F&O) to import vendor invoices.
In this post, I am taking the concept one step further. I am invoking the AP automation framework through Microsoft Flow and the recurring integration framework.
Prerequisites
Before you can import vendor invoices there are a few things you need to do. These prepatory activities are described in the following subsections.
Data Package
The AP Automation framework is designed to import data packages – not individual files, so you need to be able to submit a data package to the service endpoint. A data package consists of the Manifest.xml file, the PackageHeader.xml file and data files for Vendor Invoice Header, Vendor Invoice Lines and Vendor Invoice Document Attachments respectively. The following screenshot shows my data package.

The easiest way to generate the XML files is to export a data package from the Data Management workspace in D365F&O. This generates a .ZIP file you can extract.
The files you want to submit must be zipped before they can be put on the job queue. Each .ZIP file represents a data package.
Data Management Import Job
Next step is to create an import job in the Data Management workspace in D365F&O. The following screenshot shows the import job I will be using to import vendor invoices.

Next step is to set up job recurrence. To do this, click on Create recurring data job in the ribbon. This governs how often the system should process messages put on the inbound message queue. This is shown in the following screenshot.

Some of the settings in this screen are very important, namely:
- ID = The GUID used to bind the enqueue http call to the job.
- Application ID = The application ID registered for D365F&O in system administration.
- Process messages in order = Must be set to Yes to ensure headers are processed before lines.
- Supported data source type = Must be set to Data package to enable import of data packages.
Folder Structure
Lastly, I have set up two folders on my OneDrive:
- Pending Invoices = This is where I places packages ready for import.
- Processed Invoices = This is were packages are moved to after import.
Now we are set up to build the flow that puts data packages onto the job queue.
AP Automation Flow
Any third party application that can make calls to a REST endpoint can put data packages on the message queue for D365F&O. In this example I am using Microsoft Flow.
The following screenshot shows the required steps in all their glory.

In the following subsections, I will be double-cliking on each box and explain their configuration. As you can see, I am manually triggering this flow. In the real world, the flow would be triggered automatically either through a schedule or when a file is created in the Pending Invoices folder. This can be automated using Microsoft Flow.
Read Files From Pending Invoice Folder
The first action in the flow is to get all files in the Pending Invoices folder as shown here.

Get File Content
For each file in the folder I get the file content (binary data stream).

Put Data Package onto the Job Queue
The following screenshot shows how you make an http POST call to the REST endpoint for the job queue set up earlier.

In the http call you need, as a minimum, to configure the following properties:
- Method = POST
- URI = https://%5BD365F&O URI]/api/connector/enqueue/[ID]; ID is the job ID generated in the recurring job
- Body = Binary file content for the data package
- Authentication = Active Directory OAuth
- Tenant = [Your Tenant]
- Audience = Mandatory, but content is not used. I use the D365F&O URI.
- Client ID = The application you have registered for OAuth in Azure
- Secret = The OAuth token issued for the application in Azure
This call to the REST endpoint with “enqueue” will put the data package onto the message queue associated with the recurring job. Depending on the frequency of the recurring job, the message will be processed automatically.
Move the File After Processing
Once the data package has successfully been put onto the queue, I move it to the Processed Invoices folder so it does not get picked up again.
Monitoring Messages in D365F&O
In D365F&O when I go to the recurring job and click on Manage messages in the ribbon, I can see the messages put on the queue by the flow. In the following screenshot you can see that at the top of the list, I have a queued message ready for processing after I invoked the flow.

When I select the message and click on Execution details in the ribbon I get a status on how the import is going as shown below.

In this case, the job has successfully imported 3 headers, 2 lines and 3 images from the data package.
Import Result
Now, when I go to Accounts payable / Invoices / Pending invoices I see three invoices in the overview:

When I click on the AP.003 invoice, I am presented with the invoice data and the imported image as shown here.

Conclusion
What I have shown here is how easy it is to use recurring integration and the AP automation framework to import vendor invoices and related images into D365F&O. With Microsoft Flow the sting has been taken out of the mechanics of managing the files and calling the REST endpoint. What’s not to like?

Like
Report
*This post is locked for comments