Parag Chapre

Create Sales order and Purchase order using X++

In this article, we will see how to create sales order and purchase order using x++.

The code snippet for purchase order creation

internal final class CreatePurchaseOrder
{
    /// <summary>
    /// Class entry point. The system will call this method when a designated menu 
    /// is selected or when execution starts and this class is set as the startup class.
    /// </summary>
    /// <param name = "_args">The specified arguments.</param>
    public static void main(Args _args)
    {
        PurchTable purchTable;
        PurchLine purchLine;
        NumberSeq numberSeq;
        PurchFormLetter purchFormLetter;
        #OCCRetryCount
        try
        {
            ttsBegin;
            //Create purchase order header
            numberSeq = NumberSeq::newGetNum(PurchParameters::numRefPurchId());
            numberSeq.used();
            purchTable.PurchId = numberSeq.num();
            purchTable.initValue();
            purchTable.AccountingDate = systemDateGet();
            purchTable.initFromVendTable(VendTable::find('1001'));
            purchTable.InventSiteId = '5';
            purchTable.InventLocationId = '51';
            if (!purchTable.validateWrite())
            {
                throw Exception::Error;
            }
            //insert purchase order header data
            purchTable.insert();
            //Create purchase order line
            purchLine.PurchId = purchTable.PurchId;
            purchLine.ItemId = '1000';
            purchLine.purchQty = 10;
            purchLine.createLine(true, true, true, true, true, true);
            ttsCommit;
            info(strFmt("Purchase order '%1' has been created",purchTable.PurchId));
            purchFormLetter = PurchFormLetter::construct(DocumentStatus::PurchaseOrder);
            purchFormLetter.update(purchTable, strFmt("PO_%1", purchTable.PurchId));
    
    }
    catch (Exception::Deadlock)
    {
        retry;
    }
    catch (Exception::UpdateConflict)
    {
        if (appl.ttsLevel() == 0)
        {
            if (xSession::currentRetryCount() >= #RetryNum)
            {
 
                throw Exception::UpdateConflictNotRecovered;
            }
            else
            {
                retry;
            }
        }
        else
        {
            throw Exception::UpdateConflict;
        }
    }
    }
}

The code snippet for sales order creation

internal final class CreateSalesOrder
{
        /// <summary>
    /// Class entry point. The system will call this method when a designated menu 
    /// is selected or when execution starts and this class is set as the startup class.
    /// </summary>
    /// <param name = "_args">The specified arguments.</param>
    public static void main(Args _args)
    {
        SalesTable salesTable;
        SalesLine salesLine;
        SalesFormLetter salesFormLetter;
        NumberSeq numberSeq;
        #OCCRetryCount
        try
        {
            ttsbegin;
 
            //Create Sales order
 
            ttsBegin;
            numberSeq = NumberSeq::newGetNum(SalesParameters::numRefSalesId());
            numberSeq.used();
 
            salesTable.SalesId = numberSeq.num();
            salesTable.initValue();
            //Assign customer account
            salesTable.CustAccount = "US-003";
            salesTable.initFromCustTable();
            salesTable.InventSiteId = '5';
            salesTable.InventLocationId = '51';
 
            if (!salesTable.validateWrite())
            {
                throw Exception::Error;
            }
            salesTable.insert();
                       
 
            //Create sales order line
            salesLine.clear();
            salesLine.initFromSalesTable(salesTable);
            salesLine.SalesId = salesTable.SalesId;
            salesLine.ItemId = 'MA-001';
            salesLine.SalesQty = 10;
            salesLine.createLine(true, true, true, true, true, true);
            info(strFmt("Sales order ‘%1’ has been created", salesTable.SalesId));
            ttsCommit;
            //Sales order confirmation
            salesFormLetter = SalesFormLetter::construct(DocumentStatus::Confirmation);
            salesFormLetter.update(salesTable);
 
            // Sales order invoicing
            salesFormLetter = salesFormLetter::construct(DocumentStatus::Invoice);
            salesFormLetter.update(salesTable);
            ttscommit;
        }
        catch (Exception::Deadlock)
        {
            retry;
        }
        catch (Exception::UpdateConflict)
        {
            if (appl.ttsLevel() == 0)
            {
                if (xSession::currentRetryCount() >= #RetryNum)
                {
 
                    throw Exception::UpdateConflictNotRecovered;
                }
                else
                {
                    retry;
                }
            }
            else
            {
                throw Exception::UpdateConflict;
            }
        }
    }
}

The above code can be downloaded via the link

If you like this article, feel free to share it with others who might find it helpful! If you have any questions, feel free to reach out to me.

1 Comment

  1. […] In this article, we will see how to create sales order and purchase order using x++ >> Create Sales order and Purchase order using X++ […]

Leave a Reply

Your email address will not be published. Required fields are marked *