Reverse customer transaction in X++

Reprinted from: https://community.dynamics.com/blogs/post/?postid=d7f28166-cd5f-469c-979c-4dc412212af1

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

Code

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
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));
}
}
}