0

I have a form with a formCommandButtonControl in d365. I want to change the command associated to the button dynamically according to the condition in the code. I can't find any base enum to choose the value.

switch (x)
{
case 1:
    formButton.command(New);
break;
case 2:
    formButton.command(DeleteRecord);
break;
}

This is the property in the form enter image description here

How can I choose New and deleteRecord value in x++ code?

Jan B. Kjeldsen
  • 17,817
  • 5
  • 32
  • 50
Nastaran Hakimi
  • 695
  • 1
  • 16
  • 36
  • The more usual approach is to provide _two_ buttons and disable/enable them if needed. Why do you need this one button to have a dual function? The `FormCommandButtonControl.command()` method takes an integer argument and returns an integer as well. You could assign the desired command to the button and retrieve the integer value in the debugger or the write the value to the infolog. – Sander Sep 29 '20 at 07:02
  • I know about that approach but if I can assign the command in the code then there is no need to have multiple buttons. I can find the integer but it is better that if there is an enum or a micro I use that because maybe by the future updates the order will be changed. – Nastaran Hakimi Sep 29 '20 at 07:29
  • Another option would be to use a normal button and overwrite the clicked() method and evaluate the condition for the button behavior there. – FH-Inway Sep 29 '20 at 11:32

2 Answers2

1

Unfortunately, the answer to your question is do not do that and there is no enum.

When dynamically creating command buttons (FormBuildCommandButtonControl vs FormCommandButtonControl), the Microsoft convention has been to just use a constant (#define.New(260)) and reference that.

It is unheard of to dynamically change a command button's command and I don't believe it's done anywhere in the system.

The command button's text most likely won't update dynamically so you'll have change that too.

You should use a regular button for your purposes or create multiple command buttons and adjust their visibility as needed like the comments say.

Alex Kwitny
  • 11,211
  • 2
  • 49
  • 71
0

The most obvious way is to provide two buttons only showing the relevant.

newButton.visible(x == 1);
deleteButton.visible(x == 2);

Mark the AutoDeclaration attribute of the control.
The code placed in init or active method where appropriate.

Jan B. Kjeldsen
  • 17,817
  • 5
  • 32
  • 50