Pavlo Mykhailyshyn
Pavlo Mykhailyshyn

Reputation: 213

How to retrieve single-valued navigation properties for an entity?

I want to update parent relationships for specified entity. The problem is when I query N:1 refs I get referencing attributes that is not always a single-valued navigation property. I do not know how can I distinguish is the attribute parentcustomerid referencing to an account or to a contact entity. So the question is: How can I properly get single-valued navigation property for my specified entity to be able update it using request to PATCH api/data/v9.0/contacts({id}) with the body:

{"single-valued navigation [email protected]" : "/{accounts or contacts}({id})"}

Upvotes: 0

Views: 3038

Answers (1)

André Cavaca
André Cavaca

Reputation: 530

When creating a HTTP Request, add Prefer: odata.include-annotations="*" to your HTTP Request Headers. This way the response not only will have a _[Field Name]_value field with the Id but also a _[Field Name][email protected] with the logical name that you look for.

This is an example of a response for a request querying parentcustomerid of a specific contact without the header:

{
"@odata.context": "https://[Organization URI]/api/data/v9.0/$metadata#contacts(_parentcustomerid_value)",
"value": [
    {
        "_parentcustomerid_value": "bdeb86af-7e1c-e811-a837-000d3ac085f9",
        "contactid": "b050f3bb-dbf7-e811-a98a-000d3ac02bae"
    }
]

}

And this is an example of a response for the same request with the header added:

{
"@odata.context": "https://[Organization URI]/api/data/v9.0/$metadata#contacts(_parentcustomerid_value)",
"value": [
    {
       "_parentcustomerid_value@Microsoft.Dynamics.CRM.associatednavigationproperty": "parentcustomerid_account",
        "[email protected]": "account",
        "_parentcustomerid_value": "bdeb86af-7e1c-e811-a837-000d3ac085f9",
        "contactid": "b050f3bb-dbf7-e811-a98a-000d3ac02bae"
    }
]

}

Upvotes: 2

Related Questions