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

Passing values between form and class

Class name is EmplDuplication and Form is EmplTable . void clicked() {    MenuFunction mf;    args args = new Args();    ;     args.record(EmplTable);     mf = new menufunction(identifierstr(EmplDuplication), MenuItemType::Action); mf.run(args); } Meanwhile, in the main() method of the EmplDuplication class, we need to put this Axapta x++ code to get the datasource: static void main(Args args) {     EmplDuplication EmplDuplication; EmplTable localEmplTable; ;     if(args.record().TableId == tablenum(EmplTable)) localEmplTable = args.record();     ... }