Skip to main content

Dynamics Axapta: Sales Orders & Business Connector

Well, again folllowing my same idea of writting close to nothing and pasting code, I'll paste in some code to create a sales order from some basic data and the invoice it. I'll try to explain more in the future.

AxaptaObject axSalesTable = ax.CreateAxaptaObject("AxSalesTable");
AxaptaRecord rcInventDim = ax.CreateAxaptaRecord("InventDim");

AxaptaRecord rcCustTable = ax.CreateAxaptaRecord("CustTable");
rcCustTable.ExecuteStmt("select * from %1 where %1.AccountNum == '" + MySalesOrderObject.CustAccount + "'");
if (MySalesOrderObject.CurrencyCode.Trim().Length == 0)
MySalesOrderObject.CurrencyCode = rcCustTable.get_Field("Currency").ToString().Trim();
string sTaxGroup = rcCustTable.get_Field("taxgroup").ToString().Trim();

//set header level fields
axSalesTable.Call("parmSalesName", MySalesOrderObject.SalesName.Trim());
axSalesTable.Call("parmCustAccount", MySalesOrderObject.CustAccount.Trim());
axSalesTable.Call("parmInvoiceAccount", MySalesOrderObject.InvoiceAccount.Trim());
axSalesTable.Call("parmSalesType", (int)MySalesOrderObject.Type);
axSalesTable.Call("parmSalesStatus", (int)MySalesOrderObject.Status);
axSalesTable.Call("parmCurrencyCode", MySalesOrderObject.CurrencyCode.Trim());
axSalesTable.Call("parmProjId", MySalesOrderObject.ProjId.Trim());
axSalesTable.Call("parmPurchOrderFormNum", MySalesOrderObject.PurchOrderFormNum.Trim());
axSalesTable.Call("parmShippingDateRequested", MySalesOrderObject.InvoiceDate);

if (MySalesOrderObject.Lines.Count > 0)
{
axSalesTable.Call("parmInventLocationId", MySalesOrderObject.Lines[0].IvDim.InventLocationId);
axSalesTable.Call("parmInventSiteId", MySalesOrderObject.Lines[0].IvDim.InventSiteId);
}

//save header
axSalesTable.Call("save");

//get the ID
MySalesOrderObject.SalesId = axSalesTable.Call("parmSalesID").ToString();

//get the RECID for taxes calculations
long iSalesRecId = Convert.ToInt64(rcSalesTable.get_Field("RecId"));
int iSalesTableId = Convert.ToInt32(rcSalesTable.get_Field("TableId"));

//Create the taxes if needed. This could be useful to some of you, let's say you have tax line objects stored against the lines
foreach (SalesTaxLine MySalesLineTaxObject in MySalesOrderObject.TaxLines)
{
AxaptaRecord rcTaxWorkRegulation = ax.CreateAxaptaRecord("TaxWorkRegulation");
rcTaxWorkRegulation.set_Field("HeadingRecId", iSalesRecId);
rcTaxWorkRegulation.set_Field("HeadingTableId", iSalesTableId);
rcTaxWorkRegulation.set_Field("TaxCode", MySalesLineTaxObject.TaxItemCode);
rcTaxWorkRegulation.set_Field("TaxRegulationAmountCur", TaxAmount);
rcTaxWorkRegulation.set_Field("TaxDirection", 0);
rcTaxWorkRegulation.set_Field("ManualInsertedTax", 1);
rcTaxWorkRegulation.Insert();
}

//create lines
foreach (SalesOrderLine MySalesOrderLineObject in MySalesOrderObject.Lines)
{
AxaptaRecord rcInventTableModule = ax.CreateAxaptaRecord("InventTableModule");

//get the unit of measure
rcInventTableModule = ax.CallStaticRecordMethod("InventTableModule", "find", MySalesOrderLineObject.ItemId, "2") as AxaptaRecord;
string sUnitId = rcInventTableModule.get_Field("UnitId").ToString();
if (sUnitId == null) sUnitId = string.Empty;

AxaptaObject axSalesLine = ax.CreateAxaptaObject("AxSalesLine");
axSalesLine.Call("AxSalesTable", axSalesTable);

//set line level fields
axSalesLine.Call("parmSalesId", MySalesOrderObject.SalesId.Trim());
axSalesLine.Call("parmItemId", MySalesOrderLineObject.ItemId.Trim());
axSalesLine.Call("parmSalesUnit", sUnitId.Trim());
axSalesLine.Call("parmSalesQty", MySalesOrderLineObject.SalesQty);
axSalesLine.Call("parmSalesPrice", MySalesOrderLineObject.SalesPrice);
axSalesLine.Call("parmLineDisc", MySalesOrderLineObject.LineDisc);
axSalesLine.Call("parmLinePercent", MySalesOrderLineObject.LinePercent);
axSalesLine.Call("parmLineAmount", MySalesOrderLineObject.LineAmount);
axSalesLine.Call("parmShippingDateRequested", DateTime.Today);

rcInventDim.Clear();
rcInventDim.set_Field("inventDimId", MySalesOrderLineObject.IvDim.InventDimId.Trim());
rcInventDim.set_Field("InventLocationId", MySalesOrderLineObject.IvDim.InventLocationId.Trim());
rcInventDim.set_Field("configId", MySalesOrderLineObject.IvDim.ConfigId.Trim());
rcInventDim.set_Field("InventSizeId", MySalesOrderLineObject.IvDim.InventSizeId.Trim());
rcInventDim.set_Field("InventColorId", MySalesOrderLineObject.IvDim.InventColorId.Trim());
rcInventDim.set_Field("InventSiteId", MySalesOrderLineObject.IvDim.InventSiteId.Trim());
rcInventDim.set_Field("InventBatchId", MySalesOrderLineObject.IvDim.InventBatchId.Trim());
rcInventDim.set_Field("wMSPalletId", MySalesOrderLineObject.IvDim.swMSPalletId);
rcInventDim = ax.CallStaticRecordMethod("inventDim", "findOrCreate", rcInventDim) as AxaptaRecord;

MySalesOrderLineObject.IvDim.InventDimId = rcInventDim.get_Field("inventDimId").ToString().Trim();
axSalesLine.Call("parmInventDimId", MySalesOrderLineObject.IvDim.InventDimId.Trim());

//save the line
axSalesLine.Call("save");

//Update any line items if needed, this can be donde this way:
if (MySalesOrderLineObject.Departamento.Trim() != string.Empty)
{
AxaptaRecord rcSalesLine = ax.CreateAxaptaRecord("SalesLine");
decimal dLineNum = Convert.ToDecimal(axSalesLine.Call("parmLineNum"));
rcSalesLine.ExecuteStmt("select forupdate * from %1 where %1.salesid == '"
+ MySalesOrderObject.SalesId + "' && %1.LineNum == " + dLineNum.ToString());

//By default from the customer
//rcSalesLine.set_Field("Dimension[1]", "DIM1");
//rcSalesLine.set_Field("Dimension[2]", "DIM2");
rcSalesLine.set_Field("Dimension[3]", "DIM3");
rcSalesLine.Call("Update");
}
}


Now let's say we want to invoice our order, we would do the following:
ax.TTSBegin();

AxaptaRecord axSalesTable = ax.CreateAxaptaRecord("SalesTable");
axSalesTable = ax.CallStaticRecordMethod("SalesTable", "find", MySalesOrderObject.SalesId) as AxaptaRecord;
AxaptaObject axSalesFormLetter_Invoice = ax.CreateAxaptaObject("SalesFormLetter_Invoice");
axSalesFormLetter_Invoice.Call("new");
axSalesFormLetter_Invoice.Call("update", axSalesTable);

AxaptaRecord rcCustInvoiceJour = ax.CreateAxaptaRecord("CustInvoiceJour");
rcCustInvoiceJour.ExecuteStmt("select * from %1 where %1.salesid == '" + MySalesOrderObject.SalesId.Trim() + "'");
MySalesOrderObject.InvoiceNumber = Convert.ToString(rcCustInvoiceJour.get_Field("InvoiceId")).Trim(); //get the invoice number if needed

ax.TTSCommit();

Popular posts from this blog

What does this mean: "The form datasource query object does not support changing its AllowCrossCompany property after the form has executed the query."?

I have made a form with datasources vendtable and vendtrans. Inside vendtable_ds.executequery() looks like this: QueryBuildDataSource queryBuildDatasource ,queryBDS_VendTrans_Invoice; ; queryBuildDatasource = this.query().dataSourceTable(tablenum(vendtable)); queryBDS_VendTrans_Invoice = this.query().dataSourceTable(tablenum(vendtrans)); if (curext() == "MASTERCOMP") { this.query().allowCrossCompany(true); } else { this.query().allowCrossCompany(false); } //FilterVendorName = stringedit control on form if (FilterVendorName.text()) { queryBuildDatasource.addRange(fieldNum(VendTable,Name)).value(strfmt("*%1*", FilterVendorName.text())); } else { queryBuildDatasource.clearRange(fieldNum(VendTable,Name)); } //FilterInvoiceNumber = stringedit control on form if (FilterInvoiceNumber.valueStr() == "") { queryBDS_VendTrans_Invoice.enabled(false); } else { queryBDS_VendTrans_Invoice.enabled(true); queryBDS_VendTrans_In...

Credit Note [Dynamics AX] using X++

This post will help to create credit note for a sales order based on the invent lot id. All the invoices raised for a particular sales line – Lot Id will be raised back as a credit note. Information on Credit Note: A credit note or credit memorandum (memo) is a commercial document issued by a seller to a buyer. The seller usually issues a Credit Memo for the same or lower amount than the invoice, and then repays the money to the buyer or sets it off against a balance due from other transactions Below Code will help to create credit note for all the invoices raised against the sales line -lot id. Please note: This code can be customized as per your requirements. This is just a template to help creating credit note using X++ code. Please test the code before use. static void SR_CreateCreditNote_Sales(Args _args) { // Coded by Sreenath Reddy CustInvoiceTrans custInvoiceTrans; Dialog dialog = new Dialog(“Create credit note – for sales.”); DialogField dfInv...