i tired since last week i am not able to fix my code, iam trying to set lookup filed but its appear as (No Name) and i did alot of workaround on my code and i tired from fix it and display the real name of record in lookup instead of NO NAME
var projectAttribute = Xrm.Page.getAttribute(/msdyn_project/);
var resourceOrganizationalUnitAttribute = Xrm.Page.getAttribute(/msdyn_resourceorganizationalunitid/);
if (!projectAttribute || !projectAttribute.getValue() || projectAttribute.getValue().length !== 1) {
alert(/Please select a project./);
return;
}
var projectId = projectValue.id;
var projectName = projectValue.name;
var userSettings = Xrm.Utility.getGlobalContext().userSettings;
var userId = userSettings.userId.replace(/{/, //).replace(/}/, //);
var userName = userSettings.userName;
var fetchXml = `
<fetch version=/1.0/ output-format=/xml-platform/ mapping=/logical/ distinct=/false/>
<entity name=/msdyn_projectteam/>
<attribute name=/msdyn_organizationalunit/ />
<filter type=/and/>
<condition attribute=/msdyn_project/ operator=/eq/ value=/${projectId}/ />
</filter>
<link-entity name=/bookableresource/ from=/bookableresourceid/ to=/msdyn_bookableresourceid/ link-type=/inner/ alias=/ai/>
<filter type=/and/>
<condition attribute=/userid/ operator=/eq/ uitype=/systemuser/ value=/${userId}/ />
</filter>
</link-entity>
</entity>
</fetch>`;
Xrm.WebApi.retrieveMultipleRecords(/msdyn_projectteam/, `?fetchXml=${encodeURIComponent(fetchXml)}`)
.then(function success(results) {
if (results.entities.length > 0) {
//// var organizationalUnit = results.entities[0][/msdyn_organizationalunit/];
var organizationalUnit = results.entities[0]._msdyn_organizationalunit_value;
var organizationalUnitLogicalName = /msdyn_organizationalunit/;
// Set the value of the lookup field
resourceOrganizationalUnitAttribute.setValue([{
id: organizationalUnit,
entityType: organizationalUnitLogicalName
}]);
// Trigger the onchange event for the lookup field
resourceOrganizationalUnitAttribute.fireOnChange();
} else {
alert(/Organizational Unit is empty in the retrieved record./);
}
} else {
alert(/No matching records found./);
}
})
.catch(function error(error) {
console.error(/Error retrieving data: / + error.message);
});
}