Sunday, December 8, 2013

AX 2012:- Business Framework

We as AX developers at some point have developed functionalities wherein we need to take some input from the user and based on that input some processing needs to be done in batch. When such requirements come up, the first thing that comes to our mind is RunBase and RunBaseBatch.
I would like to introduce a framework introduced in AX2012, the still is derived from RunBaseBatch but it is a lot easier to perform tasks like building the dialog UI, validate the user entered input and retrieve user entered input for processing. Well in the introduction (to keep it simple), we’ll take only these three objectives.
Requirement
Suppose, I have a requirement of displaying to user a dialog in which he/she can enter following fields through a dialog
i. Name (String)
ii. Greeting Count (Any number not greater than 10)
After pressing OK, user sees an info log of a greeting, as many numbers of times as he had entered in the second field.
Solution
The above solution when build by deriving class from RunBaseBatch will need a class extended from RunBaseBatch. That single class will handle building of user interface in the dialog, validate user input in the dialog fields and perform required processing on the user input. However, when using Business Operation Framework, we can split this into individual tasks. Let’s build the above requirements with Business Operation Framework.
Data Contract Class

First we need a data contract class for the operation framework. The data contract class will contain parm methods to retrieve user entered values from the dialog. Assuming we have an integer based EDT named GreetCount.

[DataContractAttribute]
class GreetOperationDataContract
{
GreetCount greetCount;
Name usersName;
}
[DataMemberAttribute]
public GreetCount parmGreetCount(greetCount _greetCount = greetCount)
{
greetCount = _greetCount;
return greetCount;
}
[DataMemberAttribute]
public Name parmUsersName(Name _usersName = usersName)
{
usersName = _usersName;
return usersName;
}

Service Class
Next we a class that performs the required operation of displaying a greeting infolog the required number of times.

class GreetService
{
}
public void showGreetingInfolog(GreetOperationDataContract _dataContract)
{
int i;
for(i = 0; i < _dataContract.parmGreetCount(); i++)
{
info(strFmt("Hello %1. No. %2", _dataContract.parmUsersName(), i));
}
}

Service Controller Class

class GreetServiceController extends SysOperationServiceController
{
}
public static void main(args _args)
{
GreetServiceController greetController;
greetController = GreetServiceController::construct();
greetController.parmExecutionMode(SysOperationExecutionMode::Synchronous);
greetController.startOperation();
}
public static GreetServiceController construct()
{
return new GreetServiceController();
}
public ClassDescription caption()
{
return "Greeting to Operation framework";
}
public void new()
{
super();
this.parmClassName(classstr(GreetService));
this.parmMethodName(methodStr(GreetService, showGreetingInfolog));
}
public LabelType parmDialogCaption(LabelType _dialogCaption = "")
{
LabelType ret;
ret = this.caption();
return ret;
}


User Interface Builder Class
The user interface builder class is needed only if you wish to perform some validations on the user input, or create a custom lookup on some dialog field.

class GreetUIBuilder extends SysOperationAutomaticUIBuilder
{
DialogField dlgGreetCount;
}
public boolean greetCountValidate(FormIntControl _control)
{
if (_control.value() > 10)
{
return checkFailed("Please enter a number less than 10");
}
return true;
}
public void postBuild()
{
super();
dlgGreetCount = this.bindInfo().getDialogField(this.dataContractObject(), methodStr(GreetOperationDataContract, parmGreetCount));
}
public void postRun()
{
super();
dlgGreetCount.registerOverrideMethod(methodStr(FormIntControl, validate), methodStr(GreetUIBuilder, greetCountValidate), this);
}


Output
Run the controller class to see the output.




And in the Greet # field try to enter a value greater than 10. If you choose to run this operation in batch, you can go to System Administration -> Inquiries -> Batch jobs -> Batch jobs or (Batch jobs history) to view the status of the batch job. Select the operation "Greeting to operation framework" and click button Log. Will see the output.




No comments:

Post a Comment