Thursday, November 3, 2016

This record is in multiple queues. Go to the specific queue to view the details

We would have come across this issue several times.

The Cause:
When an entity record in a Queue is deactivated, the corresponding Queeu Item record also is deactivated,
but when the record is Reactivated,  the Queue Item still remains Inactive.

Hence, if you click on Add to Queue on the Record now/Programmatically add this record to a Queue, one more Queue Item is created corresponding to this record.
Implying, now there are 2 Queue Items corresponding to this record, 1 Inactive and other Active.
If  you now click on the Queue Item details, it would throw an error- that record is in multiple Queues.

Solution:
Remove Queue Item from Queue whenever it is deactivated.

Below is the Custom WF to do it.

This can be added as a step in a System WF, with
Primary Entity as Queue Item,  
Trigger: Status Change,  
Condition : If Queue Item:Status equals [Inactive] AND Queue Item:Type equals [<<Your Entity Name>>].
 Parameters to be passed to the custom WF Step : {Record URL(Dynamic)(Object (<<your Entity  type>> ))}


using Microsoft.Crm.Sdk.Messages;
using Microsoft.Xrm.Sdk;
using Microsoft.Xrm.Sdk.Query;
using Microsoft.Xrm.Sdk.Workflow;
using System;
using System.Activities;
namespace Workflow.RemoveQIFRomQueue

{
    public class RemoveQueueItemFromQueue : CodeActivity
    {
        [Input("Queue Item")]
        [ReferenceTarget("queueitem")]
        public InArgument<EntityReference> queueItemReference { get; set; }
        protected override void Execute(CodeActivityContext executionContext)
        {
            #region declare Service related holders
            IWorkflowContext context = executionContext.GetExtension<IWorkflowContext>();
            IOrganizationServiceFactory serviceFactory = executionContext.GetExtension<IOrganizationServiceFactory>();
            IOrganizationService _service = serviceFactory.CreateOrganizationService(context.UserId);
            ITracingService tracingService = executionContext.GetExtension<ITracingService>();
            #endregion
            EntityReference queueItem = queueItemReference.Get<EntityReference>(executionContext);
            tracingService.Trace("Checking Queue Item Id");
            tracingService.Trace(queueItem.Id.ToString());
            RemoveFromQueueRequest removeFromQueueReq = new RemoveFromQueueRequest()
            {
                QueueItemId = queueItem.Id
            };
            _service.Execute(removeFromQueueReq);
        }
    }
}

Example System WF For removing a  Case - Queue Item from Queue

No comments:

Post a Comment