Wednesday, October 12, 2022

To add dynamics default controls on UI in D365FO

 Reference Class:

SysSystemDefinedButtons


Sample code:

[ExtensionOf(classStr(SysSystemDefinedButtons))]

internal final class SysSystemDefinedButtonsCls_SAN_Extension

{

    private static str SANDE_SystemDefinedButtonsActionPane = "SystemDefinedButtonsActionPane";

    private static str SAN_SystemDefinedButtonsActionPane = "SAN_SystemDefinedButtonsActionPane";

    private static str SAN_SystemDefinedFormButtonGroup = "SAN_SystemDefinedButtonsButtonGroup_Form";

    private static str SAN_SystemDefinedWorkFlowButton = "SAN_SystemDefinedButtonWorkflow";

    private static str SAN_SystemDefinedWorkdlowActionPaneTab = "SAN_SystemDefinedWorkflow";

    private static str SAN_SystemDefinedWorkflowGroup = "SAN_SystemDefinedWorkflowGroup";


    public void addSystemDefinedButtons()

    {

        next addSystemDefinedButtons();


        if (this.isFormStyleSupportedForSystemDefinedButtons())

        {

            FormActionPaneControl actionPane;

            List listOfActionPanes;

            ListEnumerator apListEnumerator;


            listOfActionPanes = this.san_GetActionPanesForSystemButtons();

            apListEnumerator = listOfActionPanes.getEnumerator();


            while (apListEnumerator.moveNext())

            {

                actionPane = apListEnumerator.current();

                this.san_AddButtonsToActionPane(actionPane);

            }

            

        }

    }


    private List san_GetActionPanesForSystemButtons()

    {

        List listOfActionPanes = new List(Types::Class);

        FormActionPaneControl actionPane;

        Object childControl;

        FormRun form = this.getFormRun();

        

        for (int i = 1; i <= form.design().controlCount(); i++)

        {

            childControl = form.design().controlNum(i);


            if (classidget(childControl) == classnum(FormActionPaneControl))

            {

                actionPane = childControl as FormActionPaneControl;


                if (actionPane.Style() == ActionPaneStyle::Standard)

                {

                    listOfActionPanes.addEnd(actionPane);

                }

            }

        }


        if (listOfActionPanes.empty())

        {

            actionPane = form.design().addControl(FormControlType::ActionPane, SANDE_SystemDefinedButtonsActionPane);

            form.design().moveControl(actionPane.id());


            listOfActionPanes.addEnd(actionPane);

        }


        return listOfActionPanes;

    }


    private void san_AddButtonsToActionPane(FormActionPaneControl _actionPane)

    {

        FormButtonGroupControl buttonGroup;

        FormMenuButtonControl menuButton;

        FormCommandButtonControl commandButton;

        FormRun formRun = this.getFormRun();


        if (this.getFormStyle() != FormStyle::Workspace)

        {

            this.SAN_AddWorkflowsTab(_actionPane);

        }

    }


    private void SAN_AddWorkflowsTab(FormActionPaneControl _actionPane)

    {

        FormButtonGroupControl buttonGroup;

        FormMenuButtonControl menuButton;

        FormActionPaneTabControl workflowTab = _actionPane.addControl(FormControlType::ActionPaneTab, this.SAN_CreateUniqueName(SAN_SystemDefinedWorkdlowActionPaneTab));

        workflowTab.allowUserSetup(FormAllowUserSetup::No);

        FormRun form = this.getFormRun();


        workflowTab.caption("Custom Workflow");

        workflowTab.hideIfEmpty(true);


        buttonGroup = workflowTab.addControl(FormControlType::ButtonGroup, this.SAN_CreateUniqueName("SAN_SystemDefinedWorkflowGroup"));

        buttonGroup.allowUserSetup(FormAllowUserSetup::No);

        buttonGroup.caption("Workflow");


        FormFunctionButtonControl menuItemButton = buttonGroup.addControl(FormControlType::MenuFunctionButton, "SAN_WorflowButton", buttonGroup);

        menuItemButton.menuItemType(MenuItemType::Display);

        menuItemButton.menuItemName(menuItemDisplayStr(<Display menu item>));

        menuItemButton.dataSource(form.dataSource());

        menuItemButton.visible(true);

    }


    private str SAN_CreateUniqueName(str baseControlName)

    {

        str candidateName = baseControlName;

        int i = 1;


        if(this.SAN_isControlFound(candidateName))

        {

            do

            {

                candidateName = strFmt("%1%2", baseControlName, i);

                i++;

            }

            while (this.SAN_isControlFound(candidateName));

        }

        

        return candidateName;

    }


    private boolean SAN_isControlFound(str controlName)

    {

        FormRun form = this.getFormRun();


        boolean isControlFound = false;


        if (form.controlId(controlName) > 0)

        {

            isControlFound = true;

        }

              

        return isControlFound;

    }


}

Friday, October 7, 2022

Globals in D365FO (Implementation samples)

Samples


/// <summary>

    /// Convert the date input value to the string format YYYYMMDD.

    /// </summary>

    /// <param name="_input">Date input value; use today's date, if input is empty.</param>

    /// <returns>Returns the date input value in the string format YYYYMMDD.</returns>

    public static str YYYYMMDD(Date _input = DateTimeUtil::date(DateTimeUtil::getSystemDateTime()))

    {

        str dateString;

        

        dateString  = date2str(_input,

                                321,

                                DateDay::Digits2,

                                DateSeparator::None,

                                DateMonth::Digits2,

                                DateSeparator::None,

                                DateYear::Digits4);


        return dateString;

    }


    /// <summary>

    /// Convert the time input value to the string format hhmmss.

    /// </summary>

    /// <param name="_input">Time input value; use the current time, if the input is empty.</param>

    /// <returns>Returns the time input value in the string format hhmmss.</returns>

    public static str hhmmss(TimeOfDay _input = DateTimeUtil::time(DateTimeUtil::getSystemDateTime()))

    {

        str timeValue;

        str timeString;

        

        timeValue   = time2str( _input,

                            TimeSeparator::Dot,

                            TimeFormat::Hour24);


        timeString  =   substr(timeValue,1,2) +

                    substr(timeValue,4,2) +

                    substr(timeValue,7,2);


        return  timeString;

    }


    /// <summary>

    /// Convert the date and time input to the string format YYYYMMDD_hhmmss.

    /// </summary>

    /// <param name="_input">Date and time input value; use the current system date and time, if the input is empty.</param>

    /// <returns>Returns the date and time input in the string format YYYYMMDD_hhmmss.</returns>

    public static str YYYYMMDD_hhmmss(utcdatetime _dateTime = DateTimeUtil::getSystemDateTime())

    {

        Date        dateValue;

        TimeOfDay   timeValue;

        

        dateValue = DateTimeUtil::date(_dateTime);

        timeValue = DateTimeUtil::time(_dateTime);


        return strfmt('%1_%2', Global::YYYYMMDD(dateValue), Global::hhmmss(timeValue));

    }


    /// <summary>

    /// Convert the date and time input to the string format YYMMDD.

    /// </summary>

    /// <param name="_input">Date and time input value; use the current system date and time, if the input is empty.</param>

    /// <returns>Returns the date and time input in the string format YYMMDD.</returns>

    static str YYMMDD(Date _input = DateTimeUtil::date(DateTimeUtil::getSystemDateTime()))

    {

        str dateString;

        

        dateString  = date2str(_input,

                                321,

                                DateDay::Digits2,

                                DateSeparator::None,

                                DateMonth::Digits2,

                                DateSeparator::None,

                                DateYear::Digits2);


        return dateString;

    }


    public static date getCompanyDate(utcdatetime _input = DateTimeUtil::utcNow())

    {

        return DateTimeUtil::date(DateTimeUtil::applyTimeZoneOffset(_input, DateTimeUtil::getCompanyTimeZone()));

    }


    static str removeTabLineBreaks(str _input)

    {

        #File


        _input  = strreplace(_input, #delimiterEnter, #delimiterSpace);

        _input  = strreplace(_input, #delimiterTab, #delimiterSpace);

        _input  = strreplace(_input, #delimiterCRLF, #delimiterSpace);


        return _input;

    }


public static void setVisibilityForControls(FormRun _formrun, List _listOfControls, boolean isVisible)

    {

        if (_formrun && _listOfControls && !_listOfControls.empty())

        {

            ListIterator iterator = new ListIterator(_listOfControls);

            while(iterator.more())

            {

                FormControlName controlName = iterator.value();

                FormControl control = Global::getFormControl(_formrun, controlName);

                if (Control)

                {

                    control.visible(isVisible);

                }


                iterator.next();

            }

        }

    }


    private static FormControl getFormControl(FormRun _formrun, FormControlName _controlName)

    {

        return _formrun != null ? _formrun.control(_formrun.controlId(_controlName)) : null;

    }



Convert Call stack to readable format in D365FO X++

//Input --container _xppCallStack = xSession::xppCallStack();  Public static str POL_formatXppCallStack(container _xppCallStack, int _skipFr...