How to: Copy a sales order using SalesCopying class

There are a million code snippets that show you how to create/copy a sales order that work 80% of the time, but often don't account for the many different scenarios from different environments.


This job shows you how to copy a sales order, but more importantly it shows you how to use the SalesCopying class so that you can copy from SalesQuotes, Journals, etc.

static void JobCopySO(Args _args)
{
    SalesTable  salesTable = SalesTable::find('SO000777');
    SalesLine   salesLine;
    SalesTable  salesTableNew;
    SalesOrderCopyingContract contract = SalesOrderCopyingContract::newIsCreditNote(false);
    
    SalesCopying            salesCopying;
    TmpFrmVirtual           tmpFrmVirtualLines;
    TmpFrmVirtual           tmpFrmVirtualHeader;
    
    void writeTmpFrmVirtual(TmpFrmVirtual _tmpFrmVirtual, TableId _tableId, RecId _recId, Num _id, LineNum _lineNum = 0, TransDate _transDate = systemDateGet(), Qty _qty = 0)
    {
        _tmpFrmVirtual.clear();
        _tmpFrmVirtual.TableNum     = _tableId;
        _tmpFrmVirtual.RecordNo     = _recId;
        _tmpFrmVirtual.Id           = _id;
        _tmpFrmVirtual.LineNum      = _lineNum;
        _tmpFrmVirtual.TransDate    = _transDate;
        _tmpFrmVirtual.Qty          = _qty;

        _tmpFrmVirtual.write();
    }
    
    // Create your new sales header
    salesTableNew.SalesId = NumberSeq::newGetNum(SalesParameters::numRefSalesId()).num();
    salesTableNew.initValue();
    salesTableNew.CustAccount = salesTable.CustAccount;
    salesTableNew.initFromCustTable();
    salesTableNew.insert();
    
    // Build header virtual
    writeTmpFrmVirtual(tmpFrmVirtualHeader, salesTable.TableId, salesTable.RecId, salesTable.SalesId);
    
    while select salesLine
        where salesLine.SalesId == salesTable.SalesId
    {
        writeTmpFrmVirtual(tmpFrmVirtualLines, salesLine.TableId, salesLine.RecId, salesLine.SalesId, salesLine.LineNum, systemDateGet(), salesLine.SalesQty);
    }
    
    contract.parmSalesPurchCopy(SalesPurchCopy::CopyAllHeader);
    contract.parmCallingTableSalesId(salesTableNew.SalesId);
    contract.parmTmpFrmVirtualLines(tmpFrmVirtualLines);
    contract.parmTmpFrmVirtualHeader(tmpFrmVirtualHeader);
    contract.parmQtyFactor(1);
    contract.parmRecalculateAmount(NoYes::No);
    contract.parmReverseSign(NoYes::No);
    contract.parmCopyMarkup(NoYes::No);
    contract.parmCopyPrecisely(NoYes::No);
    contract.parmDeleteLines(NoYes::Yes);    
    
    SalesCopying::copyServer(contract.pack(), false);
       
    info(strFmt("Created %1", salesTableNew.SalesId));
}

https://www.alexondax.com/2014/07/how-to-copy-sales-order-using.html

    Danzel
    Danzel管理员

    • 声明:本文由Danzel于2021-09-22转载(优化),转载须经原站同意并注明出处。
    • 本文地址:http://maryd.cn/?id=197
    上一篇:通过员工获取邮箱
    下一篇:Send email with attached putting in Dynamics Ax queue By code X++

    留言评论

    暂无留言
    取消
    扫码支持