Create/Update Sales order and Purchase order using X++


In this article, We will see how to create/update sales order and purchase order through x++.

The code snippet for purchase order creation

public class PuchaseOrderDemo
{
    private void new()
    {
    }

    public static PuchaseOrderDemo construct()
    {
        return new PuchaseOrderDemo();
    }

    public void createPurchOrder()
    {
		PurchTable purchTable;
		PurchLine purchLine;
		VendTable vendTable = VendTable::find("1000");
		AxPurchTable axPurchTable;
		AxPurchLine axPurchLine;
		PurchFormLetter purchFormLetter;

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

		axPurchTable = axPurchTable::newPurchTable(purchTable);
		axPurchTable.parmPurchaseType(PurchaseType::Purch);
		axPurchTable.parmDocumentStatus(DocumentStatus::PurchaseOrder);
		axPurchTable.parmAccountingDate(systemDateGet());
		//axPurchTable.parmDeliveryDate(016\2012);
		axPurchTable.parmPurchStatus(PurchStatus::Backorder);
		axPurchTable.parmInventSiteId(‘1′);
		axPurchTable.parmInventLocationId(’11’);
		axPurchTable.doSave();

		//Create PurchLine for item 1000
		purchLine.initFromPurchTable(purchTable);

		axPurchLine = AxPurchLine::newPurchLine(purchLine);
		axpurchLine.parmItemId(“1000”);
		axPurchLine.parmPurchQty(10);
		axPurchLine.parmPurchPrice(100);
		axPurchLine.doSave();

		purchLine.clear();
		purchLine.initFromPurchTable(purchTable);
		axPurchLine = AxPurchLine::newPurchLine(purchLine);
		axpurchLine.parmItemId(“A0001”);
		axPurchLine.parmPurchQty(10);
		axPurchLine.parmPurchPrice(100);
		axPurchLine.doSave();
		//Posting PO Confirmation,I guess its mandatory
		//You cannot do invoice without doing PO confirm
		purchTable = axPurchTable.purchTable();
		purchFormLetter = PurchFormLetter::construct(DocumentStatus::PurchaseOrder);
		purchFormLetter.update(purchTable, strFmt(“PO_%1”, purchTable.PurchId));

		//Posting PO Invoice
		purchFormLetter = PurchFormLetter::construct(DocumentStatus::Invoice);
		purchFormLetter.update(purchTable, strFmt(“Inv_%1”, purchTable.PurchId));
    }
}

The code snippet for sales order creation

<!-- wp:code -->
<pre class="wp-block-code"><code>public class SalesOrderDemo
{
    private void new()
    {
    }

    public static SalesOrderDemo construct()
    {
        return new SalesOrderDemo();
    }

    public void createSalesOrder()
    {
		SalesTable salesTable;
		SalesLine salesLine;
		CustTable custTable= CustTable::find(“US-007”);
		AxSalesTable axsalesTable;
		AxSalesLine axSalesLine;
		SalesFormLetter salesFormLetter;
		NumberSeq numberSeq;
		#OCCRetryCount
		if (! this.validate())
		throw error(“”);

		try
		{
		ttsbegin;

		//Create Sales order

		// ttsBegin;
		numberSeq = NumberSeq::newGetNum(SalesParameters::numRefSalesId());
		numberSeq.used();

		salesTable.SalesId = numberSeq.num();
		salesTable.initValue();
		salesTable.CustAccount = ‘US-007’;
		salesTable.initFromCustTable();
		salesTable.InventSiteId = ‘1’;
		salesTable.InventLocationId = ’11’;

		if (!salesTable.validateWrite())
		{
		throw Exception::Error;
		}
		salesTable.insert();
		//ttsCommit;
		info(strFmt(“Sales order ‘%1’ has been created”, salesTable.SalesId));

		while select tmpFromVirtual
		{
		salesLine.clear();
		salesLine.initFromSalesTable(salesTable);
		salesLine.SalesId = salesTable.SalesId;
		salesLine.ItemId = tmpFromVirtual.ItemId;
		salesLine.createLine(true, true, true, true, true, true);
		}
		if (1)
		{
		throw error(‘canceled’);
		}
		//SO confirmation
		salesFormLetter = SalesFormLetter::construct(DocumentStatus::Confirmation);
		salesFormLetter.update(salesTable);

		// SO 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;
		}
	}
}

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.

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 )

Google photo

You are commenting using your Google 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 )

Connecting to %s

Create a website or blog at WordPress.com

Up ↑

%d bloggers like this: