Previously, we looked at how to create and train AI models using AI Builder in Power Apps in these three posts:
- AP Automation using AI Builder in the Power Platform
- Using Category Classification AI to Identify the General Ledger Account
- Using a Power Apps app with AI Builder Models to Capture a Vendor Invoice
In this post we will take a look at the mechanics behind how the AI models are used in the app.
As you may recall from my previous post, the user was guided to a screen where he or she could take a picture of an invoice for processing. The following screenshot shows this screen in edit mode in the Power Apps editor.
As you can see, at the top of the screen, I have inserted a Form processor AI Builder control. This is done simply by going to the Insert menu in the ribbon and selecting Form processor from the AI Builder drop-down. You are then presented with a selection box where you can select from existing AI Builder models (or create a new one).
In the properties pane on the right-hand side, you can see the various options available for the control. This is all we need to do to embed the form processor AI Builder model we created earlier into the app.
Now we need to react to the result once the user has uploaded an invoice image and the image has been processed. We do this by writing some code in the OnChange method.
Before we start looking at the code, here is some important information:
- The form processor control is named AIInvoice.
- You refer to field on the invoice using AIInvoice.Fields.[Field].
- Instead of using the category classification AI Builder model I created earlier, I am actually using the underlying Azure cognitive service directly because when I built the app, it was not possible to access the model programmatically.
- The Azure text analysis service has been added to my data sources and is named TextAnalytics.
- In CDS I have created a table called TextToAccounts when I match key phrases with ledger accounts.
Now, let’s take a look at the code in the OnChange method:
// This statement sets the value of the PostingText variable to the description found on the invoice.
// This statement creates a new collection and collects key phrases from the invoice description using text analysis.
Set(CostAccount,LookUp(TextToAccounts,Text = AccountTag).Account);
// The first statement sets the AccountTag variable to the first tag found in the collection. Based on this tag, the CostAccount variable is set to the ledger account value found by looking up in the TextToAccounts table in Common Data Service (CDS).
// The captured image is stored in a new collection.
// The VendAccount variable is set to the captured vendor account from the invoice.
As you can read from this short article, it is now possible (in preview) to use an AI Builder model programmatically within a Power App. This means that instead of using the Azure text analysis service, I would have been able to use a model built in AI Builder instead. A topic for a future blog post…
Now we have combined the form processor results with the text analysis service from Azure to provide us with the information we need to submit the invoice to Dynamics 365 Finance.
We will look at how this is done using Power Automate in the next and final post in this series.