Thursday, June 28, 2012

AX2012 - How to control the visibility of FactBox on form thru X++


Before discussing the code, let me point out that this is not recommended, since the user should have control over the visibility of FactBoxes so your design should always take this into consideration; Having said that, if you for some reason want to control the visibility, here’s how to do it:

void toggleFormPartVisibility(boolean _isVisible = false)
{
    PartList                partList;
    FormRun                 formPart;
    str                     formName;
    int                     counter;    
   
    partList = new PartList(element);
   
    //formName is the name of the form that the factbox is referring to.
    formName = formStr(SomeFormPart);
   
    for(counter = 1; counter <= partList.partCount(); counter++)
    {
        if(partList.getPartById(counter).name() == formName)
        {
            formPart = partList.getPartById(counter);
            formPart.design().visible(_isVisible);
            break;
        }
    }
}

Other properties can also be accessed this way, e.g. if you want to refresh the FormPart’s data source:
formPart.dataSource().reread();
formPart.dataSource().refresh();

This will cause some performance issues due to extra RPC calls, so you should have a good reason to take this approach.

Experiment with the other properties and share how it goes.