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 SysComputedColumn with an if loop in D365FO

Chaitanya Golla Profile Picture Chaitanya Golla 17,225

Hi,

In this post, we will view the code to use SysComputedColumn with an if loop on an entity.

Setup:

View - DAXCustHistoryInvoices:

Created a view by name "DAXCustHistoryInvoices" to display customer transactions of type Invoices. Included the data source CustTable and CustTrans as shown below.

1373.pastedimage1669609841854v1.png

Created a method by name "computedTransDescription" to display the value of transaction as Invoice.

private static str computedTransDescription()
    {
        return SysComputedColumn::returnLiteral("Invoice");
    }

7532.pastedimage1669612544415v15.png

Created a string unmapped field "TransTypeName" and assigned computedTransDescription to it.

0508.pastedimage1669612637252v16.png

View - DAXCustHistoryPayments:

Created another view by name "DAXCustHistoryPayments" to display customer transactions of type Payments. Included the data source CustTable and CustTrans as shown below.

4857.pastedimage1669609899241v2.png

Created a method by name "computedTransDescription" to display the value of transaction as Payment.

 private static str computedTransDescription()
    {
        return SysComputedColumn::returnLiteral("Payment");
    }

2318.pastedimage1669612909150v17.png

Created a string unmapped field "TransTypeName" and assigned computedTransDescription to it.

3286.pastedimage1669613083421v18.png

Query - DAXCustHistoryQuery:

Created a union query by name "DAXCustHistoryQuery" and included these two views.

7455.pastedimage1669610190394v3.png

View - DAXCustHistoryTransView:

Created a view DAXCustHistoryTransView(namely) and added the "DAXCustHistoryPayments" union query as its datasource.

8666.pastedimage1669610273845v4.png

Entity - DAXCustHistoryTransEntity:

Created an entity DAXCustHistoryTransEntity with the view DAXCustHistoryTransView as its datasource. Propery IsPublic is set to Yes and PublicCollectionName is set as DAXCustHistoryTrans.

7652.pastedimage1669610442291v5.png

SysComputedColumn - If

Step 1: At the entity level created a method "computedAmountDueStr" to display the value of amountdue as zero for payment transactions. If loop operates on the column transtypename. 

Amount Due

private static str computedAmountDueStr()
    {
        TableName   viewName      = tableStr(DAXCustHistoryTransEntity); //Entity name
        str         transTypeName = SysComputedColumn::returnField(viewName, identifierStr(DAXCustHistoryTransView), identifierStr(transTypeName));
        str         amountDue     = SysComputedColumn::returnField(viewName, identifierStr(DAXCustHistoryTransView), identifierStr(AmountDue));

        return SysComputedColumn::if (SysComputedColumn::equalExpression(transTypeName, SysComputedColumn::returnLiteral('Invoice')),
                                      amountDue,
                                      SysComputedColumn::returnLiteral(0));  
    }

Step 2: Created a string unmapped field and named it as AmountDueStr and assigned method computedAmountDueStr to it.

7462.pastedimage1669611788918v10.png

Step 3: At the entity level created a method to display the value of amountpaid as zero for invoice transactions. If loop operates on the column transtypename. 

private static str computedAmountPaidStr()
    {
        TableName   viewName      = tableStr(DAXCustHistoryTransEntity); //Entity name
        str         transTypeName = SysComputedColumn::returnField(viewName, identifierStr(DAXCustHistoryTransView), identifierStr(transTypeName));
        str         amountPaid    = SysComputedColumn::returnField(viewName, identifierStr(DAXCustHistoryTransView), identifierStr(AmountPaid));

        return SysComputedColumn::if (SysComputedColumn::equalExpression(transTypeName, SysComputedColumn::returnLiteral('Payment')),
                                      amountPaid,
                                      SysComputedColumn::returnLiteral(0));
    }

8715.pastedimage1669612116230v12.png

Step 4: Created a string unmapped field and named it as AmountPaidStr and assigned method computedAmountDueStr to it.

pastedimage1669614915741v3.png

Step 5: Build the solution and refreshed the data entity list.

pastedimage1669614847338v2.png

Fired the Odata call to view the data.

D365FO-URL/data/DAXCustHistoryTrans

Output

0871.pastedimage1669614545750v19.png

Thanks,

Chaitanya Golla

Comments

*This post is locked for comments