Wednesday, January 19, 2011

CRM 4.0 - Assigning Case to Queue

I had the task to assign case (incident) to queue. MSDN has good sample. But this sample didn't solved my problem because in my case not in all situations item was assigned to "In Progress" queue of user. Following code solved my problem.




string fetchRequest = string.Format(@"<fetch mapping='logical'>
<entity name='queueitem'>
<attribute name='queueid'/>
<filter type='and'>
<condition attribute='objectid' operator='eq' value='{0}'/>
</filter>
</entity>
</fetch>", Guid.Empty);//Insert identifier of incident here

string fetchResponce = string.Empty;

try
{
fetchResponce = crmservice.Fetch(fetchRequest);
}
catch (SoapException sexc)
{
throw new Exception(sexc.Detail.InnerText);
}

XmlDocument doc = new XmlDocument();
doc.LoadXml(fetchResponce);

XmlNode queueidnode = doc.SelectSingleNode("//resultset/result/queueid");

TargetQueuedIncident target = new TargetQueuedIncident();
target.EntityId = Guid.Empty;//Insert identifier of your incident here

RouteRequest routerequest = new RouteRequest();
routerequest.EndpointId = Guid.Empty;//Insert Identifier of queue here
routerequest.RouteType = RouteType.Queue;

if (queueidnode != null)
routerequest.SourceQueueId = new Guid(queueidnode.InnerText);
else
throw new Exception("Queue was not found");

routerequest.Target = target;

try
{
crmservice.Execute(routerequest);
}
catch (SoapException sexc)
{
throw new Exception(sexc.Detail.InnerText);
}