redacted
redacted

Reputation: 3959

Dynamics365 web api update vs upsert

I'm quite confused. I understand the actual difference between those two, but I can't see any difference in the actual implementation here.

Here is an excerpt from the docs

Basic update

Update operations use the HTTP PATCH verb. Pass a JSON object containing the properties you want to update to the URI that represents the entity. A response with a status of 204 will be returned if the update is successful.

This example updates an existing account record with the accountid value of 00000000-0000-0000-0000-000000000001.

PATCH [Organization URI]/api/data/v9.0/accounts(00000000-0000-0000-0000-000000000001) HTTP/1.1  
Content-Type: application/json  
OData-MaxVersion: 4.0  
OData-Version: 4.0  

{  
    "name": "Updated Sample Account ",  
    "creditonhold": true,  
    "address1_latitude": 47.639583,  
    "description": "This is the updated description of the sample account",  
    "revenue": 6000000,  
    "accountcategorycode": 2  
}

Upsert

An upsert operation is exactly like an update. It uses a PATCH request and uses a URI to reference a specific entity. The difference is that if the entity doesn’t exist it will be created. If it already exists, it will be updated. Normally when creating a new entity you will let the system assign a unique identifier. This is a best practice. But if you need to create a record with a specific id value, an upsert operation provides a way to do this. This can be valuable in situation where you are synchronizing data in different systems.

Sometimes there are situations where you want to perform an upsert, but you want to prevent one of the potential default actions: either create or update. You can accomplish this through the addition of If-Match or If-None-Match headers. For more information, see Limit upsert operations.

So in reality Basic update as stated above will be an upsert and to achieve a real basic update (update if given account eixists, 404 otherwise) I need to add the If-Match: * header to the PATCH request.

Did I understand that correctly?

Upvotes: 4

Views: 4065

Answers (1)

Tim Hutchison
Tim Hutchison

Reputation: 3633

I have the same understanding here as you have. In practice, I've found that using a patch request without If-Match: * will do an insert if the record doesn't exist. The puzzling piece however, is that when the upsert succeeds in inserting a record, it returns a 404 error. When I've included the If-Match: *, I've received a 400 error when the update failed.

Upvotes: 1

Related Questions