我正在使用ssis在crm中进行一些更新。我试图根据某些条件关闭crm中的一些案例。这是我在public override void Input0_ProcessInputRow(Input0Buffer Row)
方法中的示例代码。
public override void Input0_ProcessInputRow(Input0Buffer Row)
{
// Create a Entity object of type 'case'
Entity caseEnt = new Entity("incident");
Entity incidentResolution= new Entity("incidentresolution");
incidentResolution.Attributes.Add("incidentid", new
EntityReference("incident", Row.DEVCaseGUID));
caseEnt["incidentid"] = Row.DEVCaseGUID;
//organizationservice.Update(caseEnt);
//Changes added here by //
EntityCollection collection= GetAssociatedActivities(new EntityReference("incident", Row.DEVCaseGUID))
foreach (Entity activity in collection.Entities)
{
CancelActivity(activity, organizationservice);
}
// Changes added here //
// Close the incident with the resolution.
var closeIncidentRequest = new CloseIncidentRequest
{
IncidentResolution = incidentResolution,
Status = new OptionSetValue(5)
};
organizationservice.Execute(closeIncidentRequest);
}
private EntityCollection GetAssociatedActivities(EntityReference regarding)
{
QueryExpression query = new QueryExpression { EntityName = "activitypointer", ColumnSet = new ColumnSet(new string[] { "activitytypecode" }) };
query.Criteria.AddCondition("regardingobjectid", ConditionOperator.Equal, regarding.Id);
query.Criteria.AddCondition("statecode", ConditionOperator.NotEqual, 1); //ignore completed
EntityCollection collection = organizationservice.RetrieveMultiple(query);
return collection
}
// Cancel an Activity
private static void CancelActivity(Entity entity, IOrganizationService service)
{
EntityReference moniker = new EntityReference();
if (entity.LogicalName == "activitypointer")
{
if (entity.Attributes.Contains("activityid") & entity.Attributes.Contains("activitytypecode"))
{
moniker.LogicalName = entity.Attributes["activitytypecode"].ToString();
moniker.Id = (Guid)entity.Attributes["activityid"];
SetStateRequest request = new SetStateRequest();
request.EntityMoniker = moniker;
request.State = new OptionSetValue(2);
request.Status = new OptionSetValue(-1);
SetStateResponse response = (SetStateResponse)service.Execute(request);
}
}
}
Row.DEVCaseGUID 是案例的GUID。
状态代码已关闭5
。
已解决的州代码为2
。
我尝试遵循此example但没有成功。或者有没有简单的方法来实现这个目标?
答案 0 :(得分:1)
在CRM中关闭案例与设置州/状态码不同。
关闭案例时会创建名为IncidentResoultion
的中间实体。
您可以尝试以下代码以编程方式关闭案例。
Entity incidentResolution= new Entity("incidentresolution");
incidentResolution.Attributes.Add("incidentid", new
EntityReference("incident", Row.DEVCaseGUID));
// Close the incident with the resolution.
var closeIncidentRequest = new CloseIncidentRequest
{
IncidentResolution = incidentResolution,
Status = new OptionSetValue(5)
};
organizationservice.Execute(closeIncidentRequest);
请注意,如果案件的所有Closed/Completed only
都已完成,则只能将案例标记为activitites
。
更新2017年11月9日:添加关闭CASE相关活动的代码
private List<Entity> GetAssociatedActivities(EntityReference regarding)
{
QueryExpression query = new QueryExpression { EntityName = "activitypointer", ColumnSet = new ColumnSet(new string[] { "activitytypecode" }) };
query.Criteria.AddCondition("regardingobjectid", ConditionOperator.Equal, regarding.Id);
query.Criteria.AddCondition("statecode", ConditionOperator.NotEqual, 1); //ignore completed
EntityCollection activities = organizationservice.RetrieveMultiple(query);//change collection to activities
foreach (Entity activity in activities.Entities)
{
CancelActivity(activity, organizationservice);
}
}
// Cancel an Activity
private static void CancelActivity(Entity entity, IOrganizationService service)
{
EntityReference moniker = new EntityReference();
if (entity.LogicalName == "activitypointer")
{
if (entity.Attributes.Contains("activityid") & entity.Attributes.Contains("activitytypecode"))
{
moniker.LogicalName = entity.Attributes["activitytypecode"].ToString();
moniker.Id = (Guid)entity.Attributes["activityid"];
SetStateRequest request = new SetStateRequest();
request.EntityMoniker = moniker;
request.State = new OptionSetValue(2);
request.Status = new OptionSetValue(-1);
SetStateResponse response = (SetStateResponse)service.Execute(request);
}
}
}