Purpose:
The purpose of this document is to demonstrate how we can reverse a posted customer transaction through X++. The code below can be used as a script to automate reversal of posted customer transactions.
Product:
Dynamics 365 for Finance and Operations
Development:
Please find below the code which can be used to reverse a posted customer transaction. It will actually create and post a negative entry transaction against the transaction to reverse. Please note the code defaults reason code to “ERROR” while posting a reversal transaction. Once developed this can be triggered using the following URL:
https://usnconeboxax1aos.cloud.onebox.dynamics.com/?mi=SysClassRunner&cmp=USMF&cls=MAKCustTransReversal
Code:
class MAKCustTransReversal extends TransactionReversal_Cust
{
public static MAKCustTransReversal construct()
{
return new MAKCustTransReversal();
}
public boolean showDialog()
{
return false;
}
public static void main(Args _args)
{
CustTrans custTrans;
MAKCustTransReversal makCustTransReversal;
ReasonTable reasonTable;
ReasonCode reasonCode;
ReasonRefRecID reasonRefRecID;
InvoiceId invoiceId;
Args args;
;
invoiceId = "3392";
reasonCode = "ERROR";
reasonTable = ReasonTable::find(reasonCode);
reasonRefRecID = ReasonTableRef::createReasonTableRef(
reasonTable.Reason, reasonTable.Description);
custTrans = CustTrans::findFromInvoice(invoiceId);
if (custTrans.RecId && !custTrans.LastSettleVoucher)
{
args = new Args();
args.record(custTrans);
makCustTransReversal = MAKCustTransReversal::construct();
makCustTransReversal.parmReversalDate(systemDateGet());
makCustTransReversal.parmReasonRefRecId(reasonRefRecID);
makCustTransReversal.reversal(args);
info(strFmt("%1 %2 %3 %4 reversed.",
custTrans.Voucher,
custTrans.TransDate,
custTrans.Invoice,
custTrans.Txt));
}
}
}