Skip to main content

Notifications

Multi Selection on a form by user and get selected records by X++ code

Context

We might have scenario where we need to customize a form to add a button so that it should have multiselect capability and for example some functionality should happen on multi select like to get all selected records by user so that scenario is to send them as a list to some external service for integration.

Or we might also have a scenario so that for example if we are generating packing slip, then we only have to select all Saleslines with same warehouse. In such scenarios also we will go by multi select.

Ideal way would be to create a button by setting multi select property to yes.

But question is how to get all selected records in a form by code?

What are the best ways to do?

This can be done by either event handler like onclicked event handler or the pre coc on clicked method of the button.

Basically, we can do this by MultiSelectionHelper class. But we also have another way.

So how to do that then?

For this we need to get the formdatasource from the FormControl class and FormDataSource class as shown in the below codes.

When I have above requirements for the first time, I have written my code using while loop. For the Sales order line validation, I choose the For loop. Comparatively For loop is faster.

To not to change the basic theme of this blog, I will only explain how to do multiselect via X .

Below code gives the idea how to get the selected records on a form.

Here the main idea is to get the formdatasource and from that we need to assign that buffer to the Table buffer.

The buffer will increment with new record and shows all the records selected by the user.

With While Loop:

FormDateSource  salesLine_DS;
SalesLine       salesLine;

salesLine = salesLine_DS.getFirst(1) ? 
                salesLine_DS.getFirst(1) :
                    salesLine_DS.cursor();

while (salesLine)
{
    info(salesLine.LineNumber);
    salesLine = salesLine_DS.getNext();
}

With For Loop:

[ExtensionOf(formControlStr(SalesTable,buttonUpdateInvoice))]
final class MYSalesTable_buttonUpdateInvoice_Extension
{
    public void clicked()
    {
        SalesLine               salesLine;
        FormControl             formButtonControl = any2Object(this) as FormControl;
        FormDataSource salesLine_ds = formButtonControl.formRun().dataSource(tableStr(SalesLine));

        for (salesLine = salesLine_ds.getFirst(1); salesLine; salesLine = salesLine_ds.getNext())
        {
                 //Here you can test the working by adding some unique ID. For example Sales Id and line Number in a info log.
        }
        next clicked();
    }

}

Comments

*This post is locked for comments