用c#写事件

本文关键字:事件 | 更新日期: 2023-09-27 18:09:32

我使用CRM 4和SDK来抓取案例,如下所示:

public List<Case> GetCases()
    {
        List<Case> cases = new List<Case>();

            #region Retrieve Resolved Cases
        try
        {
            InitSession();
            RetrieveMultipleRequest req = new RetrieveMultipleRequest();
            req.ReturnDynamicEntities = true;
            //QueryExpression says what entity to retrieve from, what columns we want back and what criteria we use for selection
            QueryExpression qe = new QueryExpression();
            qe.EntityName = EntityName.incident.ToString();
            List<string> attributes = new string[] { 
            "incidentid","title" ,"description", "ticketnumber", "statuscode",
            "kez_allocatedhours",
            "customerid",
            "casetypecode"
            }.ToList();
            //columns to retireve 
            ColumnSet AvailabilityColumnSet = new ColumnSet();
            AvailabilityColumnSet.Attributes = attributes.ToArray();
            qe.ColumnSet = AvailabilityColumnSet;
            //filter
            FilterExpression fe = new FilterExpression();
            fe.FilterOperator = LogicalOperator.And;
            //condtion for filter
            ConditionExpression isResolved = new ConditionExpression();
            isResolved.AttributeName = "statuscode";
            isResolved.Operator = ConditionOperator.NotEqual;
            isResolved.Values = new string[] { "5" };
            fe.Conditions = new ConditionExpression[] { isResolved }; //Add the conditions to the filter
           qe.Criteria = fe; //Tell the query what our filters are
            req.Query = qe; //Tell the request the query we want to use
            //retrieve entities
            RetrieveMultipleResponse resp = svc.Execute(req) as RetrieveMultipleResponse;
            if (resp != null)
            {
                BusinessEntity[] rawResults = resp.BusinessEntityCollection.BusinessEntities;
                List<DynamicEntity> castedResults = rawResults.Select(r => r as DynamicEntity).ToList();
                foreach (DynamicEntity result in castedResults)
                {
                    string id = GetProperty(result, "incidentid");
                    string title = GetProperty(result, "title");
                    string description = GetProperty(result, "description");
                    string ticket = GetProperty(result, "ticketnumber");
                    string customer = GetProperty(result, "customerid");
                    int statuscode = -1;
                    string statusname = "";
                    double estHours = 0.0;
                    string casetype = "";
                    int casetypecode = -1;
                    Property prop = result.Properties.Where(p => p.Name == "statuscode").FirstOrDefault();
                    if (prop != null)
                    {
                        StatusProperty status = prop as StatusProperty;
                        if (status != null)
                        {
                            statuscode = status.Value.Value;
                            statusname = status.Value.name;
                        }
                    }
                    prop = result.Properties.Where(p => p.Name == "kez_allocatedhours").FirstOrDefault();
                    if (prop != null)
                    {
                        CrmFloatProperty fl = prop as CrmFloatProperty;
                        if (fl != null)
                        {
                            estHours = fl.Value.Value;
                        }
                    }
                    prop = result.Properties.Where(p => p.Name == "casetypecode").FirstOrDefault();
                    if (prop != null)
                    {
                        PicklistProperty fl = prop as PicklistProperty;
                        if (fl != null)
                        {
                            casetype = fl.Value.name;
                            casetypecode = fl.Value.Value;
                        }
                    }
                    Case c = new Case();
                    c.ID = id;
                    c.Title = title;
                    c.Description = description;
                    c.StatusCode = statuscode;
                    c.StatusName = statusname;
                    c.TicketNumber = ticket;
                    c.CustomerName = customer;
                    c.EstimatedHours = estHours;
                    c.Type = casetype;
                    c.TypeCode = casetypecode;
                    bool allowedThroughStat = true;
                    bool allowedThroughType = true;
                    var userStatuses = SettingsManager.Get("CRMUserStatusReasons").Split(';').ToList().Where(p => p.Length > 0).ToList();
                    var userTypes = SettingsManager.Get("CRMUserCaseTypes").Split(';').ToList().Where(p => p.Length > 0).ToList();
                    if(userStatuses.Count > 0 && !userStatuses.Contains(c.StatusCode.ToString()))
                    {
                        allowedThroughStat = false;
                    }
                    if (userTypes.Count > 0 && !userTypes.Contains(c.TypeCode.ToString()))
                    {
                        allowedThroughType = false;
                    }
                    if(allowedThroughStat && allowedThroughType)
                    cases.Add(c);
                }
            }
        }// end try
        catch (Exception)
        {
            return null;
            // The variable 'e' can access the exception's information.
            // return "Error Message: " + e.Message.ToString() + " | Stack Trace: " + e.StackTrace.ToString();
        }
        return cases;
            #endregion
    }

然而,现在我需要能够从c#中更改给定事件的案例的状态和标题。

我看了SDK文档,找不到这样的例子。

有人以前用过这个吗?

感谢简单地说,上面是读取事件的代码。我能举一个写事件场的例子吗,就一个。例:如何更改事件的标题

用c#写事件

您可以在CrmService上调用Update方法。这里是SDK的文章。

Case c = new Case();
c.ID = id;
c.Title = title;
svc.Update(c); 

要改变一个实体的状态,你可以使用setstaterrequest。如果你想对一个动态实体进行修改这个博客里有一个描述