1

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 i.e 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.

Sankar R
  • 45
  • 2
  • 7

1 Answers1

1

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.

Alex Kwitny
  • 11,211
  • 2
  • 49
  • 71
  • Thanks for the response Alex. But I can't the change entity from "CNUS" to "CNEN". – Sankar R Jun 06 '17 at 04:59
  • Also, in some cases you can use `y.data(x)` to copy all fields from x to y. I do not recall if this can be used in a cross company scenario like here. – Jan B. Kjeldsen Jun 06 '17 at 06:20
  • @SankarR - Yes you can. You still do `changeCompany()`. See http://fourone.se/blog/2007/11/07/intercompany-with-buf2buf/#more-20 – Alex Kwitny Jun 06 '17 at 14:41