Route an Incident from a User to a Queue
This documentation is archived and is not being maintained.

Route an Incident from a User to a Queue

Dynamics
banner art

[Applies to: Microsoft Dynamics CRM 4.0]

Find the latest SDK documentation: CRM 2015 SDK

The following code example demonstrates how to route an incident from a user's work-in-progress queue to another queue.

This sample code can be found in the following files in the SDK download:

Server\HowTo\CS\Entities\HowToRoute.cs
Server\HowTo\VB\Entities\HowToRoute.vb

For more information about the helper methods in the Microsoft.Crm.Sdk.Utility.CrmServiceUtility namespace, see Utility Sample Code.

Example

The following code example shows how to route an incident from a user's Work In Progress (WIP) queue into a public queue. Similar code could be used to route an incident from a public queue into a WIP queue.

[C#]
using System;
using CrmSdk;
using Microsoft.Crm.Sdk.Utility;

namespace Microsoft.Crm.Sdk.HowTo
{
   public class HowToRoute
   {
      static void Main(string[] args)
      {
         // TODO: Change the server URL and organization to match your Microsoft
         // Dynamics CRM Server and Microsoft Dynamics CRM organization.
         HowToRoute.Run("http://localhost:5555", "CRM_SDK");
      }
      
      public static bool Run(string crmServerUrl, string orgName)
      {
         bool success = false;

         try
         {
            // Set up the CRM Service.
            CrmService service = CrmServiceUtility.GetCrmService(crmServerUrl, orgName);

            // Get the ID of the system user.
            WhoAmIRequest userRequest = new WhoAmIRequest();
            WhoAmIResponse user = (WhoAmIResponse)service.Execute(userRequest);

            #region Setup Data Required for this Sample
            // Create a new customer account.
            account myAccount = new account();
            myAccount.name = "A Bike Store";
            Guid accountID = service.Create(myAccount);

            // Create a new subject.
            subject mySubject = new subject();
            mySubject.title = "Bicycles";
            Guid subjectID = service.Create(mySubject);

            // Create a new incident.
            incident incident = new incident();

            Customer myCustomer = new Customer();
            myCustomer.Value = accountID;
            myCustomer.type = EntityName.account.ToString();
            incident.customerid = myCustomer;

            Lookup subjectLookup = new Lookup();
            subjectLookup.Value = subjectID;
            subjectLookup.type = EntityName.subject.ToString();
            incident.subjectid = subjectLookup;

            incident.title = "Broken Chain";
            Guid incidentID = service.Create(incident);

            // Create a new public Bicycle Cases queue.
            queue publicQueue = new queue();

            Lookup businessLookup = new Lookup();
            businessLookup.Value = user.BusinessUnitId;
            businessLookup.type = EntityName.businessunit.ToString();
            publicQueue.businessunitid = businessLookup;

            Lookup userLookup = new Lookup();
            userLookup.Value = user.UserId;
            userLookup.type = EntityName.systemuser.ToString();
            publicQueue.primaryuserid = userLookup;

            Picklist plist = new Picklist();
            plist.name = "Public";
            plist.Value = 1;
            publicQueue.queuetypecode = plist;

            publicQueue.name = "Bicycle Cases";
            Guid publicQueueID = service.Create(publicQueue);
            #endregion

            // Find the WIP queue for the user who currently owns the incident.
            // The queue type code for a WIP queue is 3.
            QueryByAttribute query = new QueryByAttribute();
            // Be aware that using AllColumns may adversely affect
            // performance and cause unwanted cascading in subsequent 
            // updates. A best practice is to retrieve the least amount of 
            // data required.
            query.ColumnSet = new AllColumns();
            query.EntityName = EntityName.queue.ToString();
            query.Attributes = new string[] { "primaryuserid", "queuetypecode" };
            query.Values = new string[] { user.UserId.ToString(), "3" };
            BusinessEntityCollection results = service.RetrieveMultiple(query);

            queue wipQueue = (queue)results.BusinessEntities[0];

            // Create a Target object that refers to the incident.
            TargetQueuedIncident target = new TargetQueuedIncident();
            // SDK:target.EntityId = new Guid("A0F2D8FE-6468-DA11-B748-000D9DD8CDAC");
            target.EntityId = incidentID;

            // Route the incident from the WIP queue to the public queue.
            RouteRequest route = new RouteRequest();
            route.Target = target;
            route.RouteType = RouteType.Queue;
            // SDK:route.EndpointId = new Guid("A0F2D8FE-6468-DA11-C748-000D9DD8CDAC");
            route.EndpointId = publicQueueID;
            // SDK:route.SourceQueueId = new Guid("A0F2D8FE-6468-DA11-D748-000D9DD8CDAC");
            route.SourceQueueId = wipQueue.queueid.Value;

            RouteResponse routed = null;
            routed = (RouteResponse)service.Execute(route);

            #region check success

            if (routed != null) success = true;

            #endregion

            #region Remove Data Required for this Sample

            service.Delete(EntityName.incident.ToString(), incidentID);
            service.Delete(EntityName.queue.ToString(), publicQueueID);
            service.Delete(EntityName.subject.ToString(), subjectID);
            service.Delete(EntityName.account.ToString(), accountID);

            #endregion
         }
         catch (System.Web.Services.Protocols.SoapException ex)
         {
            Console.WriteLine(String.Format("{0}. {1}", ex.Message, ex.Detail.InnerText));
         }

         return success;
      }
   }
}
[Visual Basic .NET]
Imports System
Imports CrmSdk
Imports Microsoft.Crm.Sdk.Utility

Namespace Microsoft.Crm.Sdk.HowTo
   Public Class HowToRoute

      Sub Main()
   
         ' TODO: Change the server URL and organization to match your Microsoft
         ' Dynamics CRM Server and Microsoft Dynamics CRM organization.
         HowToRoute.Run("http://localhost:5555", "CRM_SDK")

      End Sub
      
     Public Shared Function Run(ByVal crmServerUrl As String, ByVal orgName As String) As Boolean
       Dim success As Boolean = False

       Try

         ' Set up the CRM Service.
         Dim service As CrmService = CrmServiceUtility.GetCrmService(crmServerUrl, orgName)

         ' Get the ID of the system user.
         Dim userRequest As WhoAmIRequest = New WhoAmIRequest
         Dim user As WhoAmIResponse = CType(service.Execute(userRequest), WhoAmIResponse)

         '#region Setup Data Required for Me Sample
         ' Create a new customer account.
         Dim myAccount As account = New account
         myAccount.name = "A Bike Store"
         Dim accountID As Guid = service.Create(myAccount)

         ' Create a new subject.
         Dim mySubject As subject = New subject
         mySubject.title = "Bicycles"
         Dim subjectID As Guid = service.Create(mySubject)

         ' Create a new incident.
         Dim incident As incident = New incident

         Dim myCustomer As Customer = New Customer
         myCustomer.Value = accountID
         myCustomer.type = EntityName.account.ToString()
         incident.customerid = myCustomer

         Dim subjectLookup As Lookup = New Lookup
         subjectLookup.Value = subjectID
         subjectLookup.type = EntityName.subject.ToString()
         incident.subjectid = subjectLookup

         incident.title = "Broken Chain"
         Dim incidentID As Guid = service.Create(incident)

         ' Create a new public Bicycle Cases queue.
         Dim publicQueue As queue = New queue

         Dim businessLookup As Lookup = New Lookup
         businessLookup.Value = user.BusinessUnitId
         businessLookup.type = EntityName.businessunit.ToString()
         publicQueue.businessunitid = businessLookup

         Dim userLookup As Lookup = New Lookup
         userLookup.Value = user.UserId
         userLookup.type = EntityName.systemuser.ToString()
         publicQueue.primaryuserid = userLookup

         Dim plist As Picklist = New Picklist
         plist.name = "Public"
         plist.Value = 1
         publicQueue.queuetypecode = plist

         publicQueue.name = "Bicycle Cases"
         Dim publicQueueID As Guid = service.Create(publicQueue)
         '#endregion

         ' Find the WIP queue for the user who currently owns the incident.
         ' The queue type code for a WIP queue is 3.
         Dim query As QueryByAttribute = New QueryByAttribute
         query.ColumnSet = New AllColumns
         query.EntityName = EntityName.queue.ToString()
         query.Attributes = New String() {"primaryuserid", "queuetypecode"}
         query.Values = New String() {user.UserId.ToString, "3"}

         Dim results As BusinessEntityCollection = service.RetrieveMultiple(query)

         Dim wipQueue As queue = CType(results.BusinessEntities(0), queue)

         ' Create a Target object that refers to the incident.
         Dim target As TargetQueuedIncident = New TargetQueuedIncident
         ' SDK:target.EntityId = new Guid("A0F2D8FE-6468-DA11-B748-000D9DD8CDAC");
         target.EntityId = incidentID

         ' Route the incident from the WIP queue to the public queue.
         Dim route As RouteRequest = New RouteRequest
         route.Target = target
         route.RouteType = RouteType.Queue
         ' SDK:route.EndpointId = new Guid("A0F2D8FE-6468-DA11-C748-000D9DD8CDAC");
         route.EndpointId = publicQueueID
         ' SDK:route.SourceQueueId = new Guid("A0F2D8FE-6468-DA11-D748-000D9DD8CDAC");
         route.SourceQueueId = wipQueue.queueid.Value

         Dim routed As RouteResponse = Nothing
         routed = CType(service.Execute(route), RouteResponse)

         '#region check success

         If Not routed Is Nothing Then
            success = True
         End If

         '#endregion

         '#region Remove Data Required for Me Sample

         service.Delete(EntityName.incident.ToString(), incidentID)
         service.Delete(EntityName.queue.ToString(), publicQueueID)
         service.Delete(EntityName.subject.ToString(), subjectID)
         service.Delete(EntityName.account.ToString(), accountID)

         '#endregion
       Catch ex As System.Web.Services.Protocols.SoapException
         Console.WriteLine(String.Format("{0}. {1}", ex.Message, ex.Detail.InnerText))
       End Try

       Return success
     End Function
   End Class
End Namespace

See Also

Reference


© 2010 Microsoft Corporation. All rights reserved.


Show:
© 2016 Microsoft