Create PO’s in D365 F&O using PowerApps and Virtual Data Entities

Hello All,

A few months back I wrote a blog about how to create PO’s using PowerApps and the out of the box D365 Finance and Operations power platform connector. (Link here for that blog: https://empowerdynamics365axusers.com/2021/02/03/create-purchase-orders-in-d365-fo-using-power-apps/)

In that blog I had pointed out a few nuisances with the connector and the whole process.

1. My number one complaint was the speed and performance of the creation of PO’s and PO lines. Part of the problem is that the App uses Power Automate to write the data back to D365 F&O.

Recently I decided to re-work the App but this time use Virtual data entities. If you are not familiar with virtual data entities and Dataverse (old CDS) then I will add two links below for blogs that do an amazing job of explaining what these are and how to enable them in your environment.

https://www.powerazure365.com/blog-1/cds-virtual-entities-with-dynamics-365-finance-and-operations

Setting up Virtual Entities for Microsoft Dynamics 365 Finance and Operations in the Common Data Service

In this blog, I will show what virtual data entities you will need to build the App, plus some tips on how to Patch the data back to D365 Finance and Operations.

  1. Which data entities you need to enable in Dataverse?

See the snippet below for a list of the entities you will need

In my example I am using procurement categories to create the PO lines. You will need to enable additional data entities if you are using items instead.

2. Using the Onstart property of the App add the following code

This code below is to grab the data schema of the fields needed to write the PO lines back to the virtual data entities.

ClearCollect(
    colPurchaseordelines,
    ShowColumns(
        Table(Defaults('Purchase order lines V2 (mserp)')),
        "mserp_purchaseordernumber",
        "mserp_procurementproductcategoryname",
        "mserp_linenumber",
        "mserp_purchaseprice",
        "mserp_orderedpurchasequantity",
        "mserp_dataAreaId_id",
        "mserp_lineamount",
        "mserp_purchasepricequantity",
        "mserp_purchaseunitsymbol",
        "mserp_linedescription",
        "mserp_defaultledgerdimensiondisplayvalue"
    )
);

3. Using the ‘Purchase order headers V2 (mserp)’ as the data source, add an edit form to a blank screen

This form will be used to Patch and create the PO header or a blank new purchase order in D365 F&O. On that form add two fields:

mserp_dataAreaId_id – This will be a choices drop down that will allow the user to select which legal entity to create the PO on

The second one is a drop down that has the list of vendors, which will be connected to the Vendors V2 entity

on the items property of the vendors drop down use the following piece to retrieve the vendor code and vendor name.

the items property of the company drop down use the following code

This will take care of the PO header form. the save button will patch the data and create the new PO. Use the last submit function to retrieve the last submitted record information to be used later.

3. Create another screen to allow the user to submit the PO lines of the previously created PO

Add a combo box to allow users to select from the ‘Product categories (mserp)’ entity

I added 3 text input controls to allow the user to enter the amount, QTY and description of the item being purchased

Finally I added combo box controls to allow the user to select from the Financial dimensions in the system. Use the ‘Financial dimension values (mserp)’ data entity to filter the dimensions each combo box will present to the user

Here is where the magic happens, I added a button that will add the user selection to the collection we created on the start of the App. This collection contains the data schema needed to patch the data to the ‘Purchase order lines V2 (mserp)’ entity.

I am using a variable incrementor to add the line numbers. Here is a snippet of the code I used to add the lines to the collection

Set(
    varlinenumber,
    varlinenumber + 1
);
Patch(
    colPurchaseordelines,
    Defaults(colPurchaseordelines),
    {
        mserp_linenumber: varlinenumber,
        mserp_purchaseordernumber: varlastPOnumber.'Purchase order',
        mserp_dataAreaId_id: varlastPOnumber.Company,
        mserp_purchaseunitsymbol: "ea",
        mserp_orderedpurchasequantity: Value(TIQTY.Text),
        mserp_procurementproductcategoryname: TICategory.Selected.'Product category name',
        mserp_purchaseprice: Value(TIAmount.Text),
        mserp_lineamount: Value(TIQTY.Text) * Value(TIAmount.Text),
        mserp_purchasepricequantity: Value(TIQTY.Text),
        mserp_linedescription: TIItemdescription.Text,
        mserp_defaultledgerdimensiondisplayvalue:Concatenate(ddFund.Selected.'Dimension value',"-",ddDepartment.Selected.'Dimension value')
    }
);

Finally, once you have the lines in the collection you can just patch the ‘Purchase order lines V2 (mserp)’ from the collection.

Patch(
    'Purchase order lines V2 (mserp)',
    colPurchaseordelines
);

In conclusion, I was very pleased at the speed in which data is written back to D365 F&O. I feel that virtual data entities is the best way to build Canvas apps for D365 F&O. The flexibility of leveraging Dataverse relational database translates to Virtual entites and makes referencing fields across different tables a lot easier.

Leave a Reply

Fill in your details below or click an icon to log in:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out /  Change )

Facebook photo

You are commenting using your Facebook account. Log Out /  Change )

Connecting to %s