Create Return Order using X++

Return Order header:
SalesTable createReturnOrderHeader(
    CustAccount          _custAcct,
    ReturnReasonCodeId   _returnReasonCodeId,
    str                  _emplId    = '',
    SalesId              _salesId   = '',
    SalesId              _ROSalesId = '')
{
    SalesTable              prevSalesTable,
                            newSalesTable,
                            ROSalesTable;
    SalesParameters         salesParameters;
    ReturnPeriodOfValidity  periodofValid;

    // create the sales order header for the return order
    salesParameters = SalesParameters::find();
    periodofValid   = salesParameters.ReturnPeriodOfValidity;
    ROSalesTable    = SalesTable::find(_ROSalesId);
    if(!ROSalesTable)
    {
        // Initialize from Invoiced SO
        if(_salesId)
        {
            prevSalesTable                        = SalesTable::find(_salesId);
            newSalesTable.initReturnFromSalesTable(prevSalesTable);
            newSalesTable.CustAccount             = prevSalesTable.CustAccount;
        }
        // Create new Return Order
        else
        {
            newSalesTable.CustAccount             = _custAcct;
            newSalesTable.SalesId                 = NumberSeq::newGetNum(SalesParameters::numRefSalesId()).num();
            newSalesTable.ReturnItemNum           = NumberSeq::newGetNum(SalesParameters::numRefReturnItemNum()).num();
            newSalesTable.SalesType               = SalesType::ReturnItem;
            newSalesTable.ReturnStatus            = ReturnStatusHeader::Created;
            newSalesTable.DeliveryDateControlType = SalesDeliveryDateControlType::None;
            newSalesTable.ShippingDateRequested   = today() + periodofValid;
        }
        newSalesTable.initFromCustTable();
        // periodofValid derive from Sales Parameter
        if(periodofValid)
        {
            newSalesTable.ReturnDeadline      = today() + periodofValid;
        }
        else
        {
            newSalesTable.ReturnDeadline      = today();
        }
        //Reason Code
        newSalesTable.ReturnReasonCodeId      = _returnReasonCodeId;
        newSalesTable.SalesType               = SalesType::ReturnItem;
        if (newSalesTable.validateWrite())
        {
            newSalesTable.insert();
            // return the new Return Order
            return newSalesTable;
        }
    }
    //return the existing RO
    return ROSalesTable;
}

Return Order lines:
public void createReturnOrderLine(
    ItemId                            _itemId,
    Qty                               _qty,
    SalesTable                        _returnOrder,
    SalesId                           _originalSalesId= '',
    EcoResItemSizeName                _inventSizeId   = '',
    EcoResItemColorName               _inventColorId  = '',
    EcoResItemConfigurationName       _configId       = '',
    EcoResItemStyleName               _inventStyleId  = '',
    InventBatchID                     _inventBatchId  = '',
    InventSerialId                    _inventSerialId = ''
)
{
    CustInvoiceTrans    custInvoiceTrans;
    SalesLine           salesLine;
    CustInvoiceJour     custInvoiceJour;
    InventDim           inventDim,
                        pvInventDim;

    ttsbegin;
    if(_originalSalesId)
    {
        // Need to populate all the necessary fields for the new salesline
        // using the existing invoice and the new sales order
        select custInvoiceJour
            where custInvoiceJour.RefNum   == RefNum::SalesOrder
                &&custInvoiceJour.SalesId  == _originalSalesId;

        if(custInvoiceJour)
        {
            select * from custInvoiceTrans
                where  custInvoiceTrans.SalesId             == custInvoiceJour.SalesId
                    && custInvoiceTrans.InvoiceId           == custInvoiceJour.InvoiceId
                    && custInvoiceTrans.ItemId              == _itemid
                    && custInvoiceTrans.InvoiceDate         == custInvoiceJour.InvoiceDate
                    && custInvoiceTrans.numberSequenceGroup == custInvoiceJour.numberSequenceGroup
                join inventDim
                    where inventDim.inventDimId == custInvoiceTrans.InventDimId
                    && ((_inventBatchId     == '')  || (inventDim.inventBatchId  == _inventBatchId))
                    && ((_inventSerialId    == '')  || (inventDim.inventSerialId == _inventSerialId))
                    && ((_configId          == '')  || (inventDim.configId      == _configId))
                    && ((_inventColorId     == '')  || (inventDim.InventColorId == _inventColorId))
                    && ((_inventSizeid      == '')  || (inventDim.InventSizeId  == _inventSizeid))
                    && ((_inventStyleId     == '')  || (inventDim.InventStyleId == _inventStyleId));
            
            salesLine.initFromCustInvoiceTrans(custInvoiceTrans);
            salesLine.InventTransIdReturn   = custInvoiceTrans.InventTransId;
            salesLine.InventDimId           = custInvoiceTrans.InventDimId;
        }

    }
    salesLine.initFromSalesTable(_returnOrder);
    salesLine.ItemId        = _itemId;
    salesLine.SalesUnit     = InventTableModule::find(_itemId, ModuleInventPurchSales::Invent).UnitId;
    salesLine.LineAmount    = salesLine.returnLineAmount();
    salesLine.SalesQty      = 0;
    
    // find inventDim
    inventDim.data(InventDim::find(salesLine.InventDimId));
    inventDim.InventSizeId  = _inventSizeId;
    inventDim.InventColorId = _inventColorId;
    inventDim.configId      = _configId;
    inventDim.InventStyleId = _inventStyleId;
    inventDim.inventBatchId = _inventBatchId;
    inventDim.inventSerialId= _inventSerialId;
    pvInventDim             = inventDim::findOrCreate(inventDim);
    salesLine.InventDimId   = pvInventDim.inventDimId;
    salesLine.createLine(true, false, true, false, false, false, false, false);
    salesLine.ExpectedRetQty= -_qty;
    salesLine.update();
    ttscommit;
}
To find original SalesId if return order lines exist:
            select firstOnly RecId, ReferenceId from inventTransOrigin
                where inventTransOrigin.InventTransId       == tradeInventTransIdReturn
                    &&inventTransOrigin.ReferenceCategory   == InventTransType::Sales;
            if (inventTransOrigin)
            {
                originalSalesId = inventTransOrigin.ReferenceId;
            }


 

Search

About

DaxOnline.org is free platform that allows you to quickly store and reuse snippets, notes, articles related to Dynamics AX.

Authors are allowed to set their own AdSense units.
Join us.

Blog Tags