如何以编程方式在CRM 2011中结案 [英] How to close cases in crm 2011 programmatically

查看:79
本文介绍了如何以编程方式在CRM 2011中结案的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在使用ssis在crm中进行一些更新。我试图根据某些条件关闭一些crm案件。这是我在公共重写void Input0_ProcessInputRow(Input0Buffer Row)方法中的示例代码。

I am doing some update in crm using ssis. I tried to close some cases in crm based on certain conditions. This is my sample code in public override void Input0_ProcessInputRow(Input0Buffer Row) method.

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);
        }
    }
}

行。DEVCaseGUID 是案例的GUID。
状态码为关闭状态的 5
状态码 2 (已解决)。

Row.DEVCaseGUID is the GUID of the Case. statuscode is 5 for closed. statecode is 2 for Resolved.

我尝试按照此<一个href = https://community.dynamics.com/crm/f/117/t/160206 rel = nofollow noreferrer>示例,但没有成功。还是有任何简单的方法来实现此目的?

I tried follow this example but no success. Or is there any simple way to achieve this ?

推荐答案

在CRM中关闭案例与设置状态/状态代码不同。
在结案时会创建一个名为 IncidentResoultion 的中间实体。
您可以尝试使用以下代码以编程方式关闭此案例。

Closing a Case in CRM is different than setting state/statuscode. An Intermediate entity named IncidentResoultion is created when a case is closed. You can try the following code to close the case programmatically.

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);

请注意,案件只能标记为仅已结案/已完成,如果有关该案例的所有活动已完成。

Please note that a case can only be marked as Closed/Completed only if all the activititesregarding that Case are completed.

更新09- 2017年11月:添加代码以关闭与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);
          }
      }
  }

https://www.magnetismsolutions.com/blog/roshanmehta/2012/2/16/ Dynamics_CRM_2011_Closing_all_Related_Activities_for_a_Record.aspx

> https://msdynamicscrmblog.wordpress.com/2013/06/18 /当解决动态CRM-2011中的情况时,还有这种情况下的开放活动/

这篇关于如何以编程方式在CRM 2011中结案的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆