[AX]AX2012开发新特性添加计算字段到视图

AX2012中视图可以有计算字段,这里根据MSDN上的演示来看看如何添加计算字段到视图。

首先在AOT中创建视图名为TestCompColView,把CustTable表拖入到视图的DataSource中,可以看到名为CustTable_1的节点,把CustTable_1节点下的AccountNum和SubsgementId拖入到视图的字段中。

创建计算字段的第一步是要定义一个静态方法,比如这里的 :

private static server str compColSubsegAcctMethod()
{
    #define.ViewName(TestCompColView)
    #define.DataSourceName("CustTable_1")
    #define.FieldSubsegmentId("SubsegmentId")
    #define.FieldAccountNum("AccountNum")
    str sReturn,
        sAccountNum,
        sSubsegmentId;
    DictView dictView2;

    // Construct a DictView object for the present view.
    dictView2 = new DictView(tableNum(#ViewName));

    // Get a string that has the target field name
    // propertly qualified with an alias (such
    // as "A." or "B.").
    sAccountNum = dictView2.computedColumnString
        (#DataSourceName,
        #FieldAccountNum,
        FieldNameGenerationMode::FieldList,
        true);

    sSubsegmentId = dictView2.computedColumnString
        (#DataSourceName,
        #FieldSubsegmentId,
        FieldNameGenerationMode::FieldList,
        true);

    sReturn = "substring("
        + sSubsegmentId
        + ",1,1) + ' - ' + "
        + sAccountNum;

    // Helpful confirming or diagnostic information.
    info(sAccountNum);
    info(sSubsegmentId);
    info(sReturn);

    return sReturn;
}

这里使用了DictView.computedColumnString来获取在SQL语句中字段的表述名称,有了静态方法就可以在AOT中视图的字段下创建一个String Computed Column类型的字段,命名为compCol_Subseg_Acct,将其ViewMethod 属性设置为前面的静态方法compColSubsegAcctMethod,这样就完成了计算字段的创建。

那么计算字段是如何运作的呢?我们在AOT中保存视图时,方法compColSubsegAcctMethod被调用,视图从数据库同步数据,也仅在视图从数据库同步数据的时候被调用,后续对View的数据读取操作不会在调用函数,来看看这个函数的返回值:

substring(A.SUBSEGMENTID,1,1) + ' - ' + A.ACCOUNTNUM

在SQL跟踪窗口可以看到完整的SQL语句是:

CREATE VIEW [dbo].[TESTCOMPCOLVIEW]
AS
    SELECT
        A.SUBSEGMENTID AS SUBSEGMENTID,
        A.ACCOUNTNUM AS ACCOUNTNUM,
        A.DATAAREAID AS DATAAREAID,
        A.RECID AS RECID,
        (CAST
            (
                (substring(A.SUBSEGMENTID,1,1)
                + ' - '
                + A.ACCOUNTNUM
                )
                    AS
                    NVARCHAR(32)
            )
        )
            AS COMPCOL_SUBSEG_ACCT
    FROM
        CUSTTABLE A

可以看到前面函数的返回值被强化为:

(CAST
            (
                (substring(A.SUBSEGMENTID,1,1)
                + ' - '
                + A.ACCOUNTNUM
                )
                    AS
                    NVARCHAR(32)
            )
        )
            AS COMPCOL_SUBSEG_ACCT

在AOT中打开视图就能看到相应的数据了。

 更多内容参看MSDN:http://msdn.microsoft.com/EN-US/library/gg845841

原文地址:https://www.cnblogs.com/duanshuiliu/p/2637847.html