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

Mark All for Open Cust Trans and Open Vend Trans

We have situations where there are lots of open transactions that need to be settled against each other. This can be the case if auto settlement is turned off. One solution is to add a "Mark All" button to the custOpenTrans or vendOpenTrans forms. This button "checks" the mark checkbox on every line. The user can then uncheck several lines if needed and Update to settle the lines. The code below is an example of what we used on the open vendor transaction screen. The code is very similar on the AR side. One note: I used vendTable.AccountNum in the code below. That should be generalized to work with any buffer that is passed into the open trans form. void customMarkAll() { VendTransOpen localVendTransOpen; VendTrans localVendTrans; container conSum; int linesProcessed; ; //show wait cursor startLengthyOperation(); element.lock(); //remove all prior markings specOffsetVoucher.deleteSpe...

Print Report in Microsoft Dynamics AX 2009 through X++

I am trying to print sales confirmation report on a button click which I have added on Sales Order Detail form in Microsoft Dynamics AX 2009. On click event of that button, I have written following code: void clicked() {     Args                args;     ReportRun           reportRun;     SalesFormLetter     salesFormLetter;     PrintJobSettings    printJobSettings;     CustConfirmJour     custConfirmJour;     RecordSortedList    list                = new RecordSortedList(55);     SalesTable          salesTableUpdate;     ;     SELEC...

Creating Free Text Invoice through X++ code

Job: Calling class from job to run the class public void freeTextInvoicePostTestJob() { Dialog dialog; DialogField dlgCustAcc; DialogGroup dialogPeriodLengthGroup, dialogPeriodLengthGroup1; DialogField dlgLedgerAcc; ; dialog = new Dialog("Free-Text Invoice"); dialogPeriodLengthGroup1 = dialog.addGroup('Cust Table'); dlgCustAcc = dialog.addField(typeid(CustAccount)); dialogPeriodLengthGroup = dialog.addGroup('Ledger Table'); dlgLedgerAcc = dialog.addField(typeid(LedgerAccount)); if(dialog.run()) { if(dlgCustAcc.value() && dlgLedgerAcc.value() != '') FreeTxtInvoiceCreatePost::main(dlgCustAcc.value(), dlgLedgerAcc.value()); else throw error(strfmt("Either CustAccount or LedgerAccount info is missing.")); } } Class: Which creates the free text invoice class FreeTxtInvoiceCreatePost { } static void main(CustAccount _custAccount, LedgerAccount _ledgerAccount) { CustInvoiceTable custInvoiceTable; ...