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

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...