The everyday life of a Dynamics AX developer
Hi All :-)
This is part 1 of a series of 3 blog posts where I demonstrate how to create batch classes using the SysOperation classes (Read the introduction here)
We are going to create a batch that looks like this:
And we are going to do that without creating a RunBaseBatch class!
Note: Just to be clear: the batch class won’t actually do anything useful, everything is just for demo purposes.
Okay, let’s start!
First, we will create a data contract. The data contract contains data members that represent the fields that will be available on the dialog.
Let’s create a new class, KlForCustTesterDataContract:
The string DataContractAttribute indicates that this class is a Data Contract. The square brackets  indicate that this string is an attribute (You can read more about attributes on MSDN).
Next, let’s add Data Members to this contract by first declaring them in the class declaration
We add a string field, a date field, and a query. We still need to create parm methods for these variables.
This is just a simple string field, nothing special here except for the DataMemberAttribute attribute that indicates that this method is da data member.
The date field is of type TransDate, so we’ll give it a nicer label by using the SysOperationLabelAttribute attribute to specify the label used.
Also note that, when two attributes are specified, the are separated by a comma (,).
We will also add a query, so we can loop all customers. As you can see, we can specify the query by using the AifQueryTypeAttribute. The query KlForCustomers is just a query in the AOT with CustTable as a datasource (see xpo file at the end).
Next, we add two helper methods that are not Data Members, so we can easily set and get the query variable.
To get the query:
To set the query:
Next, let’s create a service class called KlForCustTesterDataService.
Right click the class, click properties, and set the run on property to server.
Adding a service operation to this service class is as simple as adding a method. As you can see, the SysEntryPointAttribute attribute indicates that this is a service operation, and our data contract is used as an argument.
Of course, this method needs some demo functionality, so let’s add that:
Right click your project, and choose New – Service. Give this service the same name as the service class, KlForCustTesterDataService.
In the properties of this node, set the class property to KlForCustTesterDataService
Expand the node of your service, then right click on the Operations node.
Click Add operation and check the add checkbox next to the method we’ve created, and click OK.
All we need to do now is create a menu item so we can start our dialog.
Do this by right clicking your project – New – Menu item. Name it KlForCustTesterDataService.
Enter the following properties:
As you can see, we don’t link directly to our service class, in stead, the class SysOperationServiceController will be used, and will know what service operation to start because it is specified in the parameters property. More about these controller class in part 2 of this series.
But wait, we still have to do one small thing: compile our X++ into the common intermediate language (CIL). Don’t worry, it’s easy. Just click the Generate Incremental CIL button in the toolbar (this might take a while).
After CIL was generated, right click the menu item and click Open. You should see your batch class. As you can see, all fields from the datacontract are displayed, and a select button was automatically generated so you can modify the query.
Output when run in batch:
In part 2, we will take a closer look at the SysOperationServiceController class. See you tomorrow.