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;
}
}
}