Skip to main content

How to create return Order from code

static void SR_CreateReturnOrderAfterInvoice(Args _args)
{
CustInvoiceJour _invoiceRec;
str _returnReason;
CustInvoiceTrans custInvoiceTrans;
SalesLine salesLine;
SalesTable newRetOrder;
CustInvoiceJour custInvoiceJour;

SalesTable createReturnOrderHeader(CustInvoiceJour invoiceRec)
{

SalesTable old, newRec;
boolean bChecksOk = true;
;

old = SalesTable::find(invoiceRec.SalesId);
newRec.initReturnFromSalesTable(old);
newRec.CustAccount = old.CustAccount;

newRec.initFromCustTable();

newRec.CustInvoiceId = invoiceRec.InvoiceId;
newRec.ReturnDeadline = today();
newRec.ReturnReasonCodeId = ’21′; // Defective
newRec.SalesType = SalesType::ReturnItem;
newRec.SalesTaker = SysCompanyUserInfo::current().EmplId;

if ( newRec.ReturnReasonCodeId == ” && CustParameters::find().ReturnOrdersReasonReq ||
newRec.ReturnReasonCodeId != ” && !ReturnReasonCode::exist(newRec.ReturnReasonCodeId) )
{
checkFailed(strfmt(“@SYS26332″, fieldid2pname(tablenum(SalesTable), fieldnum(SalesTable, ReturnReasonCodeId))));
bChecksOk = false;
}

if ( bChecksOk && newRec.validateWrite())
{
newRec.insert();
}
else
{
throw error(“@SYS18722″);
}

return newRec;
}

ttsbegin;

// first we need to create the sales order header for the return order
select custInvoiceJour where custInvoiceJour.RefNum == RefNum::SalesOrder && custInvoiceJour.InvoiceId == ’101231′;

newRetOrder = createReturnOrderHeader(custInvoiceJour);

while select * from custInvoiceTrans where custInvoiceTrans.SalesId == custInvoiceJour.SalesId
&& custInvoiceTrans.InvoiceId == custInvoiceJour.InvoiceId
&& custInvoiceTrans.InvoiceDate == custInvoiceJour.InvoiceDate
&& custInvoiceTrans.numberSequenceGroup == custInvoiceJour.numberSequenceGroup
{
// now we need to populate all the necessary fields for the new salesline
// using the existing invoice and the new sales order
salesLine.initFromCustInvoiceTrans(custInvoiceTrans);
salesLine.initFromSalesTable(newRetOrder);

// udpate the quantity
salesLine.ExpectedRetQty = -custInvoiceTrans.Qty;

if (salesLine.ExpectedRetQty > 0)
{
error(“@SYS53512″);
ttsabort;
}

// set the quantity and amount fields
salesLine.LineAmount = salesLine.returnLineAmount();
salesLine.SalesQty = 0;
salesLine.InventTransIdReturn = custInvoiceTrans.InventTransId;

//create the line
salesLine.createLine(true, false, false, false, false, false, false, false, salesLine.InventTransId);

// clear the buffer
salesLine.clear();
}

ttscommit;

info(strfmt(‘Newly created return order is %1′, newRetOrder.SalesId));

}

Popular posts from this blog

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", M

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();     ... }