web
You’re offline. This is a read only version of the page.
close
Skip to main content
Community site session details

Community site session details

Session Id :

X++ code using getDateDiffWithFallbackToZero of SysComputedColumn to display difference of dates in D365FO.

Chaitanya Golla Profile Picture Chaitanya Golla 17,225

Hi,

In this post will provide the code to view the difference between two dates using getDateDiffWithFallbackToZero of SysComputedColumn in D365FO.

Setup: Please refer this post to know the details of views DAXCustHistoryInvoices, DAXCustHistoryPayments and data entity DAXCustHistoryTransEntity.

X code using SysComputedColumn with an if loop in D365FO - Dynamics 365 Finance Community

Step 1: Created a new method computeColumnDateDiffToZero(namely) on entity DAXCustHistoryTransEntity.

Step 2: In this method, used getDateDiffWithFallbackToZero method of SysComputedColumn to calculate difference between dates transDate and dueDate of data entity DAXCustHistoryTransEntity and if the result is negative then zero will be displayed.

/// 
/// Calculates the days difference between DueDate and TransDate.
/// 
/// 
/// A date difference expression with handling for negative and null values.
/// 
public static str computeColumnDateDiffToZero()
{
    TableName   viewName      = tableStr(DAXCustHistoryTransEntity); //Entity name
    str         dueDateStr    = SysComputedColumn::returnField(viewName, identifierStr(DAXCustHistoryTransView), identifierStr(DueDate));
    str         transDateStr  = SysComputedColumn::returnField(viewName, identifierStr(DAXCustHistoryTransView), identifierStr(TransDate));

    return SysComputedColumn::getDateDiffWithFallbackToZero(dueDateStr, transDateStr, SysComputedColumnDatePart::Day);
}

Step 3: Created a new field DatesDiff on entity DAXCustHistoryTransEntity and assigned the method computeColumnDateDiffToZero to it.

4064.pastedimage1674342074243v1.png

On firing the OData call to the entity, received the below output with column DatesDiff showing the difference between the dates (in days) with negative values as zero.

pastedimage1674342394630v2.png

Comments

*This post is locked for comments