How to update certain records in a form using button

In my Form, I need to create a button that will update records which user has selected. At the same time this button will also need to check whether the records selected are under certain condition.

How to do that ?
For example, I have table A that contain 10 records with field : Status as part of the fields.
The button is to change the field’s Status to “Close”

But this button will need to behave if user select only 1 record which actually already has status “Closed”, the button will be disabled. If user select many records, it will be only update those selected records that only has Status not “Close”

Before begin to update, I will need to show a confirmation dialog first, whether user want to proceed or not.

I cannot figure out how exactly to do this in D365 F&O. Please help.
Thanks

Your description covers many things and it’s not clear which ones you need to help with. You need to iterate selected records, evaluate a condition, update a record, show a dialog and so on. I guess you likely know some parts and you’re struggling with just a few.
Just a guess, maybe you don’t know how to get the selected records. MultiSelectionHelper class will help you with it.

Hi,
Yes, it is exactly like you mentioned and no “so on”. And the issue is I don’t how to code all that ? will it need a class ? and how the code looks like ?

The points are like u said:

  1. In my form, there is a grid listing my records in Table A
  2. User can select 1 record or multiple record
  3. I will need to have a button at the top panel
  4. When user select 1 record this button disabled if the Status of the record already “Close” (I’m not sure whether it is possible, though)
  5. When user click the button, it will update selected records, either 1 or multiple, updating the status of selected records to be “Close”, but of course to those records which has not been “Close” yet.
  6. Before doing step 5. a confirmation dialog will pop up first to ask whether she wants to proceed or not.

Thanks

Don’t try to do everything at once. You see that you’re completely stuck of you do. Consider which steps are necessary and deal with them one by one.
For example, ignore requirements 4 and 6 for the moment and focus on the key part - updating of selected records.
Before you can update them, you must be able to get the selected records, therefore that’s the thing your need to address first.
I already told you which class can be used to get selected records.
You can implement your logic either directly in the form, or in a separate class. The latter is a better choice if you want to have the same logic in multiple forms. The exact implementation depends on which approach you choose.
When you’re able to iterate the records, you can start implementing the logic for update.

1 Like

Hi,
Thank you. Problem is I don’t know how to start ? Understand from how I described it looks I know something, but my days in Ax especially development is already far too long, before Ax 2012, and now I see it is far different style.
Currently more of citizen developer in D365 as I’m move on to Power Platform area.
So might need a help here where / what to start.
Thanks

Okay… but you said you have your form, so I thought you know how to create a form and do such things.
Do you know how to start a development environment and create a form or not? Or you know that but not how to add a button to your form?

Yes, for the form, I already have it. In fact, I managed to do it with help of a blog (about those pattern things)

Unfortunately, regarding the specific case of mine, it doesn’t clear what to do. So yes, apart of adding a button to my form, what I know now is have to create a class then set this to a display menu item (this is “fortunately” still the same way as the old Ax way). But how is the class, and how the code will need to be written, this is where I’m lost.

Sorry, I mean Action menu item.

Creating a class is one of the possible approaches I mentioned. It seems you’ve made your choice, so let’s stick to it.

To be able to call the class through a menu item, it must have main() method. Try code like this:

public static void main(Args _args)
{
    info("The button was called!");
}

Then drag your menu item to a button group on your form to create a menu item button, compile your project, run the application and try your button. You should see the infolog message, proving that the code gets called.

ok, but first, what is the “internal final class” thing when we create new class ?

Do I need to use it or something need to change or added ?
For the “main”, I guess this is written in its body of the class, right ?

You can find answers for these questions on internet. It’s not even specific to X++; it’s the same in C#.
A final class is a class that can’t be inherited from.
An internal class is a class that can be accessed only from the assembly (package/module in F&O) where it’s declared.
You can safely ignore both at the moment, because they aren’t related to what you’re doing.

Ok, so in the Main method, how to referring the table I want to update to be one in the form ? because this is a separate class, right ?
If I declare variable for my table in this class, will it be a local declaration in my class only, and it will not refer to the form and my grid also which record are being selected at the moment.

You have a grid in your form and the grid is bound to a data source. Put the name of this data source to the Data Source property of your menu item button.
Then your main() method, you can call _args.record() to get the currently active record from the data source.
Then you can use FormDataUtil::getFormDataSource() to get the form data source from the table record. You’ll need it for MultiSelectionHelper.

To achieve this in Dynamics 365 Finance and Operations, you can create a new action button on the form and add a custom method to handle the button click event. Here’s how you can implement the logic you described:

  1. Add a new button to your form using the form designer.
  2. In the Properties window for the button, set the “Action” property to “Custom.”
  3. In the “Methods” section of the form designer, create a new method to handle the button click event.
  4. In the method, first check if the user has selected any records. If not, display a message and exit the method.
  5. If records have been selected, loop through each selected record and check if its Status field is not equal to “Closed”. If any of the selected records have a Status of “Closed,” disable the button and exit the method.
  6. If all selected records have a Status other than “Closed,” display a confirmation dialog to the user asking if they want to proceed with the update.
  7. If the user confirms, loop through the selected records again and update the Status field to “Closed.”

Here’s some sample code to get you started:

[FormMethodName(‘MYBUTTONMETHOD’)] public void myButtonMethod() { // Get the selected records MyTable myTable; MyTable_ds.query().dataSourceTable(tableNum(MyTable)).research(true); MyTable_ds.queryBuildDataSource().addRange(fieldNum(MyTable, RecId)).value(SysQuery::value(MyTable_ds.selection()));

// Check if any records were selected
if (MyTable_ds.getCount() == 0)
{
// Display a message and exit the method
warning(“Please select at least one record.”);
return;
}

// Loop through the selected records and check if any have a Status of “Closed”
boolean allOpen = true;
while (MyTable_ds.next())
{
myTable = MyTable_ds.get(MyTable_ds.cursor());
if (myTable.Status == Status::Closed)
{
allOpen = false;
break;
}
}

// If any selected records have a Status of “Closed,” disable the button and exit the method
if (!allOpen)
{
this.buttonEnabled(false);
warning(“One or more selected records have already been closed.”);
return;
}

// If all selected records have a Status other than “Closed,” display a confirmation dialog to the user
if
}

Make sure to replace “MyTable” and “Status” with the names of your table and status field, respectively.