简体   繁体   中英

Copy table data from one object to another object AX 2012

I'm trying to do a Job which used to Copy and insert Journal Names from one entity to another entity. Below code able to handle only fields only I hardcoded. But I'm trying to do a code which will be useful in future ie if we add new custom field to the table. My code should able to copy data of that newly custom field added.

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));
}

Please let me know if you have any suggestions.

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

It does not perform an insert, so after you do buf2buf() you can modify any fields you want in the target before doing an insert.

You could do tblLedgerJournalNameDesination.data(tblLedgerJournalNameSource); but that also copies system fields, which you most likely do not want.

You can also look at the source code behind Global::buf2Buf(from, to) and modify that logic if you want.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM