Skip to main content

Notifications

Announcements

No record found.

Create master setup form from master table with X++ code

This code is used to create a simple form with overview and general tab when table name is passed to it. Before creating the form make sure the table contains overview, identification and description field group as per the best practice. Also check that label of Table is given which help in creating the caption of form.

#AOT
static void CreateFormfromTable(Args _args)
{
    SysDictTable            dictTable; //= new SysDictTable(tableNum(AVATest));
    FormBuild               formBuild;

    Form                    form;
    FormBuildDesign         formBuildDesign;
    FormBuildTabControl     formBuildTabControl;
    FormBuildTabPageControl formBuildTabPageControl;
    FormBuildGridControl    formBuildGridControl;
    FormBuildGroupControl   formBuildGroupControl;
    MenuItemBuild           menuItemBuild;
    MenuFunction            menuFunction;

    Set                     fieldGroupStr = new Set(Types::String);
    SysDictFieldGroup       dictFieldGroup;
    SetEnumerator           setEnum;// = dictTable.fieldGroups().getEnumerator();
    Dialog                  dialog = new Dialog("Form Creater");
    DialogField             dialogField_TableName;
    str                     tableName
    ;
    dialogField_TableName = dialog.addField(typeid(Name),"Table Name");
    if(dialog.run())
    {
        tableName = dialogField_TableName.value();
        if(!tableName)
            throw error("Please enter table name");

        dictTable = new SysDictTable(tableName2id(tableName));
        if(!dictTable)
            throw error("Check your table name");

        setEnum = dictTable.fieldGroups().getEnumerator();
        if (TreeNode::findNode(#FormsPath).AOTfindChild(dictTable.name()) ||
            TreeNode::findNode(#MenuItemsDisplayPath).AOTfindChild(dictTable.name()))
            throw error("Has already been created");

        while (setEnum.moveNext())
        {
            dictFieldGroup = setEnum.current();
            fieldGroupStr.add(dictFieldGroup.name());
        }
        formBuild = new FormBuild(dictTable.name());
        form = formBuild.form();
        form.addDataSource(dictTable.name());
        formBuildDesign         = form.design();
        info(dictTable.labelLabelId());
        formBuildDesign.caption(dictTable.labelLabelId());
        formBuildDesign.titleDatasource(dictTable.name());
        formBuildTabControl     = formBuildDesign.addControl(FormControlType::Tab, "@SYS78488");
        formBuildTabControl.widthMode(FormWidth::ColumnWidth);
        formBuildTabControl.heightMode(FormHeight::ColumnHeight);

        formBuildDesign.addControl(FormControlType::ButtonGroup, 'ButtonGroup');

        formBuildTabPageControl = formBuildTabControl.addControl(FormControlType::TabPage, 'TabOverview');
        formBuildTabPageControl.caption(literalstr('@SYS34991'));
        formBuildTabPageControl.widthMode(FormWidth::ColumnWidth);
        formBuildTabPageControl.heightMode(FormHeight::ColumnHeight);

        formBuildGridControl    = formBuildTabPageControl.addControl(FormControlType::Grid, '@SYS26263');
        formBuildGridControl.widthMode(FormWidth::ColumnWidth);
        formBuildGridControl.heightMode(FormHeight::ColumnHeight);
        if (fieldGroupStr.in('@SYS34991'))
        {
            formBuildGridControl.dataSource(dictTable.name());
            formBuildGridControl.dataGroup('@SYS34991');
            formBuildGridControl.autoDataGroup(true);
        }

        formBuildTabPageControl = formBuildTabControl.addControl(FormControlType::TabPage, 'TabGeneral');
        formBuildTabPageControl.caption(literalstr('@SYS81043'));
        formBuildTabPageControl.widthMode(FormWidth::ColumnWidth);
        formBuildTabPageControl.heightMode(FormHeight::ColumnHeight);

        if (fieldGroupStr.in('@SYS36186'))
        {
            formBuildGroupControl   = formBuildTabPageControl.addControl(FormControlType::Group, '@SYS36186');
            formBuildGroupControl.dataSource(dictTable.name());
            formBuildGroupControl.dataGroup('@SYS36186');
        }

        if (fieldGroupStr.in('@SYS80470'))
        {
            formBuildGroupControl   = formBuildTabPageControl.addControl(FormControlType::Group, '@SYS80470');
            formBuildGroupControl.dataSource(dictTable.name());
            formBuildGroupControl.dataGroup('@SYS80470');
        }
        form.save();
        form.AOTcompile();

        menuItemBuild = new MenuItemBuild(dictTable.name(), MenuItemType::Display);
        menuFunction  = menuItemBuild.menuFunction();
        menuFunction.label(dictTable.labelLabelId());
        menuFunction.objectType(UtilElementType::Form);
        menuFunction.object(dictTable.name());
        menuFunction.AOTsave();

        TreeNode::findNode(#FormsPath).AOTrefresh();
        Info(strfmt(" Form %1 is created",tableName));
    }

}

Comments

*This post is locked for comments