Sunday, May 19, 2019

Batch Processing in Dynamics AX 2012 Using Sys Operation Framework

In Microsoft Dynamics AX 2012, SysOperation framework replaced RunBase framework to support the batch processing functionality. Going forward, SysOperation framework is recommended for writing custom business logic that requires batch processing functionality, over the deprecated RunBase framework.
The RunBase framework defines coding patterns that implement these requirements. The SysOperation framework provides base implementations for many of the patterns defined by the RunBase framework. Another great thing is SysOperation framework simplified the pack / unpack of variables that was additional work in the RunBase framework, taking advantage of the Attributes feature introduced with AX 2012. For a detailed comparison between SysOperation and RunBase framework, refer to Microsoft Dynamics AX 2012 White Paper: Introduction to the SysOperation Framework.
SysOperation framework allows application logic to be written in a way that supports running operations interactively or via the Microsoft Dynamics AX batch server. It implements the MVC (Model–View–Controller) design pattern, with the isolation of Parameters (Model), Dialog (View) and Service (Controller), for the code that’s executed.

The key objects of the framework are defined below:

• Service: Service class extends from the SysOperationServiceBase class and contains the business logic for the batch operation. Developers often tend to add the business logic in controller classes, which violates the Single responsibility principle.
• Data Contract: Data contract class is the model class defining attributes needed for batch operations. These attributes are provided by the user, in a dialog. DataContractAttribute attribute is needed for the class and the properties methods requires DataMemberAttribute attribute.
• Controller: Controller class extends from the SysOperationServiceController class. It holds information about the batch operation like execution mode, show dialog or progress bar etc. and directs the batch operation.
• UI Builder: UI Builder class extends from SysOperationAutomaticUIBuilder class and is used for adding custom behavior to dialog / dialog fields dynamically constructed by the SysOperation framework.
In this blogpost, I’ll cover three different scenarios (in a sequential manner) for creating batch operations using SysOperation framework classes – Service, Data Contract, Controller and UI Builder.

Scenario 1: Creating a simple batch operation

Running batch operation
1. Controller Class
public class SysOperationControllerClass extends SysOperationServiceController
{
}
public void new()
{
    super();
    
    this.parmClassName(classStr(SysOperationServiceClass));
    this.parmMethodName(methodStr(SysOperationServiceClass, processOperation));
    
    this.parmDialogCaption("Batch Operation Dialog Title");
}
public ClassDescription caption()
{
    return "Batch Operation Task Description";
}
public static void main(Args args)
{
    SysOperationControllerClass controller;
    
    controller = new SysOperationControllerClass();
    controller.startOperation();
}
 2. Service Class
public class SysOperationServiceClass extends SysOperationServiceBase
{
}
public void processOperation()
{
    info ("Running SysOperation Batch Operation");
}

3. Optional: Menu item
Type: Action menu item
Object type: Class
Object: SysOperationControllerClass

Scenario 2: Add user defined parameters for batch operation

Add user defined parameters for batch operation
Add user defined parameters for batch operation

1. Controller Class
public class SysOperationControllerClass extends SysOperationServiceController
{
}
public void new()
{
    super();
    
    this.parmClassName(classStr(SysOperationServiceClass));
    this.parmMethodName(methodStr(SysOperationServiceClass, processOperation));
    
    this.parmDialogCaption("Batch Operation Dialog Title");
}
public ClassDescription caption()
{
    return "Batch Operation Task Description";
}
public static void main(Args args)
{
    SysOperationControllerClass controller;
    
    controller = new SysOperationControllerClass();
    controller.startOperation();
}
2. Service Class

public class SysOperationServiceClass extends SysOperationServiceBase
{
}
public void processOperation(SysOperationDataContractClass _contract)
{
    info ("Running SysOperation Batch Operation");
    
    info ("Date Property: " + date2Str
            (_contract.parmDate(),
            213,
            DateDay::Digits2,
            DateSeparator::Dot,
            DateMonth::Long,
            DateSeparator::Dot,
            DateYear::Digits4));
    
    info ("Text Property: " + _contract.parmText());
}
3. Data Contract Class

[DataContractAttribute]
public class SysOperationDataContractClass
{
    TransDate      dateValue;
    Description255 textValue;
}
[DataMemberAttribute,
SysOperationLabelAttribute('Date Property'),
SysOperationHelpTextAttribute('Enter a date in past'),
SysOperationDisplayOrderAttribute('1')]
public TransDate parmDate(TransDate _dateValue = dateValue)
{
    dateValue = _dateValue;
    return dateValue;
}
[DataMemberAttribute,
SysOperationLabelAttribute('Text Property'),
SysOperationHelpTextAttribute('Type some text'),
SysOperationDisplayOrderAttribute('2')]
public Description255 parmText(Description255 _textValue = textValue)
{
    textValue = _textValue;
    return textValue;
}

4. Optional: Menu item
Type: Action menu item
Object type: Class
Object: SysOperationControllerClass

Scenario 3: Add custom validation to dialog fields

Scenario 3.1 – Validation failed:

Add custom validation to dialog fields - Validation Failed

Scenario 3.2 – Validation passed:

Add custom validation to dialog fields - Validation Passed.
Add custom validation to dialog fields - Validation Passed

1. Controller Class

public class SysOperationControllerClass extends SysOperationServiceController
{
}
public void new()
{
    super();
    
    this.parmClassName(classStr(SysOperationServiceClass));
    this.parmMethodName(methodStr(SysOperationServiceClass, processOperation));
    
    this.parmDialogCaption("Batch Operation Dialog Title");
}
public ClassDescription caption()
{
    return "Batch Operation Task Description";
}
public static void main(Args args)
{
    SysOperationControllerClass controller;
    
    controller = new SysOperationControllerClass();
    controller.startOperation();
}
2. Service Class

public class SysOperationServiceClass extends SysOperationServiceBase
{
}
public void processOperation(SysOperationDataContractClass _contract)
{
    info ("Running SysOperation Batch Operation");
    
    info ("Date Property: " + date2Str
            (_contract.parmDate(),
            213,
            DateDay::Digits2,
            DateSeparator::Dot,
            DateMonth::Long,
            DateSeparator::Dot,
            DateYear::Digits4));
    
    info ("Text Property: " + _contract.parmText());
}
3. Data Contract Class

[DataContractAttribute,
SysOperationContractProcessingAttribute(classStr(SysOperationUIBuilderClass))]
public class SysOperationDataContractClass
{
    TransDate      dateValue;
    Description255 textValue;
}
[DataMemberAttribute,
SysOperationLabelAttribute('Date Property'),
SysOperationHelpTextAttribute('Enter a date in past'),
SysOperationDisplayOrderAttribute('1')]
public TransDate parmDate(TransDate _dateValue = dateValue)
{
    dateValue = _dateValue;
    return dateValue;
}
[DataMemberAttribute,
SysOperationLabelAttribute('Text Property'),
SysOperationHelpTextAttribute('Type some text'),
SysOperationDisplayOrderAttribute('2')]
public Description255 parmText(Description255 _textValue = textValue)
{
    textValue = _textValue;
    return textValue;
}
4. UI Builder Class

public class SysOperationUIBuilderClass extends SysOperationAutomaticUIBuilder
{
    DialogField dateField;
}
public void postBuild()
{
    super();
    
    // get references to dialog controls after creation
    dateField = this.bindInfo().getDialogField(this.dataContractObject(),
                                    methodStr(SysOperationDataContractClass, parmDate));
}
public void postRun()
{
    super();
    
    // register overrides for form control events
    dateField.registerOverrideMethod(methodstr(FormDateControl, validate),
                methodstr(SysOperationUIBuilderClass, validateDate), this);
}
public boolean validateDate(FormDateControl _dateControl)
{
    if (_dateControl.dateValue() > today())
    {
        error("Please select a past date");    
        return false;
    }
    return true;
}








1 comment: