Dynamics 365 FO - Classes with data - Run Base Framework - Microsoft Dynamics 365 Vietnam

Microsoft Dynamics 365 Vietnam

Song Nghia - Microsoft Dynamics 365 Vietnam

Breaking

Saturday, September 17, 2022

Dynamics 365 FO - Classes with data - Run Base Framework

 Dynamics 365 FO - Classes with data - Run Base Framework

Nghia Song -  Microsoft Dynamics 365 Technical Consultant

Nghia Song

Tel - WhatsApp: +84967324794

Email: songnghia.uit@gmail.com


Run Base Framework

The RunBaseclass is an abstract class which defines a common structure for all data manipulation functions in Microsoft Dynamics AX. Using this framework has the following advantages: 

  • The framework ensures that all updates are structured equally. 

  • Sharing common code through inheritance. 

  • A common dialog layout and functions for all updates. 

  • Automatic "memory" of the user selections from last run. 

  • Easy implementation of batch execution (when using child class RunBaseBatch). 

  • Built-in support for common tasks like query criteria selection and progress bar.

Requirement example:

  • The user is prompted to enter a customer account and a period specified by a from date and a to date. 

  • The data manipulation sums up all customer transactions specified by the account and period. 

  • The sum is presented in the infolog.

The minumum RunBase Implementation

The minimum implementation of a RunBase class consists of the following components: 

  • ClassDeclaration 

  • pack 

  • unpack 

  • run 

  • description (static) 

  • main (static) 

The following dialog methods are not explicitly required in a minimum 

implementation, but we will explore them as part of this example: 

  • dialog 

  • getFromDialog 

If the dialog methods are not overridden, this provides a standard blank dialog.

  1. ClassDeclaration

The classDeclaration consists of three types of definitions: 

  • Variables used to create fields in the dialog. 

  • Variables used within the data manipulation . 

  • Local macro to define which variables to pack (in other words, remember for next time, and/or use on the batch server).


public class DemoRunBase extends RunBaseBatch

DialogField dialogAccount; 

DialogField dialogFromDate; 

DialogField dialogToDate; 


CustAccount custAccount; 

FromDate fromDate; 

ToDate toDate; 


#DEFINE.CurrentVersion(1) 

#LOCALMACRO.CurrentList 

custAccount,

fromDate, 

toDate 

#ENDMACRO

}

The individual fields of the dialog will be initialized in the method dialog(). When the dialog is accepted by the user, the contents of the dialog fields are read in the method getFromDialog(). As both methods have to access the same variables, they are defined as members of the class. 

The manipulation in the method run()uses the information from the dialog. This information is stored in variables with specific data types. These variables are initialized in the method getFromDialog()and read in the method run(). 

The information for the last used dialog values is packed into a container structure. The variables that should be packed are defined in the CurrentList macro in the Runable .

  1. Dialog

This method builds a dialog and initializes the fields that will be used to capture data from the user. These variables can be automatically initialized with the same data selected in the last run.

protected Object dialog() 

DialogRunBase dialog; 

DialogGroup groupPeriod; 


dialog = super(); 


dialogAccount = dialog.addFieldValue(extendedTypeStr(CustAccount), custAccount); 


groupPeriod = dialog.addGroup("Period"); 


dialogFromDate = dialog.addFieldValue(extendedTypeStr(FromDate), fromDate, "Period from"); 


dialogToDate = dialog.addFieldValue(extendedTypeStr(ToDate), toDate, 

"Period to"); 


return dialog;

}

  1. GetFromDialog

This method is called immediately after the dialog is accepted by the user, and before the run()method is called. It is used to transfer the information from the dialog fields into the class variables.

public boolean getFromDialog() 

boolean ret; 

ret = super(); 


custAccount = dialogAccount.value(); 

fromDate = dialogFromDate.value(); 

toDate = dialogToDate.value(); 


return ret; 

}


  1. Pack

The task for this method is to return a container with the information. This can be used to initialize a similar data manipulation on another computer and/or at another time

public container pack() 

return [#CurrentVersion,#CurrentList]; 

return [1,list<’C0001’,’01/01/2015’, ‘30/01/2015’>];

}

  1. Unpack

It receives a container of pack method as a parameter and restores the type specific variables of the class. The method returns a Boolean with the value true if the information could be restored.

public boolean unpack(container _packedClass) 

Version version = RunBase::getVersion(_packedClass); 

switch (version) 

case(#CurrentVersion) :

[version,#CurrentList] = _packedClass; 

break; 

default : 

return false; 

return true; 

}

  1. Run

The run()method controls the data manipulation. The method can use the variables defined in the classDeclaration and initialized from the dialog. The method does not receive any formal parameters

public void run() 

CustTrans custTrans; 

select sum(AmountMST) from custTrans 

where custTrans.AccountNum == custAccount 

&& custTrans.TransDate >= fromDate 

&& custTrans.TransDate <= toDate; 

info(strFmt("Sum equals %1", custTrans.AmountMST)); 

}

  1. Description

The description() method returns a descriptive name for the data manipulation. The value is used to identify the job inthe batch queue and is used as the caption in the dialog.

static client server ClassDescription description() 

return "Sum customer transactions"; 

}

  1. Main

The main() method is also responsible for calling prompt(), which executes the dialog, and then calling run()to perform the manipulation.

static void main(Args _args) 

DemoRunBase demoRunBase; 

demoRunBase = new DemoRunBase(); 

if (demoRunBase.prompt()) 

demoRunBase.run(); 

}

Next:Dynamics 365 FO - Create simple runbase

No comments:

Post a Comment