将 table 数据从一个对象复制到另一个对象 AX 2012

Copy table data from one object to another object AX 2012

我正在尝试做一项工作,该工作用于将期刊名称从一个实体复制并插入到另一个实体。下面的代码只能处理我硬编码的字段。但我正在尝试编写一个将来会有用的代码,即如果我们向 table 添加新的自定义字段。我的代码应该能够复制新添加的自定义字段的数据。

static void sa_CopyData(Args _args)
{
    LedgerJournalName   tblLedgerJournalNameSource, tblLedgerJournalNameDesination;
    NumberSequenceTable tblNST, tblNSTCopy;
    NumberSequenceScope tblNSS;
    Counter             countIN, countOUT;
    DataAreaId          sourceEntity, destinationEntity;

    sourceEntity    = 'CNUS';
    destinationEntity = 'CNEN';
    //Journal Names creation
    changeCompany(sourceEntity)
    {
        tblLedgerJournalNameSource = null;
        while select * from tblLedgerJournalNameSource
            where tblLedgerJournalNameSource.dataAreaId == sourceEntity
        {
            ++countIN;
            tblNSTcopy = null; tblNST = null; tblNSS = null;

            select * from tblNSTcopy
            where tblNSTCopy.RecId == tblLedgerJournalNameSource.NumberSequenceTable;

            changeCompany(destinationEntity)
            {
                tblNST = null; tblNSS = null; tblLedgerJournalNameDesination = null;

                select * from tblNST
                    join tblNSS
                    where tblNST.NumberSequenceScope == tblNSS.RecId
                    && tblNST.NumberSequence == tblNSTCopy.NumberSequence
                    && tblNSS.DataArea == destinationEntity;

                //Insert only if Journal name is not exists
                if(!(LedgerJournalName::find(tblLedgerJournalNameSource.JournalName)))
                {
                    ttsBegin;
                    tblLedgerJournalNameDesination.initValue();
                    tblLedgerJournalNameDesination.JournalName = tblLedgerJournalNameSource.JournalName;
                    tblLedgerJournalNameDesination.Name = tblLedgerJournalNameSource.Name;
                    tblLedgerJournalNameDesination.JournalType = tblLedgerJournalNameSource.JournalType;
                    tblLedgerJournalNameDesination.ApproveActive = tblLedgerJournalNameSource.ApproveActive;
                    tblLedgerJournalNameDesination.ApproveGroupId = tblLedgerJournalNameSource.ApproveGroupId;
                    tblLedgerJournalNameDesination.NoAutoPost = tblLedgerJournalNameSource.NoAutoPost;
                    tblLedgerJournalNameDesination.WorkflowApproval = tblLedgerJournalNameSource.WorkflowApproval;
                    tblLedgerJournalNameDesination.Configuration = tblLedgerJournalNameSource.Configuration;
                    if (!tblNST.RecId)
                    {
                        info(strFmt('Number Sequence is not updated for %1 > Entity %2', tblLedgerJournalNameDesination.JournalName, destinationEntity));
                    }
                    tblLedgerJournalNameDesination.NumberSequenceTable = tblNST.recid;
                    tblLedgerJournalNameDesination.NewVoucher = tblLedgerJournalNameSource.NewVoucher;
                    tblLedgerJournalNameDesination.BlockUserGroupId = tblLedgerJournalNameSource.BlockUserGroupId;
                    tblLedgerJournalNameDesination.FixedOffsetAccount = tblLedgerJournalNameSource.FixedOffsetAccount;
                    tblLedgerJournalNameDesination.OffsetAccountType = tblLedgerJournalNameSource.OffsetAccountType;
                    tblLedgerJournalNameDesination.OffsetLedgerDimension = tblLedgerJournalNameSource.OffsetLedgerDimension;
                    tblLedgerJournalNameDesination.LedgerJournalInclTax = tblLedgerJournalNameSource.LedgerJournalInclTax;
                    tblLedgerJournalNameDesination.insert();
                    ttsCommit;
                    ++countOUT;
                }
            }
        }
    }

    info(strFmt('Journal Names Creation > Read: %1, Inserted: %2', countIN, countOUT));
}

如果您有任何建议,请告诉我。

使用buf2buf(from, to)https://msdn.microsoft.com/en-us/library/global.buf2buf.aspx

执行插入,所以在您执行buf2buf()之后,您可以在执行插入之前修改目标中您想要的任何字段。

您可以 tblLedgerJournalNameDesination.data(tblLedgerJournalNameSource); 但这也会复制您很可能不想要的系统字段。

您还可以查看 Global::buf2Buf(from, to) 背后的源代码,并根据需要修改该逻辑。