I am trying to create a JavaScript function that fires during form's OnSave event. This function needs to take the Guid of the email, search for related queue items, and update a user lookup field with owner of the email. I was able to get the Guid of the email and retrieve the queue items, but I keep getting the email Id instead of owner Id when using getEntityReference(), and it is not updating the lookup field in the queue item, any help is appreciated.
function updateEmailQueue(formContext) {
var emailOwner = formContext.data.entity.getEntityReference("ownerid"); //formContext.data.entities.getAttribute("ownerid").getValue()
console.log("email owner guid" emailOwner.id);
var emailId = formContext.data.entity.getId();
console.log("email guid" emailId);
Xrm.WebApi.retrieveMultipleRecords("queueitem", "?$select=title,_objectid_value,_workerid_value&$filter=_objectid_value eq " emailId).then(
function success(queueItem) {
//retrieve related queueitems and update work by field
for (var i = 0; i < queueItem.entities.length; i ) {
console.log(queueItem.entities[i]);
var queueItemId = queueItem.entities[i].queueItemId;
console.log(queueItemId);
var entity = {};
entity["workerid_systemuser@odata.bind"] = "/systemusers(" emailOwner.id ")";
Xrm.WebApi.online.updateRecord("queueitem", queueItemId, entity).then(
function success(result) {
console.log("queueitem updated");
},
function(error) {
Xrm.Utility.alertDialog(error.message);
}
);
}
},
function (error) {
console.log(error.message);
Xrm.Navigation.openAlertDialog(error.message);
// handle error conditions
}
);
}