Home > CRM 2011, Custom Workflow > CRM 2011: Removing Email from queue

CRM 2011: Removing Email from queue

Last year I blogged about this on CRM 4.0 but since last few months lots of users queried about similar solution for CRM 2011. This blog entry hopefully will provide solution for this.

Well solution logic is same as CRM 4.0, we will create custom workflow which will take email id and source queue id as input parameter and then custom will removed this email from relevant queue.

I have done following are steps to achieve this in CRM 2011.

Step 1:
Create new VS studio project and add necessary reference (i.e. Microsoft.Xrm.Sdk , Microsoft.Xrm.Sdk.Workflow etc.). for more information on how to create custom workflow activity for 2011 refer following article
http://technet.microsoft.com/en-us/library/gg328515.aspx

Step 2 :
Add new class called RemoveEmail.cs and I have provided full source code for this class at end of this article. (or it can be downloaded from http://snipt.org/xoGi)

Step 3 :
Register custom workflow assembly on 2011 dynamics server using Plug in Registration Tool.
for more information on how to register custom workflow activity for 2011 refer following article http://technet.microsoft.com/en-us/library/gg328515.aspx

Step 4:
following screen print of on demand workflow I have created for this in CRM 2011 for testing purpose.

That’s it, Done. if you need more information or got any more queries regarding this please drop comment below.
Hope this helps..

Cheers,
MayankP:)

Source Code


using System;
using System.Activities;
using Microsoft.Xrm.Sdk.Workflow;
using Microsoft.Xrm.Sdk;
using Microsoft.Xrm.Sdk.Query;
using Microsoft.Xrm.Sdk.Messages;
using Microsoft.Xrm.Sdk.Discovery;
using Microsoft.Xrm.Sdk.Client;

namespace CRM2011WorkflowUtilities
{
public class RemoveEmail : CodeActivity
{
protected override void Execute(CodeActivityContext executionContext)
{
try
{
//Create the context and tracing service
IExecutionContext context = executionContext.GetExtension();
IOrganizationServiceFactory serviceFactory = executionContext.GetExtension();
IOrganizationService service = serviceFactory.CreateOrganizationService(context.UserId);
ITracingService tracer = executionContext.GetExtension();

EntityReference incomingEmail = activityId.Get(executionContext);
string emailID = incomingEmail.Id.ToString();

EntityReference incomingQueue = queueId.Get(executionContext);
string queueID = incomingQueue.Id.ToString();

//Get the QueueItemID
Guid queueItemId = GetQueueItem(service,emailID,queueID);

//delete relevant queue item record and it will remmoved email from queue
if (queueItemId != Guid.Empty)
{
service.Delete("queueitem", queueItemId);
}
}
catch (Exception ex)
{
Helpers.Throw(String.Format("An error occurred in the {0} plug-in.",
this.GetType().ToString()),
ex);
}

return;
}

///

/// Retrieve Queue Item for this email and queue
///

///
///
///
///
private Guid GetQueueItem(IOrganizationService service,string emailGUID,string QueueGUID)
{
Guid queueItemId = Guid.Empty;

// Share the same access rights as the user, so we must retrieve the user's access rights to the record
QueryExpression qe = new QueryExpression();
qe.EntityName = "queueitem";
qe.ColumnSet = new ColumnSet();
qe.ColumnSet.AllColumns = true;

FilterExpression filter = new FilterExpression();

ConditionExpression hasEmailId = new ConditionExpression();
hasEmailId.AttributeName = "objectid";
hasEmailId.Operator = ConditionOperator.Equal;
hasEmailId.Values.Add(emailGUID) ;

ConditionExpression hasQueueId = new ConditionExpression();
hasQueueId.AttributeName = "queueid";
hasQueueId.Operator = ConditionOperator.Equal;
hasQueueId.Values.Add(QueueGUID);

filter.Conditions.Add(hasEmailId);
filter.Conditions.Add(hasQueueId);

qe.Criteria = filter;

EntityCollection ec = service.RetrieveMultiple(qe);

if (ec != null && ec.Entities.Count > 0)
{
queueItemId = ec.Entities[0].Id;
}

return queueItemId;

}

#region Input Parameters

[RequiredArgument]
[Input("Source Email Id")]
[ReferenceTarget("email")]
public InArgument activityId { get; set; }

[Input("queue Id")]
[ReferenceTarget("queue")]
public InArgument queueId { get; set; }

#endregion

#region Output Parameters

#endregion
}
}

Advertisement
  1. December 14, 2011 at 8:14 AM | #1

    Greetings, thanks for all your work. i am having issues with registering the plug-in. i keep getting no plugins selected dialogue box. am i not building the project correctly? kindly advise.

    • December 14, 2011 at 9:15 PM | #2

      This will only work in On premise version.

      are you trying to use this with CRM online?

      • December 15, 2011 at 6:06 AM | #3

        greetings. thanks for the response. am using an on-premise version. the code builds successfully. the issue is when i load the assembly into the registration tool. it loads but when i click on register selected plugin, the dialog box appears saying “no plugins have been selected….”

      • December 15, 2011 at 8:02 PM | #4

        when you load the assembly , make sure you select the relevant class..
        see below link for this

        if your class is selected then try none option isolation mode and also try to register using disk option.

        hope this helps..

      • December 16, 2011 at 9:15 AM | #5

        Hi! anyway i can send you a screenshot of what am seeing, you mentioned a link in your reply?

  2. December 14, 2011 at 11:23 AM | #6

    I´m getting an error in Line 42 with Helpers.Throw…
    Anything missing?

    • December 14, 2011 at 9:18 PM | #7

      can you please confirm passed Queue and Email Id is correct? I guess you are getting while retrieve queue item becuase either queue id or emailid is not passed correctly..

  1. No trackbacks yet.

Leave a Reply

Fill in your details below or click an icon to log in:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out / Change )

Twitter picture

You are commenting using your Twitter account. Log Out / Change )

Facebook photo

You are commenting using your Facebook account. Log Out / Change )

Connecting to %s

Follow

Get every new post delivered to your Inbox.