JSON Serialization in X++
TestCustTransExportRequest Class
The TestCustTransExportRequest class represents the request data for exporting customer transactions.
[datacontractAttribute]
class TestCustTransExportRequest
{
String10 dataAreaId;
CustAccount custAcc;
[DataMemberAttribute("dataAreaId")]
public String10 parmDataAreaId(String10 _dataAreaId = dataAreaId)
{
dataAreaId = _dataAreaId;
return dataAreaId;
}
[DataMemberAttribute("CustomerAccount")]
public CustAccount parmCustAcc(CustAccount _custAcc = custAcc)
{
custAcc = _custAcc;
return custAcc;
}
}
TestCustTransListResponseContract Class
The TestCustTransListResponseContract class is a response data contract representing customer transaction details. This class includes properties for various transaction details such as type, date, amount, and currency. The [DataMemberAttribute] attribute is used to specify the names of these properties in the serialized JSON.
[datacontractAttribute]
class TestCustTransListResponseContract
{
Str60 transType;
ExchRate exchRate;
String30 transDate;
AmountCur amountCur;
CurrencyCode currency;
TransactionTextLarge description;
AmountMST amountMST;
AmountCur remainAmountBal;
String10 dataAreaId;
CustAccount custAcc;
[DataMemberAttribute("Channel")]
public String10 parmDataAreaId(String10 _dataAreaId = dataAreaId)
{
dataAreaId = _dataAreaId;
return dataAreaId;
}
[DataMemberAttribute("Customer Id")]
public CustAccount parmCustAcc(CustAccount _custAcc = custAcc)
{
custAcc = _custAcc;
return custAcc;
}
[DataMemberAttribute("Transaction type")]
public Str60 parmTransType(Str60 _transType = transType)
{
transType = _transType;
return transType;
}
[DataMemberAttribute("Date")]
public String30 parmTransDate(String30 _transDate = transDate)
{
transDate = _transDate;
return transDate;
}
[DataMemberAttribute("Description")]
public TransactionTextLarge parmDescription(TransactionTextLarge _description = description)
{
description = _description;
return description;
}
[DataMemberAttribute("Amount in currency")]
public AmountCur parmAmountCur(AmountCur _amountMST = amountCur)
{
amountCur = _amountMST;
return amountCur;
}
[DataMemberAttribute("Currency")]
public CurrencyCode parmCurrencyCode(CurrencyCode _currency = currency)
{
currency = _currency;
return currency;
}
[DataMemberAttribute("Amount in accounting currency")]
public AmountMST parmAmountMST(AmountMST _amountMST = amountMST)
{
amountMST = _amountMST;
return amountMST;
}
[DataMemberAttribute("Exchange rate")]
public exchRate parmExchRate(ExchRate _exchRate = exchRate)
{
exchRate = _exchRate;
return exchRate;
}
[DataMemberAttribute("Balance")]
public AmountCur parmRemainAmount(AmountCur _remainAmountBal = remainAmountBal)
{
remainAmountBal = _remainAmountBal;
return remainAmountBal;
}
}
TestCustTransExportResponse Class
The TestCustTransExportResponse class represents the response data for exporting customer transactions. It contains a list of transaction data. This class is designed to be serialized into a JSON structure containing an array of transactions.
class TestCustTransExportResponse
{
List transactionData = new List(Types::Class);
CustAccount custAcc;
String10 dataAreaId;
[DataMemberAttribute("Transactions"),DataCollectionAttribute(Types::Class, classStr(TestCustTransListResponseContract))]
public List parmTransactionData(List _transactionData = transactionData)
{
transactionData = _transactionData;
return transactionData;
}
}
TestCustTransExportService Class
The TestCustTransExportService class is a custom web service class that processes export transactions based on the provided request. It uses the other three classes to gather and structure the transaction data, and then returns the response in the specified JSON format.
class TestCustTransExportService extends SysOperationServiceBase
{
public TestCustTransExportResponse processExportTransaction(TestCustTransExportRequest _requestedCustomer)
{
TestCustTransExportResponse response = new TestCustTransExportResponse();
CustTrans custTrans;
List custTransactionsList = new List(Types::Class);
CustAccount custAccount = _requestedCustomer.parmCustAcc();
Microsoft.Dynamics.Ax.Xpp.ErrorException errorEx;
try
{
changecompany(_requestedCustomer.parmDataAreaId())
{
while select custTrans
where custTrans.AccountNum == custAccount
&& (custTrans.AmountCur - custTrans.SettleAmountCur) != 0
{
TestCustTransListResponseContract custTransListContract = new TestCustTransListResponseContract();
custTransListContract.parmCustAcc(custTrans.AccountNum);
custTransListContract.parmDataAreaId(custTrans.DataAreaId);
custTransListContract.parmTransType(enum2Str(custTrans.TransType));
custTransListContract.parmTransDate(date2StrUsr(custTrans.TransDate));
custTransListContract.parmDescription(custTrans.Txt);
custTransListContract.parmamountCur(custTrans.AmountCur);
custTransListContract.parmAmountMST(custTrans.AmountMST);
custTransListContract.parmExchRate(custTrans.ExchRate);
custTransListContract.parmRemainAmount(custTrans.remainAmountCur());
custTransListContract.parmCurrencyCode(custTrans.CurrencyCode);
custTransactionsList.addEnd(custTransListContract);
}
response.parmTransactionData(custTransactionsList);
}
}
catch(errorEx)
{
throw error(strFmt("Validation failed for customer %1.",custAccount, errorEx.Message));
}
catch (Exception::CLRError)
{
System.Exception ex = CLRInterop::getLastException();
throw error(strFmt("Validation failed for customer %1.",custAccount, ex.Message));
}
return response;
}
}
JSON Input
{ "_requestedCustomer": { "dataAreaId": "SE02", "CustomerAccount": "C0002" } }
JSON Output
{ "$id": "1", "Transactions": [ { "$id": "2", "Channel": "Site1", "Customer Id": "TestCustomer", "Transaction type": "Sales order", "Date": "11/12/2023", "Description": "Credit IN000001VR6 SO SE02-12345VR6 11/12/2023", "Amount in currency": -2500.0, "Currency": "SEK", "Amount in accounting currency": -2500.0, "Exchange rate": 100.0, "Balance": -2500.0 }, { "$id": "3", "Channel": "Site1", "Customer Id": "TestCustomer", "Transaction type": "Sales order", "Date": "11/12/2023", "Description": "Credit IN000001VR7 SO SE02-12345VR7 11/12/2023", "Amount in currency": -2500.0, "Currency": "SEK", "Amount in accounting currency": -2500.0, "Exchange rate": 100.0, "Balance": -2500.0 } ] }