Looking for PowerObjects? Don’t worry, you’re in the right place! We’ve been part of HCL for several years, and we’ve now taken the final step in our acquisition journey: moving our website to the HCL domain. Nothing else is changing – we are still fanatically focused on Microsoft Business Applications!

PowerObjects Blog 

for Microsoft Business Applications


What “Add To Queue” Really Means: Plugin Code and Understanding AddtoQueue SDK Message Processing

Post Author: Joe D365 |

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.

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!

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

PowerObjects Recommends