There are still open activities associated with this case, when resolving/cancelling a Case in Dynamics CRM 2011

Problem:
When you try to resolve a case in Dynamics CRM 2011, which has open activities of Task, E-mail, Phone Call, Letter, Fax, Appointment and Service Activity you will get following error message “There are still open activities associated with this case, these must be closed before the case can be closed.”

Case_Open_Activities_Message

If you are trying to cancel a case which has open activities, you will get following error message.

Cancel_Case

If you are trying to resolve a case thru CRM SDK code, you will get following error message “The incident can not be cancelled because there are open activities for this incident.”

Case_Open_Activities_ErrorMessage

Solution 1:
If you are resolving a Case from CRM UI, you need to manually open each associated activity of the case and do ‘Mark Complete’ or ‘Cancel’
Solution 2:
If you are resolving a case from CRM SDK, you need to use following code to cancel all associated activities of the case.


  // Cancel all associated Case activities which are Open and Scheduled
        public static void CancelCaseActivities(IOrganizationService orgService, Guid caseId)
        {
            string fetchXML = string.Format(@"<fetch version='1.0' output-format='xml-platform' mapping='logical' no-lock='true' distinct='false'>
                                                    <entity name='activitypointer'>
                                                        <attribute name='activityid' />
                                                        <attribute name='activitytypecode' />
                                                        <filter type='and'>
                                                            <condition attribute='regardingobjectid' operator='eq' value='{0}' />
                                                            <condition attribute='statecode' operator='in'>
                                                                <value>0</value>
                                                                <value>3</value>
                                                            </condition>
                                                        </filter>
                                                    </entity>
                                              </fetch>", caseId.ToString());

            var fetchExp = new FetchExpression(fetchXML);

            var activities = orgService.RetrieveMultiple(fetchExp);

            foreach (Entity activity in activities.Entities)
            {
                CancelActivity(activity, orgService);
            }
        }

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

Advertisement

2 thoughts on “There are still open activities associated with this case, when resolving/cancelling a Case in Dynamics CRM 2011

Leave a Reply

Fill in your details below or click an icon to log in:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out /  Change )

Twitter picture

You are commenting using your Twitter account. Log Out /  Change )

Facebook photo

You are commenting using your Facebook account. Log Out /  Change )

Connecting to %s