Search

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.

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