In this webinar, our experts showcase a variety of demo use cases of how different components of the...
If you've ever wondered how AddToQueue functionality works behind the scenes in CRM, you've come to the right place… in today's blog, we'll demonstrate the logic behind it. Note that we assume basic knowledge of plugin code and SDK messaging, so if you need a refresher on either, please refer to these very helpful resources: plugin development and register your plugin.
When a user clicks "Add to Queue" on the ribbon (circled in the screenshot below), certain SDK messages are triggered behind the scenes.
Understanding the Code
Since "AddToQueue" is triggered in both scenarios described above, to identify whether the entity record has been newly created or moved between queues, Plugin context needs to be analyzed within the code.
Note that "SourceEntityId" in the plugin context is null for both new queue assignment and movement between queues, so it isn't helpful to us. Instead, information regarding new queue assignment or record movement is captured under shared variable "ChangedEntityTypes." See highlighted portion of code below.
public
void Execute(IServiceProvider serviceProvider)
{
Guid _queueId = Guid.Empty; // Destination Queue ID
EntityReference _target = null; // entity record – assigned to queue
try
{
IPluginExecutionContext context = (IPluginExecutionContext)serviceProvider.GetService(typeof(IPluginExecutionContext));
IOrganizationService service = ((IOrganizationServiceFactory)serviceProvider.GetService(typeof(IOrganizationServiceFactory))).CreateOrganizationService(new
Guid?(context.UserId));
ITracingService tracingService = (ITracingService)serviceProvider.GetService(typeof(ITracingService));
if (context.MessageName == "AddToQueue")
{
if (context.InputParameters.Contains("DestinationQueueId") && context.InputParameters["DestinationQueueId"] is
Guid)
{
_target = (EntityReference)context.InputParameters["Target"];
_queueId = (Guid)context.InputParameters["DestinationQueueId"];
if (context.SharedVariables.Contains("ChangedEntityTypes"))
{
Dictionary<string, string> _message =(Dictionary<string, string>)context.SharedVariables["ChangedEntityTypes"];
if (_message.ContainsKey("queueitem"))
{
if (_message["queueitem"].ToString().ToLower() == "create")
{
// Add your Logic
}
if (_message["queueitem"].ToString().ToLower() == "update")
{
// Add your Logic
}
}
}
}
}
}
catch (Exception ex)
{
throw
new
InvalidPluginExecutionException("Error occured." + ex.ToString());
}
}
And that's it… now you know what's really happening when a user clicks "AddToQueue."
Happy Dynamics 365'ing!