The Trick to Updating Custom Lookups using the Dynamics 365 Web API

3 Comments

In Dynamics 365 Power Apps, when updating a custom lookup field using the Web API, you may run into an “undeclared property” error. Let’s look at how to get around this so you can update custom lookups through the Web API. This tip comes from one of my favorite devs, Joseph Duty!

There are a couple of scenarios we will cover here. The first is when the entity is an activity, and the second is when the entity is not an activity. Both use the same solution, but the former highlights the issue more clearly.

Let’s say we have the Appointment entity, which is marked as an Activity:

Let’s add a new lookup to the Appointments entity called Appointment Leader, which is a lookup to the User entity:

So on each appointment, we can select a user to be the Appointment Leader:

Now, let’s use the CRM REST Builder to construct an update query using Web API. We end up with:

Which produces the code below. You can see we are passing the new_appointmentleader field in the request with the new value to update:

On running this code, we get the error – “An error occurred while validating input parameters: Microsoft.OData.ODataException: An undeclared property ‘new_favoriteuser’ which only has property annotations in the payload but no property value was found in the payload. In OData, only declared navigation properties and declared named streams can be represented as properties without values”:

To resolve this, we need to go to the Entity Definitions. Browse out to the Entity Definitions at https://yourorg.crm.dynamics.com/api/data/v9.1/$metadata#EntityDefinitions and under your entity, find the new attribute created, in my case, the field created is called new_appointmentleader:

We see that the NavigationProperty name is new_AppointmentLeader_Appointment. If we use this in the request instead:

We get the desired result, with the record updating, in this case:

Not very obvious but this appears to work!

Let’s look at scenario 2, the simpler example where the entity is not an activity.

On the Account entity, I have a new field called Favorite User, which is a lookup to System User:

Now, let’s use the CRM REST Builder to construct an update query. I am selecting the Web API, Update, the Account Entity, and my field, new_FavoriteUser with a System User value:

The request created looks like below:

On running this, we get the same error – “An error occurred while validating input parameters: Microsoft.OData.ODataException: An undeclared property ‘new_favoriteuser’ which only has property annotations in the payload but no property value was found in the payload. In OData, only declared navigation properties and declared named streams can be represented as properties without values”:

To resolve this, we can look in Entity Definitions in the same way, this time for new_FavoriteUser:

Now in the REST Builder, instead of new_favoriteuser:

We will use new_FavoriteUser from the EntiyDefinition:

Note in this case, the capitalization of some letters is the difference. As we saw with the Activity example, this could be more complicated but still required a tweak to get it to work.

Running this now, we get no error, and in my case, the Favorite User field is populated:

Hopefully this helps if you’re trying to update records with lookup fields using the Web API.

THANKS FOR READING. BEFORE YOU LEAVE, I NEED YOUR HELP.
 

I AM SPENDING MORE TIME THESE DAYS CREATING YOUTUBE VIDEOS TO HELP PEOPLE LEARN THE MICROSOFT POWER PLATFORM.

IF YOU WOULD LIKE TO SEE HOW I BUILD APPS, OR FIND SOMETHING USEFUL READING MY BLOG, I WOULD REALLY APPRECIATE YOU SUBSCRIBING TO MY YOUTUBE CHANNEL.

THANK YOU, AND LET'S KEEP LEARNING TOGETHER.

CARL

https://www.youtube.com/carldesouza

 

ABOUT CARL DE SOUZA

Carl de Souza is a developer and architect focusing on Microsoft Dynamics 365, Power BI, Azure, and AI.

carldesouza.comLinkedIn Twitter | YouTube

 

3 Responses to The Trick to Updating Custom Lookups using the Dynamics 365 Web API

  1. Carl – I cannot thank you enough for this post… I spent two days into the mornings troubleshooting this last piece to a project.

    Thank you so much

  2. Hello Carl,
    Thank you for your help.
    I have done the exact thing below but for some reasons, the record is not updated, even though It enters inside the success function. Run a test by updating a string field in the record and it did, but the lookup field is not updated. Could you please have a look at this?
    Thank you
    var dataOpp = {};
    dataOpp[“regardingobjectid_opportunity_gfi_surveyticket@odata.bind”] = “/opportunities(B7C85162-F373-EA11-A811-000D3AB85666)”;
    Xrm.WebApi.online.updateRecord(“gfi_surveyticket”, resp.value[i].activityid, dataOpp).then(
    function success(result)
    {
    console.log(“SurveyTicket updated”);
    },
    function (error)
    {
    console.log(error.message);
    });

Leave a Reply

Your email address will not be published. Required fields are marked *