This is the third and final part in my series of posts about SysOperation (at least for now). You can find part 1 here and part 2 here.
I will demonstrate how to override methods on the dialog (in this case modified()), and how to read and set properties of controls.
For the sake of this demo, we will add a checkbox to the dialog that will allow us to set the enabled property of the date control.
It should look 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!
1. Data Contract
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:
[DataContractAttribute] 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
[DataContractAttribute] class KlForCustTesterDataContract { Name name; TransDate transDate; str packedQuery; }
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.
[DataMemberAttribute] public Name parmName(Name _name = name) { name = _name; return name; }
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 (,).
[DataMemberAttribute ,SysOperationLabelAttribute(literalStr("@SYS11284"))] // today's date public TransDate parmTransDate(TransDate _transDate = transDate) { transDate = _transDate; return transDate; }
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).
[DataMemberAttribute, AifQueryTypeAttribute('_packedQuery', querystr(KlForCustomers)) ] public str parmQuery(str _packedQuery = packedQuery) { packedQuery = _packedQuery; return packedQuery; }
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:
public Query getQuery() { return new Query(SysOperationHelper::base64Decode(packedQuery)); }
To set the query:
public void setQuery(Query _query) { packedQuery = SysOperationHelper::base64Encode(_query.pack()); }
2. Service class and operation
Next, let’s create a service class called KlForCustTesterDataService.
class 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.
[SysEntryPointAttribute] public void testCustomer(KlForCustomerTesterDataContract _klForCustomerTesterDataContract) { }
Of course, this method needs some demo functionality, so let’s add that:
[SysEntryPointAttribute] public void testCustomer(KlForCustTesterDataContract _klForCustTesterDataContract) { QueryRun queryRun; CustTable custTable; ; // info the name parameter info(_klForCustTesterDataContract.parmName()); // create a new queryrun object queryRun = new queryRun(_klForCustTesterDataContract.getQuery()); // loop all results from the query while(queryRun.next()) { custTable = queryRun.get(tableNum(custTable)); // display the accountnum info(custTable.AccountNum); } }
3. Service
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.
4. Menu item
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:
ObjectType: Class
Object: SysOperationServiceController
Parameters: KlForCustTesterDataService.testCustomer
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.
5. Run it!
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.
SharedProject_KlFor_SysOperation_Day1.zip
留言评论
暂无留言