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);


7 comments:

  1. It will filter the sales line...partial also includes the quantity, how you will invoice the partial quantity of a particular line

    ReplyDelete
  2. Hi asaraf,

    I am not sure about it. If you have any idea about that please post below

    ReplyDelete
  3. somewhat I tried...but here I write one code..its not fully working...if you can please modify my code...
    SalesFormLetter salesFormLetter;
    SalesParmUpdate salesParmUpdate;
    SalesParmLine salesParmLine;
    ;
    salesParmUpdate::deleteWithoutUpdate(_salesID);
    salesFormLetter = SalesFormLetter::construct( DocumentStatus::Invoice,classStr(FormletterService),identifierStr(postSalesOrderInvoice));
    salesFormLetter.transDate(systemDateGet());
    salesFormLetter.initParmSalesTable(SalesTable::find(_salesID));
    salesFormLetter.parmSpecQtyIsSet(true);
    salesFormLetter.init();

    salesParmUpdate.ParmId = _salesID;
    salesParmUpdate.DocumentStatus = DocumentStatus::Invoice;
    salesParmUpdate.SpecQty = SalesUpdate::All;
    salesParmUpdate.Storno = 1;
    salesparmUpdate.insert();


    salesFormLetter.salesParmUpdate(salesParmUpdate);
    salesFormLetter.chooseLines(true);
    ttsBegin;
    select forUpdate salesParmLine order by salesParmLine.RecId desc where salesParmLine.SalesLineRecId == _salesLineRecid;
    salesParmLine.DeliverNow = _qty;
    salesParmLine.modifiedField(fieldNum(salesParmLine,DeliverNow));
    salesParmLine.update();
    ttsCommit;
    try
    {
    salesFormLetter.run();
    return true;
    }

    catch
    {
    salesParmUpdate::deleteWithoutUpdate(_salesID);
    return false;
    }
    return false;

    ReplyDelete
  4. Hi,
    could you please suggest something for PO, for invoicing selected lines.
    Thanks

    ReplyDelete