Thursday, June 27, 2013

AX 2012 Post Partial Sales Order using x++ code

In Dynamics AX 2012, we often need to post Sales Order using x++ code. It is very easy to post complete sales order using x++ code by utilizing the SalesFormLetter class. However, what if we need to post the sales order partially and not all the lines.

We have a chooseLinesQuery() method available in SalesFormLetter class that accepts a query object to post only the lines you want to post. See example below.

    SalesTable salesTable;
    SalesFormLetter salesFormLetter;
    Query query;
    QueryRun queryRun;
    QueryBuildDataSource qbds;

    query = new Query(QueryStr(SalesUpdatePackingSlip));
    qbds = query.dataSourceTable(tableNum(SalesLine));

    // Build query range to find those lines which needs to be posted.
    qbds.addRange(fieldNum(SalesLine, mzkBillingDate)).value(queryValue(systemDateGet()));
    qbds.addRange(fieldNum(SalesLine, SalesStatus)).value(queryValue(SalesStatus::Backorder));
    queryRun = new queryRun(query);

    salesFormLetter = SalesFormLetter::construct(DocumentStatus::Invoice);
    salesFormLetter.chooseLinesQuery(queryRun);
    salesFormLetter.update(salesTable);