locked
Incoming Email to Case Create CRM 2015 RRS feed

  • Question

  • Hi,

    We have couple of queues with different email addresses, when an email is received into these queues, user will manually convert them to cases. Is there any way to figure out from which queue the email was converted to case, I need to perform some extra logic before the case is created based on the queue that it is converted from.

    Regards

    Jeevan


    Regards Jeevan Please vote for me if you find the answer is useful.

    Friday, November 27, 2015 1:53 PM

Answers


  • protected void ExecutePreCaseCreate(LocalPluginContext localContext) { if (localContext == null) { throw new ArgumentNullException("localContext"); } // TODO: Implement your custom Plug-in business logic. IPluginExecutionContext context = localContext.PluginExecutionContext; IOrganizationService service = localContext.OrganizationService; // 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 parmameters. Entity caseObj = (Entity)context.InputParameters["Target"]; if (caseObj.LogicalName == "incident") { if (context.ParentContext.MessageName.ToLower() == "convertactivity") { string activityId = context.ParentContext.InputParameters["ActivityId"].ToString(); Entity queueItem = GetQueueDetails(service, activityId); if (queueItem != null) { if (queueItem.Attributes.Contains("queueid")) { EntityReference queue = (EntityReference)queueItem.Attributes["queueid"]; if (queue.Name.ToLower() == "xyz") { caseObj.Attributes["new_caseformtype"] = new OptionSetValue(100000000); } else if (queue.Name.ToLower() == "abc") { caseObj.Attributes["new_caseformtype"] = new OptionSetValue(100000001); } } } } } } } public static Entity GetQueueDetails(IOrganizationService service, string emailId) { try { string fetchXml = "<fetch version='1.0' output-format='xml-platform' mapping='logical' distinct='false'>"+ "<entity name='queueitem'>"+ "<attribute name='objectid' />"+ "<attribute name='queueid' />"+ "<filter type='and'>"+ "<condition attribute='objectid' operator='eq' uitype='email' value='" + emailId + "' />" + "</filter>"+ "</entity>"+ "</fetch>"; var fetchQuery = new FetchExpression(fetchXml); EntityCollection queueItemCol = service.RetrieveMultiple(fetchQuery); if (queueItemCol.Entities.Count > 0) { return queueItemCol.Entities[0]; } return null; } catch (FaultException ex) { throw new InvalidPluginExecutionException("An error occurred in fetching the Queue details.", ex); } } }

    Written a PreCreate Plug-in on the Case entity, the parentcontext will contain the Activity details with the messagename saying convertactivity.

    Retrieved the QueueItem record for that object(email) which contains the Queue details.

    The complete code snippet for my requirement, hope it helps someone.



    Regards Jeevan Please vote for me if you find the answer is useful.

    • Marked as answer by Jeevan264 Monday, November 30, 2015 4:04 PM
    Friday, November 27, 2015 4:58 PM

All replies

  • On create of case check if an activity exists. if so then the case has been created from an activity. Get the type and id of activity. Retreive data from QueueItemBase where Activity Type and id matches. You can get the queue name using following code:-

    
            string entityName = "email";
            Guid entityId = <GUIDOFEMAIL>;
            String fetchXml1 = string.Format(@"<fetch version='1.0' output-format='xml-platform' mapping='logical' distinct='false'>
                      <entity name='queueitem'>
                        <attribute name='objecttypecode' />
                        <attribute name='objectid' />
                        <attribute name='title' />
                        <attribute name='enteredon' />
                        <order attribute='enteredon' descending='true' />
                        <filter type='and'>
                          <condition attribute='objectid' operator='eq' uiname='' uitype='{0}' value='{1}' />
                        </filter>
                        <link-entity name='queue' from='queueid' to='queueid' alias='ab'>
                        <attribute name='name' />
                          <filter type='and'>
                            <condition attribute='queueid' operator='not-null' />
                          </filter>
                        </link-entity>
                      </entity>
                    </fetch>", entityName, entityId);
    
                    RetrieveMultipleRequest efr1 = new RetrieveMultipleRequest()
                    {
                        Query = new FetchExpression(fetchXml1)
                    };
                    EntityCollection queueCollection = ((RetrieveMultipleResponse)_orgService.Execute(efr1)).EntityCollection;
                    if (queueCollection.Entities.Count > 0)
                    {
                     
                        string QueueName = string.Empty;
                        if (queueCollection.Entities[0].Contains("ab.name"))
                        {
                            QueueName = (string)((AliasedValue)queueCollection.Entities[0].Attributes["ab.name"]).Value;
                        }
                    }

    I haven't tested this code but it should work.

    Regards

    Faisal


    Regards Faisal


    • Edited by Faisal Fiaz Friday, November 27, 2015 4:29 PM
    Friday, November 27, 2015 4:26 PM

  • protected void ExecutePreCaseCreate(LocalPluginContext localContext) { if (localContext == null) { throw new ArgumentNullException("localContext"); } // TODO: Implement your custom Plug-in business logic. IPluginExecutionContext context = localContext.PluginExecutionContext; IOrganizationService service = localContext.OrganizationService; // 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 parmameters. Entity caseObj = (Entity)context.InputParameters["Target"]; if (caseObj.LogicalName == "incident") { if (context.ParentContext.MessageName.ToLower() == "convertactivity") { string activityId = context.ParentContext.InputParameters["ActivityId"].ToString(); Entity queueItem = GetQueueDetails(service, activityId); if (queueItem != null) { if (queueItem.Attributes.Contains("queueid")) { EntityReference queue = (EntityReference)queueItem.Attributes["queueid"]; if (queue.Name.ToLower() == "xyz") { caseObj.Attributes["new_caseformtype"] = new OptionSetValue(100000000); } else if (queue.Name.ToLower() == "abc") { caseObj.Attributes["new_caseformtype"] = new OptionSetValue(100000001); } } } } } } } public static Entity GetQueueDetails(IOrganizationService service, string emailId) { try { string fetchXml = "<fetch version='1.0' output-format='xml-platform' mapping='logical' distinct='false'>"+ "<entity name='queueitem'>"+ "<attribute name='objectid' />"+ "<attribute name='queueid' />"+ "<filter type='and'>"+ "<condition attribute='objectid' operator='eq' uitype='email' value='" + emailId + "' />" + "</filter>"+ "</entity>"+ "</fetch>"; var fetchQuery = new FetchExpression(fetchXml); EntityCollection queueItemCol = service.RetrieveMultiple(fetchQuery); if (queueItemCol.Entities.Count > 0) { return queueItemCol.Entities[0]; } return null; } catch (FaultException ex) { throw new InvalidPluginExecutionException("An error occurred in fetching the Queue details.", ex); } } }

    Written a PreCreate Plug-in on the Case entity, the parentcontext will contain the Activity details with the messagename saying convertactivity.

    Retrieved the QueueItem record for that object(email) which contains the Queue details.

    The complete code snippet for my requirement, hope it helps someone.



    Regards Jeevan Please vote for me if you find the answer is useful.

    • Marked as answer by Jeevan264 Monday, November 30, 2015 4:04 PM
    Friday, November 27, 2015 4:58 PM