Home› X++ Programming Language› / Programs / Print Current User and Language
🚀 Programming Example

Print Current User and Language

33 Views
Beginner
Code Snippet
Learn this program step-by-step with algorithm, source code, output and detailed explanation.

💻 Program Code

/// <summary>
/// Print Current User and Language.
/// </summary>
internal final class Ansari005_PrintUserLanguage
{
    public static void main(Args _args)
    {
        info(strFmt("User ID  : %1", curUserId()));
        info(strFmt("Language : %1", currentUserLanguage()));
        info(strFmt("Company  : %1", curExt()));
    }

}

📘 Explanation

Print Current User and Language
Figure: Print Current User and Language

Print Current User, Language and Company in X++

Overview

Every D365 F&O session belongs to a user, runs in a company and displays text in a language. Code often needs to know these three facts: to stamp records with who did something, to choose a language for an e-mail or report, or to log context in a batch job. This program prints all three using global functions.

These functions are cheap to call and are available everywhere in X++, which makes them ideal for diagnostic messages while you learn and debug.

Prerequisites

  • A D365 F&O development environment with Visual Studio, and a model and project to work in.
  • A runnable class: Add > New Item > Dynamics 365 Items > Code > Runnable Class (Job). The class name must match the file name.
  • No tables or setup data are needed. You can see your own user ID and language in Settings > User options.

Code Walkthrough

1. Printing the user ID

info(strFmt("User ID  : %1", curUserId()));

curUserId() returns the ID of the logged-in user (for example Admin). The ID is the key of the UserInfo table, so you can look up more details such as the name and e-mail with select UserInfo where UserInfo.id == curUserId(). In batch jobs it returns the user who scheduled the job.

2. Printing the user's language

info(strFmt("Language : %1", currentUserLanguage()));

currentUserLanguage() returns the language code from the user options, such as en-US or de. D365 F&O uses this code to choose which translation of a label (@SYS12345) to display.

3. Printing the company

info(strFmt("Company  : %1", curExt()));

curExt() returns the current legal entity (DataAreaId), for example USMF. The extra spaces inside the text strings ("User ID :") line up the colons so the three lines read like a small table.

Key Concepts

  • Session context functions: curUserId(), curExt(), currentUserLanguage(), plus sessionId() and getCurrentPartition().
  • UserInfo table: stores users, including name, e-mail (networkAlias) and enabled status.
  • Labels and languages: label files contain one translation per language. SysLabel::labelId2String("@SYS1", "de") returns a label in a specific language.
  • Created by / Modified by fields: when these table properties are on, the kernel fills them automatically with curUserId(), so you rarely need to set them in code.

Expected Output

User ID  : Admin
Language : en-US
Company  : USMF

The values depend on who runs the code, the language in their user options and the company selected in the top-right corner.

Best Practices and Common Mistakes

  • Do use curUserId() instead of storing the user ID in your own variable or parameter.
  • Do remember that in batch jobs the user is the batch job owner, not someone watching the screen.
  • Do not check permissions by comparing curUserId() with a hard-coded user like "Admin". Use security roles and privileges instead.
  • Do not assume the language is English. Use labels so output is translated automatically.
  • Tip: combine all three values in a setPrefix() header for clearer batch logs.

Interview Questions

  1. Which table stores user details in D365 F&O?
    UserInfo. Its key field is id, which matches curUserId().
  2. Which user does curUserId() return inside a batch job?
    The user who created (owns) the batch job.
  3. How do you get the user's language?
    With currentUserLanguage(), or by reading UserInfo.language.
  4. How do you show a label in a language other than the user's?
    Use SysLabel::labelId2String(labelId, languageId).
  5. Should you use curUserId() for security checks?
    No. Use security roles, duties and privileges. Hard-coded user checks are hard to maintain and easy to bypass.
📚 Learning Subject

Master Programming Through Practical Examples

Improve your coding logic, problem-solving skills and programming confidence by practicing real-world examples with explanations.

🎯 How to learn from this example

First understand the algorithm carefully. Then study the program line-by-line and compare it with the output. Finally, review the explanation section to strengthen your logic and programming understanding.

🔥 Practice suggestion

Rewrite the program without looking at the code. Modify values, conditions or logic and run it again. This helps improve confidence and strengthens coding skills much faster.