Question

I am using CRM 4 and the SDK to grab cases like so:

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
    }

However, now I need to be able to change the status and title of a case from C# given its incidentid.

Ive looked at the SDK docs and cannot find an example of this.

Anyone work with this before?

Thanks

Simply put, above is code to read an incident. Could I get an example of writing an incident field, Just one. Ex: How could I change the title of an incident.

Was it helpful?

Solution

You can call the Update method on the CrmService. Here is the SDK article.

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

OTHER TIPS

To change the state of an entity you use the setstaterequest. If you want to do it to a dynamic entity there's a description in this blog

Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top