Pierre de LESPINAY
Pierre de LESPINAY

Reputation: 46178

Delete using alternate key

Since we can update an entity using an alternate key:

var entity = new Entity("my_entity", "my_alternate_key", "my_value");
entity["my_updated_field"] = "Update";
service.Update(entity);

I was expecting to be able to delete the same kind of way:

var ref = new EntityReference("my_entity", "my_alternate_key", "my_value");
service.Delete(ref);

But Delete can only take a Guid

Is there a way to delete a record using a an alternate key ?

I mean without retrieving it before

Upvotes: 3

Views: 921

Answers (3)

JohnRambo93
JohnRambo93

Reputation: 91

If someone stumbles over that question like me: Actually, you can delete by alternate key using the Organization Service. No clue if that feature was added in the past 5 years or was available all the time.

 var deleteRequest = new DeleteRequest
 {
       Target = new EntityReference("my_entity", "my_alternate_key", "my_value")
 };
 await client.ExecuteAsync(deleteRequest);

Upvotes: 3

Henrik H
Henrik H

Reputation: 5787

You cannot delete by alternate key using the Organization Service. For that you need to have the guid of the record in question (e.g. by retrieving it first).

It is possible to delete using alternate keys via the Web API as shown on MSDN:

Any time you need to uniquely identify an entity to retrieve, update, or delete, you can use alternate keys configured for the entity

Upvotes: 3

mjwills
mjwills

Reputation: 23864

You need to RetrieveMultiple to get the GUIDs that you want to delete, and then call Delete.

Upvotes: 0

Related Questions