How to get current logged user information using X++ code.
Let you have a small requirement where you want to get and display current login user information like user-id, name, position, department and etc. The below is the code that will use to get current login user information in D365FO.
class UserInfoJob
{
public static void main(Args _args)
{
Str currentUserId = curUserId();
Name workerName = HcmWorker::userId2Worker(currentUserId).name();
DirPartyName PersonName = DirPersonUser::userId2Name(currentUserId);
Str departmentName = HcmWorker::find(DirPersonuser::findUserWorkerReference(currentUserId)).primaryDepartmentName();
info(strFmt("User Id : %1 - Name : %2 - Department: %3", currentUserId, PersonName, departmentName));
}
}