0

我正在嘗試執行一個用於將Journal名稱從一個實體複製並插入到另一個實體的作業。下面的代碼只能處理我硬編碼的字段。但是我正在試着做一個代碼,這個代碼在將來也是有用的,也就是說,如果我們在表中添加新的自定義字段。我的代碼應該能夠複製新添加的自定義字段的數據。將表格數據從一個對象複製到另一個對象AX 2012

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

如果您有任何建議,請讓我知道。

回答

1

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

它確實執行插入,這樣你就buf2buf()後,你可以做一個插入之前修改你的目標所需的任何領域。

你可以做tblLedgerJournalNameDesination.data(tblLedgerJournalNameSource);但它也複製系統字段,你很可能不想要。

您還可以查看Global::buf2Buf(from, to)背後的源代碼,並根據需要修改該邏輯。

+0

感謝您的答覆亞歷克斯。但我不能將變化實體從「CNUS」改爲「CNEN」。 –

+0

此外,在某些情況下,您可以使用'y.data(x)'將所有字段從x複製到y。我不記得這是否可以用於像這裏這樣的跨公司場景。 –

+0

@SankarR - 是的,你可以。你仍然在做'changeCompany()'。請參閱http://fourone.se/blog/2007/11/07/intercompany-with-buf2buf/#more-20 –

相關問題