October 15, 2018

Table Event handler methods in Dynamics 365 FO

Hi Guys,

In my last post Form Event hander methods in Dynamics 365 FO, we discussed different event handlers which available on Form.  Let’s continue this discussion with Table event handler method today.
Let’s see the different type of event handler in today’s post.
You need to add a new class to write the event handler methods. I would recommend adding one class to one table. To make it easy for another developer postfix the name by EH or eventHander or Hander so your teammate can identify if they need to add more business logic than create a new class from scratch.

1. Table onValidateField event handler



2. Table OnValidated event hander (same logic you can try for many other event hander)
[DataEventHandler(tableStr(InventLocation), DataEventType::ValidatedField)]
public static void InventLocation_onValidatedField(Common sender, DataEventArgs e)
{
InventLocation inventLocation = sender as InventLocation;
ValidateFieldEventArgs fieldArgs = e;
boolean ret;
InventLocation inventLocationLoc;
switch(fieldArgs.parmFieldId())
{
case fieldNum(InventLocation, field1):
if(inventLocation.MyWorkerAssociate != '')
{
<Your code/ business ogic/validation>
<ret = true or false>
fieldArgs.parmValidateResult(ret);
}
}
}

Let me know if it helps you or you got some more example.

Cheers,
Harry
Follow us on Facebook to keep in rhythm with us. @Facebook

September 23, 2018

RecordInserList class for optimized insert operation


Hi Folks,

RecordInsertList class is the really useful and fastest way to insert records in AX table(s). This method is useful you don't want to run any validation while insertion for an example; insert in a temp table or SSRS reports. 

Below is the simplest example of set based RecordInserList class,
void copyVendor()
{
    VendTable    sourceVend;
    MyTmpTable   targetVend;

    //RecordInserList calss decarion
    RecordInsertList VendList;

  //Object initialization
    VendList = new RecordInsertList(tableNum(Vendtable));

    while select sourceVend
    where sourceVend.vendgroupId == ‘Local’
    {
        targetVend.data(sourceVend);
        targetVend.vendorAccount = sourceVend.vendorAccount;
           .
           .
           .
           .
        VendList.add(targetVend);
    }
    VendList.insertDatabase(); //mandatory call
}


Another example from MSDN

public void tutorialRecordInsertList()
    {
        MyTable myTable;
        RecordInsertList insertList = new RecordInsertList(
            myTable.TableId,
            True);
        int i;
      
        for ( i = 1; i <=  100; i++ )
        {
            myTable.value = i;
            insertList.add(myTable);
        }
        insertList.insertDatabase();
    }

RecordInserList class will capture all record in temp and hit The DataBase only once when we call insertDatabase(). add() insert certain blocks followed by insertDatabase to complete the insert operation. 
This is how we can reduce the SQL call and optimize the performance as well.

Cheers,
Harry

Follow us on Facebook to keep in rhythm with us. @Facebook


September 09, 2018

Form Event handler methods in Dynamics 365 FO

Hi Guys,

Let’s discuss today the different event handlers in Dynamics 365 FO. Now we don't have the leverage 😉to overwrite existing code anymore so all you have is event handlers to manipulate standard functionality. However, event handlers were available in the earlier version as an optional and best practice but now it’s the only option. I will be sharing different event handler so we can cover most of the methods with different parameters.


1. Form dataSource event handler: Here I will be using the vendTable form to get table buffer and perform additional business logic or validation.

Vendtable = Form name
vendTable = form data source name
[FormDataSourceEventHandler(formDataSourceStr(Vendtable, vendTable), FormDataSourceEventType::Written)]
public static void vendTable_OnWritten(FormDataSource sender, FormDataSourceEventArgs e)
{
    FormRun                 form           = sender.formRun();
    FormDataSource          vendTable_ds =       form.dataSource(formDataSourceStr(Vendtable,Vendtable)) as FormDataSource;
   Vendtable     Vendtable= vendTable_ds.cursor();
<Now to got table buffer, perform business logic/validation>
}
lets see one more example below;

2. Form DataSource while closing the form (same will apply for OnInitialized,
[FormEventHandler(formStr(EcoResAttributeValue), FormEventType::Closing)]
public static void EcoResAttributeValue_OnClosing(xFormRun sender, FormEventArgs e)
{
     FormDataSource ecoResProduct_ds   =          sender.dataSource(formDataSourceStr(EcoResAttributeValue, EcoResProductAttributeValue));
      EcoResProductAttributeValue      ecoResAttributeValue = ecoResProduct_ds.cursor();
<YOUR CODE>
}   

3.  Post-event hander of Form init or any other standard method method
[[PostHandlerFor(formStr(Ledger), formMethodStr(Ledger, init))]
    public static void Ledger_Post_init(XppPrePostArgs _args)
    {
        #ISOCountryRegionCodes
        FormRun form = _args.getThis();
        FormDesign design = form.design();
        FormControl revaluationAccount = design.controlName(formControlStr(Ledger, revaluationAccount));
        FormControl currencyRevaluation = design.controlName(formControlStr(Ledger, currencyRevaluation));

<your code>
}

4. Form control event hander:
[FormControlEventHandler(formControlStr(LogisticsPostalAddress, Roles), FormControlEventType::Modified)]
    public static void Roles_OnModified(FormControl sender, FormControlEventArgs e)
    {
        FormRun element = sender.formRun();
        FormControl purposeCtrl   = element.design().controlName(formControlStr(LogisticsPostalAddress, Roles));
       
        FormDataSource logisticsLocation_DS = element.dataSource(formDataSourceStr(LogisticsPostalAddress, LogisticsLocation));
       <business logic>
    }


Cheers,

Harry
Follow us on Facebook to keep in rhythm with us. @Facebook

August 12, 2018

Edit/Display or new methods on standard application objects

Hi Folks,
Let’s see how to write a new method on standard application object using the extension framework. An extension enables you to add functionality to existing model elements and source code.
Before starting adding new method, you need to create a new extension class for your object and this class should be declared as final and post fixed by ‘Extension’ keyword. See below example,

For Table:
[ExtensionOf(tableStr(CustTable))]
final class MyCustTable_Extension
{
<new methods>
}

For Class:
[ExtensionOf(classStr(CustExchAdj))]
final class MyCustExchAdj_Extension
{
<new methods>
}

For Form:
[ExtensionOf(formStr(VendParameters))]
final class MyVendParameters _Extension
{
<new methods>
}

Now lets see how to add different types of methods

1. New edit method
For Extension methods, the first argument in method parameter must be the object you're extending e.g. table buffer. See below code for reference;
public static edit str60 MyFieldEdit(<tableName> _this, boolean _set, name value)
{
MyTable mytable = <tablename>;
if(_set)
{
if(value)
{
ttsbegin;
custTable = CustTable::find(<tablename>.CustAccount,true);
custTable.Name = value;
custTable.update();
ttscommit;
}
}
}

2. New display method
public static display EcoResDescription productName(InventSum _this)
{
Return _this.itemName();
}

3. Normal method
In the same way, you can write a new method, this method also needs to be static so it can be accessed on the form.
Public static updateSearchName(_this, Name _newName)
{
Mytable mytable = _this;
If(_newname)
{
Ttsbegin;
Mytable.searchName = _ newname;
MyTable.update();
Ttscommit;
}
}

Edit method on table extension or new methods on table extension, all method needs to be static. Now to access these methods, go to your form and add new field type of string/real/XYZ. Set below property
clip_image002

Cheers,
Harry