Monday, August 6, 2018

D365FO - Extension method data accessor examples


Six months into learning how to transition from AX 2012 X++ to D365FO X++ one of the things I have been struggling with the new extension model is how many different code structures you need in order to access the calling method’s property’s/datasource when subscribing to various types of events or methods. So I started to log down anytime I discover a new method. As I am sure there are plenty of other ones that I am missing as I have only touched the tip of the iceberg when it comes to field events but I decided it would be a good time to post this information as a reference. There is more than likely better ways to do some of these however I am posting this as a starting point.

I will be updating this post as my experience with D365FO grows as well

Source Event Parm Example
Class Pre/Post event XppPrePostArgs Get args and parmameter values from a method that is being extended. Parm 1 = Object Parm 2 = Common
PurchCreateFromSalesOrder callingClass = args.getThis() as PurchCreateFromSalesOrder;       
Object callerObject = args.getArgNum(1) as Object;
Common callerRecord = args.getArgNum(2) as Common;
Class Pre/Post event XppPrePostArgs Class example: SalesLineType salesLineType = args.getThis() as SalesLineType; 
Class main args Getting the caller record that is sent to a class via args. This is the same as 2012 however args.record().datasource() is now deprecated
//check the caller
if(_args.callerName() == formStr(SalesTable))
{
        //check to see which dataset type was passed
        if(_args.dataset() == tableNum(SalesLine))
        {
                //get the forms datasource record (FormDataUtil::getFormDataSource() replaced _args.record().datasource())
               FormDataSource salesLineDS = FormDataUtil::getFormDataSource(_args.record());
        }
}
Form Initialized xFormRun FormDataSource purchLine = sender.dataSource(formDataSourceStr([formname],[table]));
Form DataSource FormDataSource FormDataSource formDS = sender.formRun().dataSource(formDataSourceStr(EcoResProductDetailsExtended, MHSmartATPItemSettings));
MHSmartATPItemSettings smartATPItemSettings = formDS.cursor();
Form DataSource Field FormDataObject FormDataSource formDS = sender.datasource();
PurchLine purchLine = formDS.cursor();
Form Form Control FormControl FormRun formRun;
FormControl formControl;
formRun = sender.formRun();
formControl = FormRun.design().controlName(formControlStr(<form name>, <control name>));
someVariable = formControl.valueStr();
Form onClicked FormControl  FormRun formRun = sender.formRun();
 FormDataSource formDSSalesTable = formRun.dataSource(formDataSourceStr(SalesTable, SalesTable));
 FormDataSource formDSSalesLine = formRun.dataSource(formDataSourceStr(SalesTable, SalesLine));
       
 SalesTable salesTable = formDSSalesTable.cursor();
 SalesLine salesLine = formDSSalesLine.cursor();
Form Pre/Post event XppPrePostArgs FormRun formRun = args.getThis();
FormDataSource formDSLogisticsPostalAddress = formRun.dataSource(formDataSourceStr(LogisticsPostalAddress, LogisticsPostalAddress));
LogisticsPostalAddress logisticsPostalAddress = formDSLogisticsPostalAddress.cursor();
Table onDelete Common PurchLine purchLine = sender as PurchLine;
Table Modified Field Value Common TableName itemSettings = sender as TableName;
ModifyFieldValueEventArgs fieldEvent = e as ModifyFieldValueEventArgs;

  //check to see which field was modified
  switch(fieldEvent.parmFieldName())
        {
            case fieldStr([tablename], [fieldname]):
            ...do stuff
            break;
        }
Table ValidateFieldValue Common/DataEventArgs ValidateFieldValueEventArgs fieldEvent = e;
boolean isValid;
PurchLine purchLine = sender as PurchLine;
       
//declare the checkFailed      
isValid = checkFailed("some error event");
//save the result
fieldEvent.parmValidateResult(isValid);
Table Pre/Post event XppPrePostArgs PurchLine purchLine = args.getThis() as PurchLine;
Form Getting different datasource from calling object FormDataObject Get different datasource from formdataobject. Such as modified InventDim.InventLocationId -> PurchLine
 FormDataSource formDS = sender.datasource();
InventDim inventDim = formDS.cursor();
FormRun formRun = sender.datasource().formRun();
FormDataSource formPurchLineDS = formRun.datasource(formDataSourceStr(PurchTable, PurchLine));
PurchLine purchLine = formPurchLineDS.cursor();

It is good to note that when it comes to accessing anything on a form I have realized the key component is getting access to the FormRun object. Once you have access to that then you can really access anything that is public on the form such as controls or datasources

Update 8/7/18: added class args example as args.record().datasource() is now deprecated
Update 1/23/19: added getting different datasource example than the sender's main common source

No comments:

Post a Comment