Creating Purchase Orders in X++

I’ve developed a few integrations that create purchase orders in AX 2009. To handle the AX end of the purchase order creation I created a static class with two methods, ‘CreatePo()’ and ‘CreatePurchLine()’.

I use these methods with the AX Business Connector, and also utilize it in some batch jobs as well.

CreatePo() returns a string, the PurchId of the newly created purchase order, and for parameters take the vendor account number, delivery date, and optionally an ID value of type ‘int’.

CreatePurchLine() returns a string, and for parameters takes a list of values, the first of which being the PurchId of the header returned from CreatePo().

CreatePo():


static PurchId CreatePo(AccountNum _vend, Date _dlv, int _webid = 0)
{
    PurchTable      purchTable;
    VendTable       vendTable = VendTable::find(_vend);
    AxPurchTable    axPurchTable;

    VendRef         vref;
    VendName        dlvname = "Name of Delivery Method"; // based on your company
    ;

    vref = int2str(_webid);

    //Create Purchase order
    purchTable.initFromVendTable(vendTable);

    axPurchTable = axPurchTable::newPurchTable(purchTable);
    axPurchTable.parmPurchaseType(PurchaseType::Purch);
    axPurchTable.parmDocumentStatus(DocumentStatus::PurchaseOrder);
    //axPurchTable.parmAccountingDate(systemDateGet());
    axPurchTable.parmDeliveryDate(_dlv);
    axPurchTable.parmDeliveryName(dlvname);
    axPurchTable.parmPurchStatus(PurchStatus::Backorder);
    axPurchTable.parmInventLocationId(vendTable.InventLocation);
    axPurchTable.parmInventSiteId(vendTable.InventSiteId);
    axPurchTable.parmVendorRef(vref);
    axPurchTable.doSave();

    return strFmt('%1',purchTable.PurchId);
}

CreatePurchLine():


static str CreatePurchLine(PurchId _purchId, ItemId _itemId, Qty _qty, UnitId _uom, InventLocationId _whs)
{
    PurchTable          purchTable;
    PurchLine           purchLine;
    AxPurchLine         axPurchLine;
    InventDim           inventDim;
    UnitConvert         uc;
    UnitId              lb = "LB";
    UnitId              cs = "CS";

    InventTableModule   ivtm;
    PriceDiscTable      price;
    PriceDiscItemRelation        item;
    PriceDiscAccountRelation     acct;
    ;

    purchTable = PurchTable::find(_purchId);

    if (!purchTable.RecId)
    {
        return "ERROR";
    }

    // populate acct and item relations
    acct = purchTable.OrderAccount;
    item = _itemId;

    //Create PurchLine
    purchLine.initFromPurchTable(purchTable);

    axPurchLine = AxPurchLine::newPurchLine(purchLine);
    axpurchLine.parmItemId(_itemId);
    inventDim.InventLocationId = _whs;
    inventDim.InventSiteId = InventLocation::find(_whs).InventSiteId;
    inventdim = InventDim::findOrCreate(inventDim);
    axPurchLine.parmInventDimId(inventDim.inventDimId);

    if (pdsIsCWItem(_itemId))
    {
        uc = UnitConvert::find(_itemId, lb, cs);
        axPurchLine.parmPdsCWQty(_qty);
        axPurchLine.parmPdsCWPurchUnit(_uom);
        axPurchLine.parmPurchQty(_qty * uc.Factor);
        axPurchLine.parmPurchUnit(lb);

        ivtm = InventTableModule::find(_itemId, ModuleInventPurchSales::Invent);
    }
    else
    {
        axPurchLine.parmPurchQty(_qty);
        axPurchLine.parmPurchUnit(_uom);

        ivtm = InventTableModule::find(_itemId, ModuleInventPurchSales::Purch);
    }

    select firstonly price
        where price.AccountCode == TableGroupAll::Table
        && price.ItemCode == TableGroupAll::Table
        && price.ItemRelation == item
        && price.AccountRelation == acct;

    if (price.RecId)
    {
        axPurchLine.parmPurchPrice(price.Amount);
    }
    else
    {
        axPurchLine.parmPurchPrice(ivtm.Price);
    }

    axPurchLine.doSave();

    return "1";
}