AX 2012: Correct product receipt journal in X++

Purpose:

The purpose of this document is to illustrate how we can correct a posted product receipt in X++ using PurchParmTable and PurchParmLine tables.

Business requirement:

Ability to correct product receipt journal automatically. As of now Standard AX offers manual product receipt correction by clicking Procurement and sourcing > Inquiries > Journals > Product receipt > Correct.

Assumptions:

Product receipt is posted.

Development:

Please find below the job to correct product receipt journal in X++

// Developed on 28 Dec 2015 by Muhammad Anas Khan
// Blog: dynamicsaxinsight.wordpress.com
// LinkedIn: pk.linkedin.com/in/muhammadanaskhan
// Description: Ability to correct product receipt journal
static void makPurchPackingSlipCorrect(Args _args)
{
    PurchFormLetter         purchFormLetter;
    PurchFormletterParmData purchFormLetterParmData;
    PurchParmUpdate         purchParmUpdate;
    PurchParmTable          purchParmTable;
    PurchParmLine           purchParmLine;
    PurchTable              purchTable;
    PurchLine               purchLine;
    PurchId                 purchId;
    Num                     packingSlipId;
    VendPackingSlipJour     vendPackingSlipJour;
    VendPackingSlipTrans    vendPackingSlipTrans;

    purchId       = "0000-000187";
    packingSlipId = "MAK3101";

    purchTable = PurchTable::find(purchId);

    select firstOnly vendPackingSlipJour
        where vendPackingSlipJour.PurchId == purchId
            && vendPackingSlipJour.PackingSlipId == packingSlipId;

    ttsBegin;

    // Instantiate PurchFormLetterParmData
    purchFormLetterParmData = PurchFormletterParmData::newData(
        DocumentStatus::PackingSlip,
        VersioningUpdateType::Correction);

    purchFormLetterParmData.parmOnlyCreateParmUpdate(true);
    purchFormLetterParmData.createData(false);
    purchParmUpdate = purchFormLetterParmData.parmParmUpdate();

    // Set PurchParmTable table
    purchParmTable.clear();
    purchParmTable.TransDate             = SystemDateGet();
    purchParmTable.Ordering              = DocumentStatus::PackingSlip;
    purchParmTable.ParmJobStatus         = ParmJobStatus::Waiting;
    purchParmTable.ParmId                = purchParmUpdate.ParmId;
    purchParmTable.Num                   = packingSlipId;
    purchParmTable.ReCalculate           = true;
    purchParmTable.PurchId               = purchTable.PurchId;
    purchParmTable.PurchName             = purchTable.PurchName;
    purchParmTable.DeliveryName          = purchTable.DeliveryName;
    purchParmTable.OrderAccount          = purchTable.OrderAccount;
    purchParmTable.InvoiceAccount        = purchTable.InvoiceAccount;
    purchParmTable.CurrencyCode          = purchTable.CurrencyCode;
    purchParmTable.DeliveryPostalAddress = purchTable.DeliveryPostalAddress;
    purchParmTable.VendPackingSlipJour   = vendPackingSlipJour.RecId;
    purchParmTable.insert();

    // Set PurchParmLine table
    while select purchLine
        where purchLine.PurchId == purchTable.purchId
    {
        select firstOnly vendPackingSlipTrans
            where vendPackingSlipTrans.OrigPurchid == purchLine.PurchId
                && vendPackingSlipTrans.PurchaseLineLineNumber == purchLine.LineNumber;

        purchParmLine.ParmId = purchParmTable.ParmId;
        purchParmLine.TableRefId = purchParmTable.TableRefId;
        purchParmLine.InitFromPurchLine(purchLine);
        purchParmLine.ReceiveNow = 1000;
        purchParmLine.modifiedReceiveNow();
        purchParmLine.PreviousReceiveNow = vendPackingSlipTrans.Qty;
        purchParmLine.PreviousInventNow = vendPackingSlipTrans.InventQty;
        purchParmLine.setQty(DocumentStatus::PackingSlip, false);
        purchParmLine.setLineAmount();
        purchParmLine.insert();
    }

    purchFormLetter = PurchFormLetter::construct(DocumentStatus::PackingSlip);
    purchFormLetter.parmVersioningUpdateType(VersioningUpdateType::Correction);
    purchFormLetter.purchParmUpdate(purchFormLetterParmData.parmParmUpdate());
    purchFormLetter.parmCallerTable(vendPackingSlipJour);
    purchFormLetter.parmParmTableNum(purchParmTable.ParmId);
    purchFormLetter.parmId(purchParmTable.ParmId);
    purchFormLetter.specQty(PurchUpdate::ReceiveNow);
    purchFormLetter.transDate(systemDateGet());
    purchFormLetter.proforma(false);
    purchFormLetter.run();

    ttsCommit;
}

Testing:

After running the job, you can see below the correction posted by comparing the last 2 versions of the product receipt journal:

Untitled

Advertisements

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 )

Twitter picture

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

Facebook photo

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

Google+ photo

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

Connecting to %s