Kontakt os

Change choice column with modern ribbon custom button in Dynamics 365

Learn how to create a custom button in Dynamics 365 and change a choice/option set value


The goal

In this exercise we will add a custom button to our model driven app, for the user to change the value of an option set/choice column.

Dynamics 365 contacts view with a custom Mark as contacted button in the command bar for changing the contact status choice field

How to

Step 1
In the contacts table create a new choice field and add the following options:

  • Has been contacted
  • Has not been contacted

Add the field to your form

Step 2
Edit your app, and in the navigation panel click the 3 dots on Contacts view -> Edit command bar -> Edit

Model Driven App navigation panel showing the three-dot context menu on Contacts view with Edit command bar selected

In the next window select Main Form

Command bar selection dialog prompting the user to choose which command bar to edit with Main Form highlighted

To add a new button to the ribbon click New in the top left corner and choose Command

Command bar editor with the New button clicked and the Command option selected to add a new custom button to the ribbon

Select your new button and in the command menu bar select a name and an icon

Command bar editor properties panel for the new button showing the label and icon fields being configured

Step 3
Add the following code to the OnSelect property of the button

If(
    Self.Selected.Item.'Contact Status' = 'Contact Status (Contacts)'.'Has not been contacted',
    Patch(
        Contacts,
            Self.Selected.Item,
                {'Contact Status': 'Contact Status (Contacts)'.'Has been contacted'}),
    Patch(
        Contacts,
        Self.Selected.Item,
                {'Contact Status': 'Contact Status (Contacts)'.'Has not been contacted'}
            )
    );
Notify("Contact Status changed to: " & Lower(Self.Selected.Item.'Contact Status'),
NotificationType.Success,5000);

Click save and publish

Step 4
Our new button is now ready in our model driven app, and can change the value of our record

Dynamics 365 contacts view with the custom button visible in the command bar and a contact record selected

Dynamics 365 contacts view showing a success notification confirming the contact status has been changed after clicking the custom button

Pinksky bruger cookies for at forbedre funktionaliteten på denne hjemmeside. Du bestemmer selv hvad vi må bruge. Læs mere i vores cookiepolitik.