I am using CRM version 9.0 and I want to delete all records with same ID in CRM through C#.
I want to achieve something like this (I am giving a SQL Query example just for reference purpose)
Delete from entityName where id = @id
Currently, I am creating records in CRM through my C# code using below
dictionary.Add("code", new CrmDataTypeWrapper(code, CrmFieldType.Raw));
dictionary.Add("id", new CrmDataTypeWrapper(id, CrmFieldType.Raw));
Guid EvntId = _client.CrmInterface.CreateNewRecord("entityname", dictionary, "", false, new System.Guid());
Now I want to write a code block before this logic which will delete all the records if they exist of the passed ID.
In order to delete an Entity in CRM - you must first fetch the entity to get the
GUIDof the entity.IOrganizationServiceinterface contains aDeletemethod which required the entity type (LogicalName) and the GUID of the entity that CRM creates.Here's how we do it.
This means that you can delete entities based on your condition by fetching the entities you need first based on your condition.
oppQuery.Criteria.AddCondition(new ConditionExpression("<your id field>", ConditionOperator.Equal, <your id>));On this line you specify the condition used to delete the relevant entities.