locked
How to add queue item in certain queue named Gold with known guid id? RRS feed

  • Question

  • I am using a plugin in CRM

    How to add queue item in certain queue named Gold with known guid id?

    My Code:

    public void Execute(IServiceProvider serviceProvider)
            {
                // Extract the tracing service for use in debugging sandboxed plug-ins.
                // If you are not registering the plug-in in the sandbox, then you do
                // not have to add any tracing service related code.
                ITracingService tracingService = (ITracingService)serviceProvider.GetService(typeof(ITracingService));

                // Obtain the execution context from the service provider.
                IPluginExecutionContext context = (IPluginExecutionContext)serviceProvider.GetService(typeof(IPluginExecutionContext));

                // The InputParameters collection contains all the data passed in the message request.
                if (context.InputParameters.Contains("Target") && context.InputParameters["Target"] is Entity)
                {
                    // Obtain the target entity from the input parameters.
                    Entity entity = (Entity)context.InputParameters["Target"];

                    // Verify that the target entity represents an entity type you are expecting. 
                    // For example, an account. If not, the plug-in was not registered correctly.
                    if (entity.LogicalName != "incident")
                        return;

                    // Obtain the organization service reference which you will need for
                    // web service calls.
                    IOrganizationServiceFactory serviceFactory = (IOrganizationServiceFactory)serviceProvider.GetService(typeof(IOrganizationServiceFactory));
                    IOrganizationService service = serviceFactory.CreateOrganizationService(context.UserId);

                    try
                    {
                        // Plug-in business logic goes here.
                        if (context.MessageName == "Create")
                        {
                            var customerid = ((EntityReference)entity.Attributes["customerid"]).Id;
                            Entity contact = service.Retrieve("contact", customerid, new ColumnSet(true));
                            //Entity contact = service.Retrieve("contact", customerid, new ColumnSet("new_contactclass", "emailaddress1"));
                            string contactClass = contact.FormattedValues["new_contactclass"];
                            string contactEmail = contact.Attributes["emailaddress1"].ToString();
                            string contactName = contact.Attributes["fullname"].ToString();

                            string queueName = "";
                            if (contactClass == "A") { queueName = "Gold"; }
                            else if (contactClass == "B") { queueName = "Silver"; }
                            else if (contactClass == "C") { queueName = "Bronze"; }

                            string fetchXml = @"<fetch version='1.0' output-format='xml-platform' mapping='logical' distinct='false'>
                                              <entity name='queue'>
                                                <attribute name='name' />
                                                <attribute name='emailaddress' />
                                                <attribute name='queueid' />
                                                <order attribute='name' descending='false' />
                                                <filter type='and'>
                                                  <condition attribute='name' operator='eq' value='{0}' />
                                                </filter>
                                              </entity>
                                            </fetch>";

                            EntityCollection result = service.RetrieveMultiple(new FetchExpression(String.Format(fetchXml, queueName)));
                            Guid queueid = (Guid)result.Entities[0].Attributes["queueid"];

                            //AddToQueueRequest queueRequest = new AddToQueueRequest();
                            //queueRequest.Target = new EntityReference("queue", queueid);
                            //AddToQueueResponse queueResponse = (AddToQueueResponse)service.Execute(queueRequest);

                            Entity queueitem = new Entity("queueitem");
                            service.Create(queueitem);
                            
                        }
                    }
                    catch (FaultException<OrganizationServiceFault> ex)
                    {
                        throw new InvalidPluginExecutionException("An error occurred in MyPlug-in.", ex);
                    }
                    catch (Exception ex)
                    {
                        tracingService.Trace("MyPlugin: {0}", ex.ToString());
                        throw;
                    }
                }
            }

    Tuesday, March 4, 2014 4:45 PM

All replies

  • On the queueitem Entity, you'll need to set the queueid attribute, and the objectid attribute to an EntityReference of whatever record you want to add to the queue. If' it's the record that the plugin fires on, you can get this from context.PrimaryEntityId

    Microsoft CRM MVP - http://mscrmuk.blogspot.com/ http://www.excitation.co.uk

    Tuesday, March 4, 2014 6:16 PM
    Moderator
  • Now the code is

    try
                    {
                        // Plug-in business logic goes here.
                        if (context.MessageName == "Create")
                        {
                            var customerid = ((EntityReference)entity.Attributes["customerid"]).Id;
                            Entity contact = service.Retrieve("contact", customerid, new ColumnSet(true));
                            //Entity contact = service.Retrieve("contact", customerid, new ColumnSet("new_contactclass", "emailaddress1"));
                            string contactClass = contact.FormattedValues["new_contactclass"];
                            string contactEmail = contact.Attributes["emailaddress1"].ToString();
                            string contactName = contact.Attributes["fullname"].ToString();

                            string queueName = "";
                            if (contactClass == "A") { queueName = "Gold"; }
                            else if (contactClass == "B") { queueName = "Silver"; }
                            else if (contactClass == "C") { queueName = "Bronze"; }

                            string fetchXml = @"<fetch version='1.0' output-format='xml-platform' mapping='logical' distinct='false'>
                                              <entity name='queue'>
                                                <attribute name='name' />
                                                <attribute name='emailaddress' />
                                                <attribute name='queueid' />
                                                <order attribute='name' descending='false' />
                                                <filter type='and'>
                                                  <condition attribute='name' operator='eq' value='{0}' />
                                                </filter>
                                              </entity>
                                            </fetch>";

                            EntityCollection result = service.RetrieveMultiple(new FetchExpression(String.Format(fetchXml, queueName)));
                            Guid queueid = (Guid)result.Entities[0].Attributes["queueid"];

                            //On the queueitem Entity, you'll need to set the queueid attribute, and the objectid attribute to an EntityReference of 
                            //whatever record you want to add to the queue. If' it's the record that the plugin fires on, you can get this from 
                            //context.PrimaryEntityId
                            Entity queueitem = new Entity("queueitem");
                            queueitem["queueid"] = queueid;
                            queueitem["objectid"] = new EntityReference("queueitem", Guid.NewGuid());
                            service.Create(queueitem);
                        }

    but it gives me this exception

    {"queueid missing. Cannot create queue item."}

    Wednesday, March 5, 2014 11:35 AM
  • I modified the code but still have error:
    objectidtypecode missing. Cannot create queue item

    Modified Code:
    -------------------

    using System;
    using System.Collections.ObjectModel;
    using System.Globalization;
    using System.Linq;
    using System.ServiceModel;
    using Microsoft.Xrm.Sdk;
    using Microsoft.Xrm.Sdk.Query;

    namespace OpenCasePlugin
    {
        public class Plugin : IPlugin
        {
            public void Execute(IServiceProvider serviceProvider)
            {
                // Extract the tracing service for use in debugging sandboxed plug-ins.
                // If you are not registering the plug-in in the sandbox, then you do
                // not have to add any tracing service related code.
                ITracingService tracingService = (ITracingService)serviceProvider.GetService(typeof(ITracingService));

                // Obtain the execution context from the service provider.
                IPluginExecutionContext context = (IPluginExecutionContext)serviceProvider.GetService(typeof(IPluginExecutionContext));

                // The InputParameters collection contains all the data passed in the message request.
                if (context.InputParameters.Contains("Target") && context.InputParameters["Target"] is Entity)
                {
                    // Obtain the target entity from the input parameters.
                    Entity entity = (Entity)context.InputParameters["Target"];

                    // Verify that the target entity represents an entity type you are expecting. 
                    // For example, an account. If not, the plug-in was not registered correctly.
                    if (entity.LogicalName != "incident")
                        return;

                    // Obtain the organization service reference which you will need for
                    // web service calls.
                    IOrganizationServiceFactory serviceFactory = (IOrganizationServiceFactory)serviceProvider.GetService(typeof(IOrganizationServiceFactory));
                    IOrganizationService service = serviceFactory.CreateOrganizationService(context.UserId);

                    try
                    {
                        // Plug-in business logic goes here.
                        if (context.MessageName == "Create")
                        {
                            var customerid = ((EntityReference)entity.Attributes["customerid"]).Id;
                            Entity contact = service.Retrieve("contact", customerid, new ColumnSet(true));
                            //Entity contact = service.Retrieve("contact", customerid, new ColumnSet("new_contactclass", "emailaddress1"));
                            string contactClass = contact.FormattedValues["new_contactclass"];
                            string contactEmail = contact.Attributes["emailaddress1"].ToString();
                            string contactName = contact.Attributes["fullname"].ToString();

                            string queueName = "";
                            if (contactClass == "A") { queueName = "Gold"; }
                            else if (contactClass == "B") { queueName = "Silver"; }
                            else if (contactClass == "C") { queueName = "Bronze"; }

                            string fetchXml = @"<fetch version='1.0' output-format='xml-platform' mapping='logical' distinct='false'>
                                              <entity name='queue'>
                                                <attribute name='name' />
                                                <attribute name='emailaddress' />
                                                <attribute name='queueid' />
                                                <order attribute='name' descending='false' />
                                                <filter type='and'>
                                                  <condition attribute='name' operator='eq' value='{0}' />
                                                </filter>
                                              </entity>
                                            </fetch>";

                            EntityCollection result = service.RetrieveMultiple(new FetchExpression(String.Format(fetchXml, queueName)));
                            Guid queueid = (Guid)result.Entities[0].Attributes["queueid"];

                            Entity queueitem = new Entity("queueitem");
                            queueitem["queueitemid"] = Guid.NewGuid();
                            queueitem["queueid"] = new EntityReference("queue", queueid);
                            queueitem["objectidtypecode"] = (new EntityReference(entity.LogicalName, entity.Id)).Id;
                            service.Create(queueitem);
                        }
                    }
                    catch (FaultException<OrganizationServiceFault> ex)
                    {
                        throw new InvalidPluginExecutionException("An error occurred in MyPlug-in.", ex);
                    }
                    catch (Exception ex)
                    {
                        tracingService.Trace("MyPlugin: {0}", ex.ToString());
                        throw;
                    }
                }
            }

        }
    }

    Wednesday, March 5, 2014 2:18 PM
  • Hi bishoycom,

    This line queueitem["objectidtypecode"] = (new EntityReference(entity.LogicalName, entity.Id)).Id; in your code is probably the reason why you're getting that error.

    The objecttypecode is an integer representing the type of object, I believe you're trying to assign an ID (GUID) in that property.


    Admin QuikView Solution for CRM 2013

    Wednesday, March 5, 2014 2:24 PM
  • Thank you but I modified the last part to

    Entity queueitem = new Entity("queueitem");
    queueitem["queueitemid"] = Guid.NewGuid();
    queueitem["queueid"] = new EntityReference("queue", queueid);
    queueitem["objecttypecode"] = 112;
    service.Create(queueitem);

    but got this error:

    objectidtypecode missing. Cannot create queue item.

    Wednesday, March 5, 2014 2:31 PM
  • Hi bishoycom,

    The line where you're specifying the queueid, I think you need to specify the Logical Name that's it a Queue that you're referencing to and also the Queue Name. I am referring to this line queueitem["queueid"] = new EntityReference("queue", queueid);


    Admin QuikView Solution for CRM 2013

    Wednesday, March 5, 2014 2:46 PM
  • I changed the last part to

    EntityCollection result = service.RetrieveMultiple(new FetchExpression(String.Format(fetchXml, queueName)));
    var resultEntity = result.Entities[0];
    Guid queueid = (Guid)resultEntity.Attributes["queueid"];
    string name = (string)resultEntity.Attributes["name"];

    Entity queueitem = new Entity("queueitem");
    queueitem["queueitemid"] = Guid.NewGuid();
    queueitem["queueid"] = new EntityReference(name, queueid);
    queueitem["objecttypecode"] = 112;
    service.Create(queueitem);

    but still makes exception

    Thursday, March 6, 2014 1:59 PM
  • You need to set the objectid attribute to an EntityReference to the record you're trying to add to the queue.
    For example, if the record you're trying to add is the one the plugin fires on, then use:

    Entity queueitem = new Entity("queueitem");
     queueitem["queueid"] = new EntityReference(name, queueid);
     queueitem["objectid"] = new EntityReference(context.PrimaryEntityName, context.PrimaryEntityId);
     service.Create(queueitem);


    Microsoft CRM MVP - http://mscrmuk.blogspot.com/ http://www.excitation.co.uk

    Thursday, March 6, 2014 9:47 PM
    Moderator
  • This code produced exception

    Entity queueitem = new Entity("queueitem");
    queueitem["queueid"] = new EntityReference(name, queueid);
    queueitem["objectid"] = new EntityReference(context.PrimaryEntityName, context.PrimaryEntityId);
    service.Create(queueitem);

    --------------------------------------------------

    this code didnot produce exception but nothing inserted in all queues

    Entity queueitem = new Entity("queueitem");
    queueitem["queueid"] = new EntityReference("queue", queueid);
    queueitem["objectid"] = new EntityReference(context.PrimaryEntityName, context.PrimaryEntityId);
    service.Create(queueitem);

    Friday, March 7, 2014 3:24 PM