I've encountered an issue with my JavaScript code on a form where it auto populates 2 lookup fields with the current user when the record is being created. Most of the time, this function works as intended. However, I've noticed that during the first use (per day/session?), one of the fields appears to remain empty even though the value has been filled in.
Upon further investigation, I confirmed that the field is indeed getting populated, but the display for this specific field is not functioning properly. It's puzzling why only this particular field is experiencing this problem despite the code being executed successfully.
Below is the code snippet I'm using, which accepts an array of field names to set values for. Can someone assist me in identifying and resolving the issue?
Thanks
function RetrieveUserInfo(fieldsToSet) {
//Retrieve user information
var context;
var serverUrl;
var UserID;
var ODataPath;
context = Xrm.Page.context;
serverUrl = context.getServerUrl();
UserID = context.getUserId();
ODataPath = serverUrl + "/XRMServices/2011/OrganizationData.svc";
var retrieveUserReq = new XMLHttpRequest();
retrieveUserReq.open("GET", ODataPath + "/SystemUserSet(guid'" + UserID + "')", true);
retrieveUserReq.setRequestHeader("Accept", "application/json");
retrieveUserReq.setRequestHeader("Content-Type", "application/json; charset=utf-8");
retrieveUserReq.onreadystatechange = function () {
SetUserValues(this, fieldsToSet);
};
retrieveUserReq.send();
}
function SetUserValues(retrieveUserReq, fieldsToSet) {
if (retrieveUserReq.readyState == 4
/* complete */
) {
if (retrieveUserReq.status == 200) {
var retrievedUser = this.parent.JSON.parse(retrieveUserReq.responseText).d;
if (retrievedUser.FullName != null)
//Get details of current user
var setUserValue = new Array();
setUserValue[0] = new Object();
setUserValue[0].id = Xrm.Page.context.getUserId();
setUserValue[0].entityType = 'systemuser';
setUserValue[0].name = retrievedUser.FullName;
//get form type
var formType = Xrm.Page.ui.getFormType();
if (formType != undefined) { //Create
if (formType == 1) {
//for each field specified, set it to the current user
for (var i = 0; i < fieldsToSet.length; i++) {
Xrm.Page.getAttribute(fieldsToSet[i]).setValue(setUserValue);
}
}
}
}
else {
}
}
}