Route an Incident from a User to a Queue

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


   1:  //# Route an Incident from a User to a Queue 

   2:  using System;

   3:  using CrmSdk;

   4:  using Microsoft.Crm.Sdk.Utility;

   5:   

   6:  namespace Microsoft.Crm.Sdk.HowTo

   7:  {

   8:     public class HowToRoute

   9:     {

  10:        static void Main(string[] args)

  11:        {

  12:           // TODO: Change the server URL and organization to match your Microsoft

  13:           // Dynamics CRM Server and Microsoft Dynamics CRM organization.

  14:           HowToRoute.Run("http://localhost:5555", "CRM_SDK");

  15:        }

  16:        

  17:        public static bool Run(string crmServerUrl, string orgName)

  18:        {

  19:           bool success = false;

  20:   

  21:           try

  22:           {

  23:              // Set up the CRM Service.

  24:              CrmService service = CrmServiceUtility.GetCrmService(crmServerUrl, orgName);

  25:   

  26:              // Get the ID of the system user.

  27:              WhoAmIRequest userRequest = new WhoAmIRequest();

  28:              WhoAmIResponse user = (WhoAmIResponse)service.Execute(userRequest);

  29:   

  30:              #region Setup Data Required for this Sample

  31:              // Create a new customer account.

  32:              account myAccount = new account();

  33:              myAccount.name = "A Bike Store";

  34:              Guid accountID = service.Create(myAccount);

  35:   

  36:              // Create a new subject.

  37:              subject mySubject = new subject();

  38:              mySubject.title = "Bicycles";

  39:              Guid subjectID = service.Create(mySubject);

  40:   

  41:              // Create a new incident.

  42:              incident incident = new incident();

  43:   

  44:              Customer myCustomer = new Customer();

  45:              myCustomer.Value = accountID;

  46:              myCustomer.type = EntityName.account.ToString();

  47:              incident.customerid = myCustomer;

  48:   

  49:              Lookup subjectLookup = new Lookup();

  50:              subjectLookup.Value = subjectID;

  51:              subjectLookup.type = EntityName.subject.ToString();

  52:              incident.subjectid = subjectLookup;

  53:   

  54:              incident.title = "Broken Chain";

  55:              Guid incidentID = service.Create(incident);

  56:   

  57:              // Create a new public Bicycle Cases queue.

  58:              queue publicQueue = new queue();

  59:   

  60:              Lookup businessLookup = new Lookup();

  61:              businessLookup.Value = user.BusinessUnitId;

  62:              businessLookup.type = EntityName.businessunit.ToString();

  63:              publicQueue.businessunitid = businessLookup;

  64:   

  65:              Lookup userLookup = new Lookup();

  66:              userLookup.Value = user.UserId;

  67:              userLookup.type = EntityName.systemuser.ToString();

  68:              publicQueue.primaryuserid = userLookup;

  69:   

  70:              Picklist plist = new Picklist();

  71:              plist.name = "Public";

  72:              plist.Value = 1;

  73:              publicQueue.queuetypecode = plist;

  74:   

  75:              publicQueue.name = "Bicycle Cases";

  76:              Guid publicQueueID = service.Create(publicQueue);

  77:              #endregion

  78:   

  79:              // Find the WIP queue for the user who currently owns the incident.

  80:              // The queue type code for a WIP queue is 3.

  81:              QueryByAttribute query = new QueryByAttribute();

  82:              // Be aware that using AllColumns may adversely affect

  83:              // performance and cause unwanted cascading in subsequent 

  84:              // updates. A best practice is to retrieve the least amount of 

  85:              // data required.

  86:              query.ColumnSet = new AllColumns();

  87:              query.EntityName = EntityName.queue.ToString();

  88:              query.Attributes = new string[] { "primaryuserid", "queuetypecode" };

  89:              query.Values = new string[] { user.UserId.ToString(), "3" };

  90:              BusinessEntityCollection results = service.RetrieveMultiple(query);

  91:   

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

  93:   

  94:              // Create a Target object that refers to the incident.

  95:              TargetQueuedIncident target = new TargetQueuedIncident();

  96:              // SDK:target.EntityId = new Guid("A0F2D8FE-6468-DA11-B748-000D9DD8CDAC");

  97:              target.EntityId = incidentID;

  98:   

  99:              // Route the incident from the WIP queue to the public queue.

 100:              RouteRequest route = new RouteRequest();

 101:              route.Target = target;

 102:              route.RouteType = RouteType.Queue;

 103:              // SDK:route.EndpointId = new Guid("A0F2D8FE-6468-DA11-C748-000D9DD8CDAC");

 104:              route.EndpointId = publicQueueID;

 105:              // SDK:route.SourceQueueId = new Guid("A0F2D8FE-6468-DA11-D748-000D9DD8CDAC");

 106:              route.SourceQueueId = wipQueue.queueid.Value;

 107:   

 108:              RouteResponse routed = null;

 109:              routed = (RouteResponse)service.Execute(route);

 110:   

 111:              #region check success

 112:   

 113:              if (routed != null) success = true;

 114:   

 115:              #endregion

 116:   

 117:              #region Remove Data Required for this Sample

 118:   

 119:              service.Delete(EntityName.incident.ToString(), incidentID);

 120:              service.Delete(EntityName.queue.ToString(), publicQueueID);

 121:              service.Delete(EntityName.subject.ToString(), subjectID);

 122:              service.Delete(EntityName.account.ToString(), accountID);

 123:   

 124:              #endregion

 125:           }

 126:           catch (System.Web.Services.Protocols.SoapException ex)

 127:           {

 128:              Console.WriteLine(String.Format("{0}. {1}", ex.Message, ex.Detail.InnerText));

 129:           }

 130:   

 131:           return success;

 132:        }

 133:     }

 134:  }