5. Adding Property Methods
Suppose you want to run the Bike-Tuning Offers
business operation directly from another piece of code without
presenting the user with a dialog box. To do so, you must implement
property methods according to the property method pattern. This pattern
allows you to set and get the properties that would otherwise be
inaccessible because member variables in Dynamics AX are protected.
Start by writing a parm method for the property as follows.
public NoYesId parmCreateServiceOrders(NoYesId _createServiceOrders =
createServiceOrders)
{
;
createServiceOrders = _createServiceOrders;
return createServiceOrders;
}
|
This job demonstrates how you can run the operation without showing the dialog box.
static void createBikeTuningOffersJob(Args _args)
{
BikeTuningOffers bikeTuningOffers;
;
bikeTuningOffers = BikeTuningOffers::construct();
bikeTuningOffers.parmCreateServiceOrders(NoYes::Yes);
bikeTuningOffers.run();
}
|
6. Adding Constructors
X++
doesn’t support method name overloading, and you should avoid using
default parameters on constructors. You must create individually named new methods with different parameter profiles instead.
In the preceding example, you created an
instance of the class and set the necessary parameters. Imagine that
there is one more parameter in your class that indicates a certain
customer account number for creating bike offers. Add a new member
variable to the class declaration, and then add the new parameter
method, like this.
public class BikeTuningOffers extends RunBase
{
DialogField dialogCreateServiceOrders;
NoYesId createServiceOrders;
CustAccount custAccount;
#define.CurrentVersion(1)
#define.version1(1)
#localmacro.CurrentList
createServiceOrders
#endmacro
}
public CustAccount parmCustAccount(CustAccount _custAccount = custAccount)
{
;
custAccount = _custAccount;
return custAccount;
}
|
Suppose that the customer record contains
information about the option to create service orders with bike offers.
For example, imagine that offers are not sent to the customer if the
customer has been stopped for new transactions. Because you want to
avoid using default parameters in the construct method, you must call
both of these parm methods when you create an instance based on a customer record.
Running the business operation from a job with a specific customer would look like this.
server static void createBikeTuningOffersJobCustomer(Args _args)
{
CustTable custTable = CustTable::find('4001');
BikeTuningOffers bikeTuningOffers;
;
bikeTuningOffers = BikeTuningOffers::construct();
bikeTuningOffers.initParmDefault();
bikeTuningOffers.parmCustAccount(custTable.accountNum);
bikeTuningOffers.parmCreateServiceOrders(custTable.blocked == CustVendorBlocked::
No);
bikeTuningOffers.run();
}
|
This code is a good candidate for the static new pattern, so implement a static newCustTable method on the BikeTuningOffers class to create an instance based on a customer record, as shown here.
server static public BikeTuningOffers newCustTable(CustTable _custTable)
{
BikeTuningOffers bikeTuningOffers;
;
bikeTuningOffers = BikeTuningOffers::construct();
bikeTuningOffers.initParmDefault();
bikeTuningOffers.parmCustAccount(_custTable.accountNum);
bikeTuningOffers.parmCreateServiceOrders(_custTable.blocked == CustVendorBlocked::
No);
return biketuningOffers;
}
|
Now change your job to a simpler version to be assured that the class gets properly instantiated and initialized.
server static void createBikeTuningOffersJobCustomer(Args _args)
{
CustTable custTable = CustTable::find('4001');
BikeTuningOffers bikeTuningOffers;
;
bikeTuningOffers = BikeTuningOffers::newCustTable(custTable);
bikeTuningOffers.run();
}
|