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.
Created a method by name "computedTransDescription" to display the value of transaction as Invoice.
private static str computedTransDescription()
{
return SysComputedColumn::returnLiteral("Invoice");
}
Created a string unmapped field "TransTypeName" and assigned computedTransDescription to it.
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.
Created a method by name "computedTransDescription" to display the value of transaction as Payment.
private static str computedTransDescription()
{
return SysComputedColumn::returnLiteral("Payment");
}
Created a string unmapped field "TransTypeName" and assigned computedTransDescription to it.
Query - DAXCustHistoryQuery:
Created a union query by name "DAXCustHistoryQuery" and included these two views.
View - DAXCustHistoryTransView:
Created a view DAXCustHistoryTransView(namely) and added the "DAXCustHistoryPayments" union query as its datasource.
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.
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.
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));
}
Step 4: Created a string unmapped field and named it as AmountPaidStr and assigned method computedAmountDueStr to it.
Step 5: Build the solution and refreshed the data entity list.
Fired the Odata call to view the data.
D365FO-URL/data/DAXCustHistoryTrans
Output
Thanks,
Chaitanya Golla

Like
Report
*This post is locked for comments