How to change command of formcommandbuttonControl dynamically in d365?

1.2k views Asked by At

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?

2

There are 2 answers

0
Alex Kwitny On BEST ANSWER

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.

0
Jan B. Kjeldsen On

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.