When building plugins for Microsoft Dataverse, setting lookup fields like regardingobjectid can be tricky—especially with polymorphic lookups. A common issue developers face is seeing “No Name” in the UI even after setting the lookup value. This blog post explores why this happens and how to handle it properly in your plugin code.
The Problem
Suppose you’re assigning a broker to an Appointment record using a plugin. Your code might look like this:
EntityReference brokerRef = (EntityReference)appointmentEntity["crm_broker"];
appointmentEntity["regardingobjectid"] = new EntityReference(brokerRef.LogicalName, brokerRef.Id);
This sets the lookup correctly in terms of entity and ID, but the regardingobjectid shows up as “No Name” in the UI. Why?
The Cause
The EntityReference object also has a Name property, which is what the UI uses to display the label. In a Pre-Operation plugin, the record isn’t fully committed to the database yet, so Dataverse doesn’t resolve or populate this display name automatically for polymorphic lookups like regardingobjectid.
The Fix
To resolve this, explicitly retrieve the name, and all dependent attributes of the target record and assign it when setting the EntityReference:
EntityReference brokerRef = (EntityReference)appointmentEntity["crm_broker"];
Entity broker = service.Retrieve(brokerRef.LogicalName, brokerRef.Id, new ColumnSet("name"));
string brokerName = broker.GetAttributeValue<string>("name");
appointmentEntity["regardingobjectid"] = new EntityReference(brokerRef.LogicalName, brokerRef.Id)
{
Name = brokerName
};
Why This Works
- UI rendering depends on the
Nameproperty of the lookup reference. If it's missing, the form defaults to displaying "No Name". - In the Pre-Operation stage, the record and its references haven’t been persisted yet—so polymorphic lookups can't be resolved unless you provide the metadata yourself.
- By manually setting the
Name, you ensure the UI has what it needs to render the label correctly.
Additional Tips
- ✅ Stage Awareness: Consider setting the regardingobjectid in a Post-Operation plugin if possible. This guarantees the record is committed and lookups are more reliably resolved.
- ✅ Check Permissions: Ensure your plugin has adequate permissions to retrieve the broker entity’s
namefield. - ✅ Avoid UI Confusion: Even after correcting the plugin, browser caching or form loading issues might delay UI updates. Refresh the form or clear the cache to see changes.
Final Thoughts
Handling lookup fields—especially polymorphic ones like regardingobjectid—requires attention to detail. By explicitly setting the Name in your EntityReference, you ensure smoother user experiences and cleaner data presentation.
No comments:
Post a Comment