Wednesday, April 25, 2018

How to override a clicked method of a button in a form Extension D365

Requirement is to add a button on CustInvoiceJour form and override the clicked method, so I would be creating an extension of that form and will add button there.

1. Create extension of 'CustInvoiceJour' form


2. Open extension of that form from the solution explorer and add a button



3. We have added a button, now we have to perform some operations on click of that button. And for that we need to write the 'OnClicked' event. In order to do that, expand the 'Event's under that control and right click on 'onClicked' event and select 'Copy Event Handler Method'

4. 'OnClicked' event handler is copied and now you can paste as a method in any of your custom class or you can create a new class. I am using one of my custom classes.

5. Now the question is, how we will get the selected record on that form or how would we get the form data source, if you take a look of this above method, you will seen we have a formContol as a parameter and by using that we can get the form data source and selected records.


After getting the table buffer and form data source, we can perform other operations.

Tags : X++ , D365, Form extensions, Events, OnClicked

Wednesday, April 18, 2018

Add controls run time and override the method in D356 (X++)

Here I will be explaining, how to create buttons run time and override the clicked method.

We would be creating buttons run time based on the setup companies in some custom table, Like I have table called 'DemoCompanyList' and that table has 3 companies setup

1. USMF
2. USMG
3. IND

Lets create a form and give a name 'DemoRunTimeControl' and then we can apply the pattern based on our requirement and then add Action Pane - Button Group - Menu Button(control name - ButtonHeaderInquiry), change the AutoDeclaration property to 'Yes' for this control.

Form Methods - 


init() - To get the list of companies and call method addRunTimeButtons()

addRunTimeButton() - In this method we will add buttons to menu button and also register the clicked method so that when we click on those buttons, it should the written clicked method.

RunTimeButton_clicked() - In this method we will opening the custInvoiceJour menu item based on the clicked button.


[Form]
public class DemoRunTimeControl extends FormRun
{
    Map                 buttonCompany;
   
    public void init()
    {
        container   companies;
        counter     i;

        super();

        buttonCompany = new Map(Types::Integer, Types::String);

        companies = DemoCompanyList::findCompanyList(); // Find company list will return the list of the companies

        for (i = 1; i <= conLen(companies); i++)
        {
            element.addRunTimeButton(conPeek(companies, i)); //addRunTimeButtons will add buttons based on the company setup
        }
    }

    private void addRunTimeButtons(DataAreaId _dataAreaId = #emptyString)
    {
        FormButtonControl           runTimeControl;
        #define.runTimeCtrl         ('DemoRunTimeButton')

        runTimeControl = ButtonHeaderInquiry.addControl(FormControlType::Button, #runTimeCtrl + _dataAreaId);
        //Invoice (%1)
        runTimeControl.text(strfmt("Customer Invoice (%1)", _dataAreaId));
        runTimeControl.registerOverrideMethod(methodStr(FormButtonControl, clicked), formMethodStr(DemoRunTimeControl, 'RunTimeButton_clicked'), element);
        buttonCompany.insert(runTimeControl.id(), _dataAreaId);
    }

    public void RunTimeButton_clicked(FormButtonControl _formButtonControl)
    {
        //Set the local control to the actual control that is calling the method.
        _formButtonControl = this.controlCallingMethod();
        CompanyId company;

        company = buttonCompany.lookup(_formButtonControl.id());

        if (company)
        {
            changecompany (company)
            {
                //Open invoice journal in specific company
                new MenuFunction(MenuItemDisplayStr(CustInvoiceJour),MenuItemType::Display).run();
            }
        }

    }

}



When you will click on first button, system will open customer invoice journals form in USMF company and the same way if you click on IND one, system will open journals from IND company.