简体   繁体   中英

CRM v9 - Delete records based on ID from CRM Entity using C# Code

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 GUID of the entity.

IOrganizationService interface contains a Delete method which required the entity type ( LogicalName ) and the GUID of the entity that CRM creates.

Here's how we do it.

QueryExpression oppQuery = new QueryExpression("opportunity");
oppQuery.ColumnSet = new ColumnSet(new string[] { "opportunityid" });
oppQuery.Criteria.AddCondition(new ConditionExpression("parentcontactid", ConditionOperator.Equal, contact.Id));

EntityCollection opportunities = crmSvc.RetrieveMultiple(oppQuery);

foreach (var opportunity in opportunities.Entities)
{
    service.Delete("opportunity", opportunity.Id);
}

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.

If you are not sure whether or not those ids exist in Dynamics, you should try-catch your Delete attempt, because if you try to delete a record that does not exist in Dynamics, Dynamics will throw you a FaultException .

var ids = new Collection<Guid>();
foreach (var id in ids)
{
    try
    {
        _service.Delete("[entityName]", id);
    }
    catch (FaultException)
    {
        continue;
    }
}

To compliment Adriani6 answer:

I think you are trying to delete a set of records which are having particular field value. But that field is named as id which is not a primary key.

There is no equivalent of the below SQL query in Dynamics CRM.

Delete from entityName where id = @id

You have to fetch all the entity records matching this condition of non-primary key using service.RetrieveMultiple and then iterate through the result set for each record to issue service.Delete request with primary key.

QueryExpression query = new QueryExpression("entityName");
query.ColumnSet = new ColumnSet(new string[] { "eventId", "code"});
query.Criteria.AddCondition(new ConditionExpression("id", ConditionOperator.Equal, id));

EntityCollection events = service.RetrieveMultiple(query);

foreach (var event in events.Entities)
{
    service.Delete("entityName", event.Id);
}

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM