Route an Incident from a User to a Queue
The following code example demonstrates how to route an incident from a users work-in-progress queue to another queue.
[C#]
using System;
using CrmSdk;
namespace Microsoft.Crm.Sdk.HowTo
{
/// <summary>
/// This sample 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.
/// </summary>
class HowToRoute
{
[STAThread]
public static void Main(string[] args)
{
Run();
}
public static bool Run()
{
bool success = false;
try
{
// Set up the Microsoft CRM service.
CrmService service = new CrmService();
service.Credentials =
System.Net.CredentialCache.DefaultCredentials;
//service.Url =
// "https://localhost/mscrmservices/2006/CrmService.asmx";
// 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();
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 which 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
Namespace Microsoft.Crm.Sdk.HowTo
'/ <summary>
'/ This sample 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.
'/ </summary>
Class HowToRoute
<STAThread()> _
Public Shared Sub Main(ByVal args() As String)
Run()
End Sub
Public Shared Function Run() As Boolean
Dim success As Boolean = False
Try
' Set up the Microsoft CRM service.
Dim service As CrmService = New CrmService
service.Credentials = _
System.Net.CredentialCache.DefaultCredentials
'service.Url =
' "https://localhost/mscrmservices/2006/CrmService.asmx"
' 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 which 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