Batch using SysOperation Framework in Microsoft Dynamics AX 2012 R3


         SysOperation framework permits batch logic to be written in a manner that helps running operations interactively, through the Dynamics batch server. So in this post i am going to tell about How to code batch using SysOperation Framework in D365 F&O.

SysOperation 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. Batch using SysOperation Framework in D365 F&O makes the system more efficient.

The important objects of the SysOperation framework are outlined beneath:

Service Class

Service class extends from the SysOperationServiceBase class and incorporates the business logic for the batch operation. Developers typically have a tendency so as to add the business logic in controller classes, which violates the Single accountability precept.

Data contract Class

Data contract class is the model class defining attributes wanted for batch operations. These attributes are provided by the user, in a dialog. DataContractAttribute attribute is required for the class and the properties methods requires DataMemberAttribute attribute.

Controller class

Controller class extends from the SysOperationServiceController class. It holds details about the batch operation like execution mode, present dialog or progress bar and many others. It directs the batch operation.

UI builder Class

UI Builder class extends from SysOperationAutomaticUIBuilder class and is used for adding customized behavior to dialog / dialog fields dynamically constructed by the SysOperation framework.


On this blog post, I’ll cover three totally different scenarios (in a sequential method) for creating batch using SysOperation framework in D365 F&O.

Scenario 1: Creating a simple batch

To Create a simple batch we need Controller class, Service class and Menu Item to run it.

Controller class

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
public class TestControllerClass extends SysOperationServiceController
{
    public void new()
    {
        super();
     
        this.parmClassName(classStr(TestServiceClass));
        this.parmMethodName(methodStr(TestServiceClass, processOperation));
     
        this.parmDialogCaption("Batch Dialog Title");
    }
 
    public ClassDescription caption()
    {
        return "Batch Task Description";
    }
 
    public static void main(Args args)
    {
        TestControllerClass controller;
     
        controller = new TestControllerClass();
        controller.startOperation();
    }
 
}

Service Class

1
2
3
4
5
6
7
8
9
10
public class TestServiceClass extends SysOperationServiceBase
{
     
    public void processOperation()
    {
        //Input any business process logic that need to be scheduled on batch
        info ("Running Batch Operation");
    }
 
}

Menu Item

Name: TestSysOperationBatchMenu
Type: Action menu item
Object type: Class
Object: TestControllerClass

Then add created menu item to some Menu like Accounts payable for testing as shown below.


Scenario 2: Add custom parameters for batch operation

You can add custom parameters using parmMethods to Data contract class.



Controller class

public class Vij_TestControllerClass extends SysOperationServiceController 
{

}

public ClassDescription caption()
{
    return "Batch Task Description";
}

 public void new()
{
    super();
 
    this.parmClassName(classStr(Vij_TestServiceClass));
    this.parmMethodName(methodStr(Vij_TestServiceClass, processOperation));
 
    this.parmDialogCaption("Batch Dialog Title");
}

 public static Vij_TestControllerClass construct()
{
    return new Vij_TestControllerClass();
}

 public static void main(Args args)
{
    Vij_TestControllerClass controller;
 
    controller = new Vij_TestControllerClass();
    controller.startOperation();
} 

Service Class


public class Vij_TestServiceClass extends SysOperationServiceBase

{
    TransDate      fromDate;
    TransDate      toDate;
}


 public void processOperation(Vij_TestContractClass _contract)
{
    Ledgerjournaltrans trans;
    LedgerJournalTable ledgerJournalTable;
 
    fromDate  =  _contract.parmTransDate();
    toDate    =  _contract.parmUpdateDate();
 
    ttsBegin;

    while select forUpdate trans
        join ledgerJournalTable where trans.JournalNum == ledgerJournalTable.JournalNum
            && ledgerJournalTable.Posted  == NoYes::No
            && trans.TransDate <= fromDate
            && ledgerJournalTable.journalname == "VendPay"
    {
        trans.TransDate = toDate;
        trans.doupdate();
    }

    ttsCommit;
} 

Data contract Class


[DataContractAttribute]

public class Vij_TestContractClass
{
    TransDate      dateValue1;
    TransDate      dateValue2;
}
 [DataMemberAttribute,
SysOperationLabelAttribute('Transaction date'),
SysOperationHelpTextAttribute('Enter a date in past'),
SysOperationDisplayOrderAttribute('1')]

public TransDate parmTransDate(TransDate _dateValue = dateValue1)
{
    dateValue1 = _dateValue;
    return dateValue1;
}
 [DataMemberAttribute,
SysOperationLabelAttribute('Update to'),
SysOperationHelpTextAttribute('Enter the new date'),
SysOperationDisplayOrderAttribute('1')]

public TransDate parmUpdateDate(TransDate _dateValue = dateValue2)
{
    dateValue2 = _dateValue;
    return dateValue2;
} 

Menu Item





Final Result: Run Job by Open Action Menu Item






Scenario 3: Add custom validation to dialog fields

On Parameter dialog many times inputs needs to be validated according to business logic. This can be achieved by using UI Builder class. It helps in creation of dynamic/interactive parameters.

Batch Validation Message D365 F&O
Batch SysOperation Framework VS Project

Controller class

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
public class TestControllerClass extends SysOperationServiceController
{
    public void new()
    {
        super();
     
        this.parmClassName(classStr(TestServiceClass));
        this.parmMethodName(methodStr(TestServiceClass, processOperation));
     
        this.parmDialogCaption("Batch Dialog Title");
    }
 
    public ClassDescription caption()
    {
        return "Batch Task Description";
    }
 
    public static void main(Args args)
    {
        TestControllerClass controller;
     
        controller = new TestControllerClass();
        controller.startOperation();
    }
 
}

Service Class

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
public class TestServiceClass extends SysOperationServiceBase
{
     
    public void processOperation(TestContractClass _contract)
    {
        //Input any business process logic that need to be scheduled on batch
        info ("Running Batch Operation");
 
     
        info ("Date: " + date2Str
            (_contract.parmDate(),
            213,
            DateDay::Digits2,
            DateSeparator::Dot,
            DateMonth::Long,
            DateSeparator::Dot,
            DateYear::Digits4));
     
        info ("Text: " + _contract.parmText());
    }
 
}

Data contract Class

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
[DataContractAttribute,
SysOperationContractProcessingAttribute(classStr(TestUIBuilderClass))]
public class TestContractClass
{
    TransDate      dateValue;
    Description255 textValue;
 
 
    [DataMemberAttribute,
    SysOperationLabelAttribute('Date'),
    SysOperationHelpTextAttribute('Enter a date in past'),
    SysOperationDisplayOrderAttribute('1')]
    public TransDate parmDate(TransDate _dateValue = dateValue)
    {
        dateValue = _dateValue;
        return dateValue;
    }
 
    [DataMemberAttribute,
    SysOperationLabelAttribute('Text'),
    SysOperationHelpTextAttribute('Type some text'),
    SysOperationDisplayOrderAttribute('2')]
    public Description255 parmText(Description255 _textValue = textValue)
    {
        textValue = _textValue;
        return textValue;
    }
 
}

UI builder Class

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
public class TestUIBuilderClass extends SysOperationAutomaticUIBuilder
{
    DialogField dateField;
 
     
    public void postBuild()
    {
        super();
     
        // get references to dialog controls after creation
        dateField = this.bindInfo().getDialogField(this.dataContractObject(),
                                    methodStr(TestContractClass, parmDate));
    }
 
    public void postRun()
    {
        super();
     
        // register overrides for form control events
        dateField.registerOverrideMethod(methodstr(FormDateControl, validate),
                methodstr(TestUIBuilderClass, validateDate), this);
    }
 
    public boolean validateDate(FormDateControl _dateControl)
    {
        if (_dateControl.dateValue() > today())
        {
            error("Please select a valid date");
            return false;
        }
  
        return true;
    }
 
}

Menu Item

Name: TestSysOperationBatchMenu
Type: Action menu item
Object type: Class
Object: TestControllerClass

So you can use any of the shared scenarios, according to requirement to facilitate your customization.

Table Browser in Dynamics 365FO

Copy D365FO Table Browser URL /?cmp=usmf&mi=SysTableBrowser&tablename= URL :  https://usnconeboxax1aos.cloud.onebox.dynamics.com...