Skip to main content

Notifications

Announcements

No record found.

How to create a Computed method in a View or in Data Entity in D365FO using X++

Bharani Preetham Peraka Profile Picture Bharani Preetham Pe... 3,526 Super User
Today we will learn about computed methods in a View or a Data Entity.

As the name suggests the computed methods are computed in SQL level and adds the query directly to the View/Data entity's metadata. A computed method generates/returns a SQL query which is directly added to the View metadata.

This doesn't mean that whenever a view is executed, the computed method executes for each line. The computed method executes when the view is loaded and in SQL level. So that if any unmapped field is using this, then we can run a select in SSMS to check that field's value. This is completely different with the normal display method which is executed at runtime in X++. So that's why whenever we need to use display methods, for better performance, this is one of the ways to do instead of a display method.

So let us understand how to write a computed method or a computed column or a SysComputedColumn method.

public static server str getVendorAccountNum()
{
    str vendorAccount;

    str voucherNum = SysComputedColumn::returnField(viewStr(OURVIEWNAME),   //Pass View name in 'OURVIEWNAME'
        identifierStr(GeneralJournalEntry),                                 //Pass DataSource name
        fieldStr(GeneralJournalEntry, SubLedgerVoucher));                   //Pass the field name on which we are interested

    str dataArea = SysComputedColumn::returnField(viewStr(OURVIEWNAME),
        identifierStr(GeneralJournalEntry),
        fieldStr(GeneralJournalEntry, SubLedgerVoucherDataAreaId));


    vendorAccount = strFmt(@'SELECT TOP 1
                                  VENDTRANS.ACCOUNTNUM
                             FROM VENDTRANS
                             WHERE
                                  VENDTRANS.VOUCHER = %1
                                  AND VENDTRANS.DATAAREAID = %2,
                             voucherNum,
                             dataArea);

    return vendorAccount;

}

Above is a simple example on how to write a computed method. Many complex queries can be written this way.

A computed method returns a SQL query in string format which is then used by any field in that view/entity. Irrespective of the datatype, the computed method's return type is always a string. So, from above we are fetching the vendor account from a computed method. 

For creating a computed field, an unmapped field should be created in the view and make sure that computed column property is enabled at the properties level and add this method as a view method.

In entities also where we have read-only fields and if data is being populated from postload() method, then using a computed column instead improves the performance a lot.

This way we can create a computed method.

Comments