Hello,
I have a modeldriven App where I want to populate a lookup field (systemusers) when I open the form.
What I found was, that I need to have an array of lookup objects wich I want to insert.
this is the code in I try to use:
let lookupData = [
{
"entityType": "systemuser",
"id": "{<the-ID-of-the-entity>}",
"name": "Name Surname"
}
]
formContext.getAttribute('lookupfield').setValue(lookupData);
Would be nice if someone has some Idea.
Thank you!
Thank you! I had spent a day and a half trying to setValue a lookup field, without success. There are plenty other how-to-deal-with-a-lookup-field posts, but none of them worked. I don't know if it was the { } round the ID, or the push of object into array, or the declarations outside the function, none of which I had, but - applying those after seeing your post sealed the deal.
The console log actually shows me the lookup object in the array:
But now I've set the value inside the success and everything works. Thank you very much!
If someone comes along this here is my final code:
'use strict';
debugger;
var StatusUpdate = window.StatusUpdate || {};
(function () {
this.formOnLoad = function (executionContext) {
let formContext = executionContext.getFormContext();
formContext.getAttribute('tpg_reportdate').setValue(new Date());
/* Code to automaticly insert Sponsor in Status Quickform-Submitted to field / seems correct but doeasnt write person into field. */
let projectId = formContext.getAttribute('tpg_msdyn_project').getValue()[0]['id']; // .slice(1, -1).toLowerCase() Tested. dont need it to work...
setSponsor(formContext, 'msdyn_project', projectId); // set value inside function
};
}).call(StatusUpdate);
function setSponsor(formContext, tableName, entityId) {
let lookupData = new Array();
let lookupItem = new Object();
Xrm.WebApi.retrieveRecord(tableName, entityId).then(
function (success) {
lookupItem.entityType = success['_tpgde_projectsponsor_value@Microsoft.Dynamics.CRM.lookuplogicalname'];
lookupItem.name = success['_tpgde_projectsponsor_value@OData.Community.Display.V1.FormattedValue'];
lookupItem.id = "{" + success['_tpgde_projectsponsor_value'] + "}";
lookupData.push(lookupItem);
formContext.getAttribute('tpg_submittedto').setValue(lookupData);
},
function (error) {
console.log(error);
}
);
}
I assume that this line
console.log(sponsor);
returns you just the empty array, correct?
Xrm.WebApi.retrieveRecord is async, this means that your line to set the value of the sponsor value get executed before the value are retrieved. Or you put the set inside the success, or you await the async function.
I don't use the Xrm.Navigation in this use case. I've posted the code in a reply below if you want to have a look.
Thank you
Hi here is how I implemented the handler:
and here is the code:
var StatusUpdate = window.StatusUpdate || {};
(function () {
this.formOnLoad = function (executionContext) {
let formContext = executionContext.getFormContext();
formContext.getAttribute('tpg_reportdate').setValue(new Date());
/* Code to automaticly insert Sponsor in Status Quickform-Submitted to field / seems correct but doeasnt write person into field. */
let projectId = formContext.getAttribute('tpg_msdyn_project').getValue()[0]['id']; // .slice(1, -1).toLowerCase() Tested. dont need it to work...
let sponsor = getSponsor('msdyn_project', projectId);
console.log(sponsor);
formContext.getAttribute('tpg_submittedto').setValue(sponsor);
};
}).call(StatusUpdate);
function getSponsor(tableName, entityId) {
let lookupData = new Array();
let lookupItem = new Object();
Xrm.WebApi.retrieveRecord(tableName, entityId).then(
function (success) {
lookupItem.entityType = success['_tpgde_projectsponsor_value@Microsoft.Dynamics.CRM.lookuplogicalname'];
lookupItem.name = success['_tpgde_projectsponsor_value@OData.Community.Display.V1.FormattedValue'];
lookupItem.id = "{" + success['_tpgde_projectsponsor_value'] + "}";
lookupData.push(lookupItem);
},
function (error) {
console.log(error);
}
);
return lookupData;
}
Thank you
Is the lookup you're setting the "parent" record? If so, there is a specific createFromEntity parameter you can set in the entityFormOptions of Xrm.Navigation.OpenForm. by setting this param, you will fill the lookup AND cause it to correctly respect cascade rules, if you have set them.
can you share the full javascript where you inserted this code and a screenshot of the event handler where this js is executed? (onload or onchange)
It just does show the entry in the field (see Picture).
the code is correct, what error do you get?
WarrenBelz
85
Most Valuable Professional
Michael E. Gernaey
65
Super User 2025 Season 1
mmbr1606
55
Super User 2025 Season 1