[英]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#. 我使用的是CRM版本9.0,我想通过C#删除CRM中具有相同ID的所有记录。
I want to achieve something like this (I am giving a SQL Query example just for reference purpose) 我想要实现这样的功能(我提供一个SQL Query示例仅供参考)
Delete from entityName where id = @id
Currently, I am creating records in CRM through my C# code using below 目前,我正在使用以下C#代码在CRM中创建记录
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. 现在,我想在此逻辑之前编写一个代码块,如果传递的ID存在,则会删除所有记录。
In order to delete an Entity in CRM - you must first fetch the entity to get the GUID
of the entity. 为了删除CRM中的实体-您必须首先获取该实体以获得该实体的
GUID
。
IOrganizationService
interface contains a Delete
method which required the entity type ( LogicalName
) and the GUID of the entity that CRM creates. IOrganizationService
接口包含Delete
方法,该方法要求实体类型( LogicalName
)和CRM创建的实体的GUID。
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
. 如果不确定这些ID是否在Dynamics中存在,则应尝试捕获Delete尝试,因为如果尝试删除Dynamics中不存在的记录,Dynamics将抛出
FaultException
。
var ids = new Collection<Guid>();
foreach (var id in ids)
{
try
{
_service.Delete("[entityName]", id);
}
catch (FaultException)
{
continue;
}
}
To compliment Adriani6 answer: 赞美Adriani6的答案:
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. 但是该字段被命名为
id
,而不是主键。
There is no equivalent of the below SQL query in Dynamics CRM. 在Dynamics CRM中没有以下SQL查询的等效项。
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. 您必须使用
service.RetrieveMultiple
获取与此非主键条件相匹配的所有实体记录,然后遍历每个记录的结果集以发出service.Delete
请求与主键。
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);
}
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.