Showing posts with label List page interaction class. Show all posts
Showing posts with label List page interaction class. Show all posts

Wednesday, May 14, 2014

Set Value On a Control in List Page through ListPage Interaction class (Dynamics AX 2012)

I recently had a requirement to display sum of total volume (a display method) of all the selected records on a List page form. There were two major development tasks in this requirement

  1. To loop through all records that user has selected on List Page.
  2. To set value on a custom control of List Page after calculation.
You can easily loop through list of selected records with in interaction class by using MultiSelectionHelper class. However, I found it a bit tricky in setting up the control value of List page as previously I only used this class to enable/disable or make visible true false of controls or buttons on list page.

Here is the code for your reference if you ever need to do the same.
For sure there will be many other more effective ways of doing this. If you ever had to do and know the best way please share with me as well.

protected void setSumOfTotalVolume()
{
    ReqPO               reqPO;
    Volume              totalVolume;
    Common              externalRecord;
    FormDataSource      frmDs;
    FormRun             formRun;
    FormRealControl     frmCtrl;
    MultiSelectionHelper helper = MultiSelectionHelper::construct();

    helper.parmDatasource(this.listPage().activeRecord('ReqPO').dataSource());

    reqPO = helper.getFirst();
    while (reqPO.RecId != 0)
    {
        totalVolume += reqPO.totalVolume();
        reqPO = helper.getNext();
    }

    //Assign value to control on list page
    externalRecord = this.listPage().activeRecord('ReqPO');

    if(externalRecord.isFormDataSource())
    {
        frmDs   = externalRecord.dataSource();
        formRun = frmDs.formRun();
        if(formRun)
        {
            frmCtrl = formRun.design().controlName(formControlStr(ReqTransPOListPage,TotalVolumeField));
            if(frmCtrl)
            {
                frmCtrl.realValue(totalVolume);
            }
        }
    }
}

You should also call this method from interactionclass.selectionchanged() method to update value on every record selection.

Hope it will be help full for if you have a similar requirement in future.

Happy DAXing !!