Dynamics 365: JavaScript Function To Set Lookup Field To Current User
Adrian Begovich
21,009
Super User 2024 Season 2
This JavaScript function sets a systemuser lookup field to the currently logged in user.
function setLookupFieldToCurrentUser(executionContext) { var formContext = executionContext.getFormContext(); var globalContext = Xrm.Utility.getGlobalContext(); var currentUser = new Array(); currentUser[0] = new Object(); currentUser[0].entityType = "systemuser"; currentUser[0].id = globalContext.userSettings.userId; currentUser[0].name = globalContext.userSettings.userName; formContext.getAttribute("lookupField").setValue(currentUser); }
globalContext.userSettings.userId returns the GUID of the SystemUser.Id value for the current user. globalContext.userSettings.userName returns the name of the current user.
*This post is locked for comments