Friday, December 15, 2023

JSON serialization in X++

JSON Serialization in X++

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
        }
    ]
}
        

Wednesday, December 13, 2023

Creating Dependent Tasks in Dynamics 365 Finance and Operations Batch Jobs with X++ Code

Creating Dependent Tasks in Dynamics 365 Finance and Operations Batch Jobs with X++ Code

Creating Dependent Tasks in Dynamics 365 Finance and Operations Batch Jobs with X++ Code

Batch processing is a critical component in handling large-scale data operations within Dynamics 365 Finance and Operations (D365FO). Often, there's a need to create dependent tasks within batch jobs to ensure proper sequencing and execution. In this blog post, we will explore how to create dependent tasks using X++ code in D365FO, based on the provided example.

Understanding the Code

    BatchHeader batchHeader = BatchHeader::construct();

// First task (your first batch job)
DMFBatchImporter batchImporter = new DMFBatchImporter();

batchImporter.batchInfo().parmBatchExecute(NoYes::Yes);
batchImporter.parmReadyForBatchRun(true);
DMFExecutionId batchExecutionId = DMFPackageImporter::PrepareDefinitionGroupForImport(definitionGroupName, executionId, dataAreaId, true);
batchImporter.parmExecutionId(batchExecutionId);

batchHeader.addTask(batchImporter);
batchHeader.addRuntimeTask(batchImporter, BatchHeader::getCurrentBatchTask().RecId);

// Add your second batch job here
TestIntegrationBatch integrationBatch = new TestIntegrationBatch();
integrationBatch.parmLogRecId(logRecId);
integrationBatch.batchInfo().parmGroupId(definition.BatchGroupId);

batchHeader.parmCaption(strFmt('%1 - Test Log - %2', TestIntegrationBatch::description(), logRecId));
// Add second task
batchHeader.addRuntimeTask(integrationBatch, BatchHeader::getCurrentBatchTask().RecId);
// Add dependency
batchHeader.addDependency(integrationBatch, batchImporter, BatchDependencyStatus::FinishedOrError);
batchHeader.save();

  

Step-by-Step Guide on Creating Dependent Tasks

  1. Batch Header Initialization:

    Create a new instance of BatchHeader to represent the batch job.

  2. First Task - DMFBatchImporter:

    Instantiate the primary task (DMFBatchImporter). Set necessary parameters for batch execution. Prepare the execution ID using DMFPackageImporter::PrepareDefinitionGroupForImport. Add the task to the batch header using addTask. Add the task as a runtime task using addRuntimeTask.

  3. Second Task - TestIntegrationBatch:

    Instantiate the secondary task (TestIntegrationBatch). Set any required parameters for this task.

  4. Batch Header Configuration:

    Set the caption for the batch header for better identification. Add the secondary task as a runtime task. Define a dependency between tasks using addDependency. In this case, the dependency is set to FinishedOrError, meaning the second task will execute only when the first task finishes successfully or encounters an error.

  5. Save Batch Header:

    Save the configured batch header to persist the changes.

Conclusion

Creating dependent tasks in batch jobs is essential for maintaining the proper flow of data processing in Dynamics 365 Finance and Operations. The provided X++ code showcases the integration of two tasks with a dependency relationship. Customize this example according to your specific business requirements and extend the logic as needed to handle more complex scenarios in your batch processing workflows.

Post partial packing slip in D365FO

How to Post a Partial Packing Slip in D365FO and AX 2012 How to Post a Partial Packing Slip in D365FO and AX 2012 Understanding t...