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.

Add To Queue

When a user clicks “Add to Queue” on the ribbon (circled in the screenshot below), certain SDK messages are triggered behind the scenes.

Add To Queue

  • If the entity record is being moved to a queue for the first time, the “Create” and “AddToQueue” SDK Messages are both triggered for the queueitem entity. Therefore, the plugin can be registered on either the “Create” or “AddToQueue” steps of the queueitem entity.
  • If the entity record has previously been in a queue and is now being moved from one queue to another, only the “AddToQueue” SDK Message is triggered. Obviously, in this case the plugin must be registered on the “AddToQueue” step of the queueitem entity, as shown below:

 

Add To Queue

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!

Avatar for JoeCRM

JoeCRM

Joe CRM is a CRM superhero who runs on pure Microsoft Dynamics CRM adrenaline. As the face of PowerObjects, Joe CRM’s mission is to reveal innovative ways to use Dynamics CRM and bring the application to more businesses and organizations around the world.