Export purchase order lines to Excel using X++ in AX 2012

 Here is the sample code to export all the purchase order lines details to an excel file, we can use the following method.


Public int getPurchaseLine()

{

    PurchLine               purchLine;

    InventDim               inventDim;

    #File

    CommaTextIo             commaTextIo;

    FileIOPermission        permission;

    int                     lineCount;

    PurchTable              purhTable;

    PurchDeliverySchedule   purchDeliverySchedule;

    ;


    permission = new FileIOPermission(fileName,#io_write);

    permission.assert();

    commaTextIo = new CommaTextIo(fileName,#io_write);

    commaTextIo.outFieldDelimiter(';');

    commaTextIo.write("@SYS15812","@sys312849","@SYS12836","@SYS5398","@RET3637","@SYS73726","@SYS103211","@SYS6437","@SYS14578",

                    "@SYS190813","@SYS16056","@SYS4006394");


    While Select * from purchLine

        Where purchLine.PurchStatus             == PurchStatus::Backorder   &&

              purchLine.ItemId                  != ''                       &&

              purchLine.PurchQty                !=  0                       &&

              purchLine.LineDeliveryType        != LineDeliveryType::OrderLineWithMultipleDeliveries

    join purhTable

        Where purhTable.PurchId                 == purchLine.PurchId

    join * from inventDim

        Where inventDim.inventDimId == purchLine.InventDimId

    notExists Join purchDeliverySchedule

       where purchDeliverySchedule.OrderLine   == purchLine.InventTransId


    {

        if ( purhTable.DocumentState           == VersioningDocumentState::Draft       ||

             purhTable.DocumentState           == VersioningDocumentState::Confirmed   ||

             purhTable.DocumentState           == VersioningDocumentState::Approved )

        {


            commaTextIo.write(purchLine.PurchId,purchLine.LineNumber,purchLine.ItemId,purchLine.itemName(),purchLine.VariantId,inventDim.InventColorId

                        ,inventDim.InventSiteId,inventDim.InventLocationId,purchLine.PurchQty,purchLine.PurchUnit,purchLine.DeliveryDate,purchLine.ConfirmedDlv);

            lineCount++;

        }


    }

    CodeAccessPermission::revertAssert();


    return lineCount;

}

No comments:

Post a Comment