How to Take User input X++ Programing Language - D365 F&O

Rumman Ansari   Software Engineer   2023-09-01   942 Share
☰ Table of Contents

Table of Content:


How to Take User input X++ Programing Language - D365 F&O


How to Take User input X++ Programing Language

In X++, you can take user input using the Dialog class, which provides a simple way to display input dialogs and retrieve user-entered values. Here's how you can use it to take user input in an X++ program:


internal final class RunnableClass
{
   public static void main(Args _args)
   {
       str userInput;
       Dialog dialog = new Dialog("Enter Information");
 
       DialogField inputField = dialog.addField(ExtendedTypeStr(String50));
       inputField.label("Enter a value:");
 
       if (dialog.run())
       {
           userInput = inputField.value();
           info(strFmt("You entered: %1", userInput));
       }
       else
       {
           info("Input dialog canceled.");
       } 
  }
 
}


Explanation of the code

Here's a breakdown of the code:

  1. Dialog dialog = new Dialog("Enter Information");: This creates a new instance of the Dialog class with a title "Enter Information" that will be displayed in the dialog window.

  2. DialogField inputField = dialog.addField(ExtendedTypeStr(String50));: This adds an input field to the dialog. In this case, the input field is a string field with a maximum length of 64 characters. You can adjust the ExtendedTypeStr and the length according to your needs.

  3. inputField.label("Enter a value:");: This sets the label for the input field to "Enter a value:".

  4. if (dialog.run()): This displays the dialog and waits for the user to input information. If the user clicks "OK," the code inside this block will execute.

  5. str userInput = inputField.value();: This retrieves the value entered by the user in the input field.

  6. info(strFmt("You entered: %1", userInput));: This displays a message showing the user's input using the info function and strFmt.

  7. else: If the user cancels the dialog (clicks "Cancel"), the code inside this block will execute.

  8. info("Input dialog canceled.");: This displays a message indicating that the input dialog was canceled.

To execute this code, create a job in your X++ development environment and paste the code inside it. When you run the job, an input dialog will appear, allowing the user to enter a value. The value entered by the user will be displayed in the information dialog.

Remember that the Dialog class is used for simple user interactions. For more complex scenarios or user interface requirements, you might need to explore other possibilities within the Dynamics AX or Dynamics 365 platform.