Blog 10: Fixing Activity.RegardingObjectId Not Being Set

When working with activity records (such as Emails, Phone Calls, Appointments, and Custom Activities) in Dataverse, you might notice that the Regarding field sometimes shows “(No Name)” instead of the expected value.

This issue occurs because the EntityReference.Name property is not automatically set by Dataverse when using plug-ins.

🚨 Known Issue:

  • Symptom: The RegardingObjectId lookup field appears blank even though a valid reference exists.
  • Affected Tables: All activity-based tables like Email, PhoneCall, Appointment, Task, etc.
  • Impact: Users cannot see the related record name in the UI.

In this blog, we’ll explore why this happens and how to fix it.


🔍 Why Does This Happen?

This issue arises when:

1️⃣ Plug-ins Set the Lookup Value in PreOperation Stage

  • If you set RegardingObjectId in PreOperation, Dataverse does not automatically populate the EntityReference.Name field.

2️⃣ EntityReference.Name is Not Explicitly Assigned

  • Unlike manual UI selection, when a plug-in sets a lookup field, the name property remains null unless explicitly assigned.

3️⃣ Dataverse Does Not Backfill the Lookup Display Name

  • Even though the lookup stores the ID (Guid), the related entity’s name is not retrieved unless explicitly provided.

🔧 How to Fix This Issue

1️⃣ Assign the EntityReference.Name Property Manually

When setting RegardingObjectId, make sure to also assign its Name property.

Correct Approach:

Entity email = new Entity("email");
email["subject"] = "Follow-up with customer";
email["regardingobjectid"] = new EntityReference("account", accountId)  
{
    Name = "Contoso Ltd." // Manually assign the display name
};
service.Create(email);

This ensures that the Regarding field correctly displays the related entity’s name in Dataverse.


2️⃣ Set the Lookup Value in PreValidation Instead of PreOperation

If modifying RegardingObjectId in a plug-in, consider setting it in the PreValidation stage.

📌 Why?

  • In PreValidation, the data has not yet entered the transaction, allowing Dataverse to correctly process and populate the lookup.

Register Plug-in in PreValidation Stage

  • In the Plug-in Registration Tool, set Execution Pipeline Stage to PreValidation.

3️⃣ Retrieve the Related Entity’s Name Before Setting the Lookup

If you don’t have the name readily available, you can retrieve it before assigning the lookup.

Example:

Entity account = service.Retrieve("account", accountId, new ColumnSet("name"));
string accountName = account.Contains("name") ? account["name"].ToString() : "Unknown";

Entity email = new Entity("email");
email["subject"] = "Follow-up with customer";
email["regardingobjectid"] = new EntityReference("account", accountId)  
{
    Name = accountName
};
service.Create(email);

This ensures the lookup correctly displays the entity name without extra database queries later.


📌 Best Practices to Avoid This Issue

Always set the EntityReference.Name property explicitly.
Consider setting lookups in the PreValidation stage.
Retrieve the related entity’s name before assigning the lookup.


That wraps up 🎉Have more plug-in issues you want us to cover? Let us know! 🚀

Leave a comment