Create Sales Orders through code X++ (AX2012)

Useful code below for creating sales order quickly for testing purposes in scenarios where we have to test bulk sales orders and than do some packing slips and or invoice consolidation…

static void Job_CreateSalesOrderForTesting(Args _args)
{
    SalesId salesId;
    int counter = 0;
    container saleOrdersCon;
    str salesOrders;
    int size = 100;

    void createSalesTable(CustAccount _custAccount)
    {
        SalesTable salesTable;
        NumberSeq NumberSeq;
        NumberSeq = NumberSeq::newGetNum(SalesParameters::numRefSalesId());
        NumberSeq.used();
        salesTable.SalesId = NumberSeq.num();
        salesTable.initValue();
        salesTable.CustAccount = _custAccount;
        salesTable.initFromCustTable();
        salesTable.insert();
        salesId = salesTable.SalesId;
    }

    void createSalesLine(SalesId _salesId, ItemId _itemId, str 50 _inventColorId, str 10 _inventSizeId, str 50 _configId, str 50 _inventLocationId)
    {
        InventDim inventDim;
        SalesLine salesLine;

        select firstOnly inventDim
        where
            inventDim.InventColorId == _inventColorId
            && inventDim.InventSizeId == _inventSizeId
            && inventDim.configId == _configId
            && inventDim.InventLocationId == _inventLocationId;

        if (inventDim){
            salesLine.clear();
            salesLine.SalesId = _salesId;
            salesLine.ItemId = _itemId;
            salesLine.InventDimId = inventDim.inventDimId;
            salesLine.SalesQty = 2; // You can add parameter for this as well
            salesLine.createLine
            (NoYes::Yes, // Validate
            NoYes::Yes, // initFromSalesTable
            NoYes::Yes, // initFromInventTable
            NoYes::Yes, // calcInventQty
            NoYes::Yes, // searchMarkup
            NoYes::Yes); // searchPrice
        }
    }


    for (counter = 0; counter < size; counter++){
        createSalesTable('CUS10580');
        createSalesLine(salesId, '000023000', 'RED', 'L', 'CONFIG', '009');
        createSalesLine(salesId, '000024MMI', 'BLACK', 'M', 'CONFIG', '009');
    }
}

Leave a Reply

Fill in your details below or click an icon to log in:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out /  Change )

Google photo

You are commenting using your Google account. Log Out /  Change )

Twitter picture

You are commenting using your Twitter account. Log Out /  Change )

Facebook photo

You are commenting using your Facebook account. Log Out /  Change )

Connecting to %s

This site uses Akismet to reduce spam. Learn how your comment data is processed.