Skip to main content

Notifications

Announcements

No record found.

Dynamics 365 Community / Forums / Finance forum / How to create a comput...
Finance forum
Suggested answer

How to create a computed field with the value coming from a display method

Posted on by 10
Hi guys,
 
To provide you some context, I have a form AMDeviceTable in which i have displayed a field coming from a display method that was created under the AMDeviceTable table class extension, please see below:
 
// Display method to extract year from Date
    [SysClientCacheDataMethodAttribute(true)]
    display HERoutYear HERoutYear()
    {
        AMDeviceTableMaster deviceTableMaster;
        // Get the current record's
        deviceTableMaster = this.AMDeviceTableMaster();
        // Check if DeRegistrationDate is available
        if (deviceTableMaster.DeRegistrationDate)
        {
            return int2str(year(deviceTableMaster.DeRegistrationDate));
        }
        // If DeRegistrationDate is empty, check EndDate in AMRentDevice
        else if (this.rentalEndDate())
        {
            return int2str(year(this.rentalEndDate()));
        }
        // If both DeRegistrationDate and EndDate are empty, check HerEstimatedRentEndDate in AMDeviceTable
        else if (this.HerEstimatedRentEndDate)
        {
            return int2str(year(this.HerEstimatedRentEndDate));
        }

Additionally, i wanted to expose this field on my data entity AMDeviceOverviewEntity. Therefore, i have created a virtual field and called my display method result under the postLoad() method on the entity (as stated in MS documentation). However, virtual fields do not allow filtering in Excel OData. After some research i found on MS documentation that i should use computed fields instead but i am not able to achive that goal. I tried the below code but i am getting a build error.
 
        str fldMyField = SysComputedColumn::returnField(tableStr(HERDeviceOverviewEntity),
        tableStr(AMDeviceTable), methodStr(HERAMDeviceTableDbt_Extension, HERoutYear));
       
        return fldMyField;
 
Could you please help me fixing the issue?
  • André Arnaud de Calavon Profile Picture
    André Arnaud de Cal... 290,277 Super User 2024 Season 2 on at
    How to create a computed field with the value coming from a display method
    Hi Francisco,
     
    You can't filter on a virtual field if it is filled using the postLoad function. The data is already retrieved from the database before this method is executed. Like mentioned by Martin, you would need to rewrite it to SQL or filter the data using Excel worksheet options after refreshing the data. 
  • FS-16071301-0 Profile Picture
    FS-16071301-0 10 on at
    How to create a computed field with the value coming from a display method
    Thank you so much for the reply, it makes total sense. Since the value was being correctly displayed on the data entity when i used the virtual field approach is there any workaround to make the filtering of this field work in Excel Odata?
  • Suggested answer
    Bharani Preetham Peraka Profile Picture
    Bharani Preetham Pe... 3,538 Moderator on at
    How to create a computed field with the value coming from a display method
    As you are using some functions in x++, this display method cannot be used directly in computed field. I would suggest to write a batch job, create some field and populate that field with batch job code based on this display method and use that in entity.
  • Suggested answer
    Martin Dráb Profile Picture
    Martin Dráb 228,126 Most Valuable Professional on at
    How to create a computed field with the value coming from a display method
    This is impossible. A computed column method is used to generate SQL code; the display method does nothing like that and it can't be called from SQL either.
     
    You'd have to rewrite X++ code of HERoutYear() to SQL and then write a computed column method generating this SQL code.

Helpful resources

Quick Links

Dynamics 365 Community Update – Sep 9th

Welcome to the next edition of the Community Platform Update. This is a weekly…

Announcing Our 2024 Season 2 Super Users!

A new season of Super Users has arrived, and we are so grateful for the daily…

Leaderboard

#1
André Arnaud de Calavon Profile Picture

André Arnaud de Cal... 290,277 Super User 2024 Season 2

#2
Martin Dráb Profile Picture

Martin Dráb 228,126 Most Valuable Professional

#3
nmaenpaa Profile Picture

nmaenpaa 101,148

Leaderboard

Featured topics

Product updates

Dynamics 365 release plans